Java split String that start with space [duplicate] - java

This question already has answers here:
How to prevent java.lang.String.split() from creating a leading empty string?
(9 answers)
Closed 4 years ago.
I know how to split a string by space as the following:
String[] array = string.split(" ");
This works great until I try to split string that starts with a space like
" I like apple"
The result looks something like this:
{"", "I", "like", "apple"}
How can I split the string so it only keeps strings that is not empty?

You can call string.trim()and then string.split(" "). The trim() method removes spaces before the first non-space-character and after the last non-space-character.

To remove leading and trailing spaces, you can use .trim().
String[] array = string.trim().split(" ");

Related

how to replace a char within a string with "nothing" [duplicate]

This question already has answers here:
Java replace method, replacing with empty character [duplicate]
(2 answers)
Closed 2 years ago.
String mine = sc.next();
String corrected = mine.replace('.', '????');
System.out.println(corrected);
that's my code. let's assume that my input on String corrected is "<..><.<..>>" , and I want to replace every "." with a null space, so I get an output like "<><<>>". is there any way to do it?
If you want to replace . with an empty ("") string, you can just do:
mine.replace(".", "");
Alternatively, you can also check .replaceAll()
Try this to replace all occurrences of . with empty:
mine.replaceAll("\\.", "")
If you don't want any method, you can do it like this.
String str = "<<.>>.<>.<<.";
String [] parts = str.split("\\.");
for(String s:parts){
System.out.print(s);
}
Because I tried the method replaceAll(".", "") ;
But that method does not allow empty or null spaces in a string.
I don't know if it's the best way, but that's what I can think of.

Java split string with regex, anything inside Double Quotes [duplicate]

This question already has answers here:
Regex for splitting a string using space when not surrounded by single or double quotes
(16 answers)
Closed 2 years ago.
I am trying to split a string such as String s = "do not split this \"split this\"";
String[] split = s.split("(?<=\\s)| (?=\") | ((?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9]));
will give me ["do", " ", "not", " ", "split", "this", " ", "split this"];
I would like to keep all words, white spaces as well, but ignore anything inside double quotes~
just a guess:
String s = "do not split this \"split this\"";
String[] split = s.split( "(?<!\".{0,255}) | (?!.*\".*)" ); // do, not, split, this, "split this"
don't split at the blank, if the blank is surrounded by quotes
split at the blank when the 255 characters to the left and all characters to the right of the blank are no quotes

In java, can i use string split to split by character? [duplicate]

This question already has answers here:
Split string into array of character strings
(12 answers)
Closed 3 years ago.
For instance, I have the String variable abbaabbbba. I would like to use String split so each character would be separated, so it would be {a,b,b,a,a,b,b,b,b,a}. All the instructions I can find for string split say i need to have something, like a a space:
String mySplit = str.split("/");
Is there anyway to do this by character?
You can use the toCharArray method like this:
String myString = "abbaabbbba";
char[] myCharacters = myString.toCharArray();

How to split a string by keywords in Java [duplicate]

This question already has answers here:
Split String In JAVA by specific words
(2 answers)
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
I need to split an input and put it in to a list according to a list of keywords or delimiters that I have created.
I've tried simply splitting the string by spaces and then processing it afterwards, but the problem is that some of my keywords have spaces in them so they are split apart which causes undesired behavior.
// "Jr.", "III", "Sr", "pro se" are all keywords in my list
String input = "Abraham Lincoln Jr. III Sr pro se";
String [] splitBySpace = input.split(" ");
List<String> separatedName = new ArrayList<>(Arrays.asList(splitBySpace);
My list ends up being: {Abraham, Lincoln, Jr., III, Sr, pro, se}
And I would like it to be: {Abraham, Lincoln, Jr., III, Sr, pro se}
This output would also work: {Abraham Lincoln, Jr., III, Sr, pro se} (I don't need the strings that are not in my delimiter list to be split apart)
You could use this: \s(?=(Jr\.|III|Sr|pro se)) - and add in the keywords you want to preserve.
Here's a working demo to see what it matches: https://regexr.com/47gbm
So with String[] split = input.split("\\s(?=(Jr\\.|III|Sr|pro se))"); you should get the desired result.
You can try adding a different seprator to the string hoding the keywords. Insted of seprating them by " " separate them by "|".
String input = "Abraham|Lincoln|Jr.|III|Sr|pro se";
String [] splitByPipe = input.split("\\|");
This way you won`t be having any problems with the spaces you need to keep.

Android: Split string by "\n" as string not as new line [duplicate]

This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 4 years ago.
I have a below string which is coming from the server
String text = "- 30016264\n- 30014837\n- 30014836\n";
When I used to split it like this
String[] list = text.split("\n");
I got the list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
And when I used to split it like this
String[] list = text.split("\\n");
I got the same list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
How do I write the code to split the string on basis of "\n" not the next line?
NOTE: This string is coming from the server as it is written here and when I use this server string as TextView value, it will display in one single line.
If you input is coming from the server and in this format :
- 30016264\n- 30014837\n- 30014836\n
Then, in Java it should be represented with double backslash like this :
- 30016264\\n- 30014837\\n- 30014836\\n
because backslash is a special character in Java, you have to escape it with another backslash.
Then to split with \\n you need to use \\\\n, why 4 backslashes because like i said before the backslash is special character for that you have to escape each one with another backslash for that you need 4 instead of 2 or 1.
Your solution should look like :
String text = "- 30016264\\n- 30014837\\n- 30014836\\n";
String[] split = text.split("\\\\n");
Outputs
- 30016264
- 30014837
- 30014836

Categories