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

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

Related

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

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.

String Split method not Giving desired results [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 7 years ago.
I am trying to use Split method of String in java I have example like this.
String number = Math.random() * 100 + "";
System.out.println("Number is : " + number);
String[] seprate = number.split(".");
System.out.println(seprate.length);
it should give me 2 Stack of array i mean 2 array element if value is like e.g. 67.90512897385857
but its not giving value like that
String number = Math.random() * 100 + "";
System.out.println("Number is : " + number);
String[] seprate = number.split(".");
System.out.println(seprate.length);
System.out.println(seprate[1]);
its giving arrayindexoutbound exception.
Someone give idea why its giving like that?
The String#split method takes a regular expression.
The "." in there means any character.
Escape your "." as such to signal a literal dot: number.split("\\.").
As Pieter De Bie points out, using java.util.regex.Pattern to safely escape your literals when passing literals to an argument that is going to be interpreted as a regular expression will help you a good deal.
In this case, you could use: number.split(Pattern.quote("."))
You need to escape the dot. The split method takes a regular expression. From the docs:
Parameters:regex the delimiting regular expression
String[] seprate = number.split("\\.");
Split works with regex and you should use like this
number.split("\\.")
Pay attention to the documentation:
public String[] split(String regex)
Splits this string around matches of the given regular expression.
In a regular expression, . is any character (except newlines, usually).
So you are splitting at every character.
If you want to match only a dot, "\\." will work.
Double f = Math.random() * 100;
String number = String.valueOf(f);
System.out.println("Number is : " + number);
String[] seprate = number.split("\\.");
System.out.println(seprate.length);
Please use this link for ur question.
The split() method in Java does not work on a dot (.)

Replace spaces in string with regex in Java [duplicate]

This question already has answers here:
Java regular expression to match _all_ whitespace characters
(7 answers)
Closed 8 years ago.
I'm trying to replace all all spaces in string with one space. I'm trying this:
String src = "2.       Test Sentence with spaces";
String out = src.replaceAll("\\s+", " ");
System.out.println(out);
And this is what I'm getting:
2.       Test Sentence with spaces
Spaces after dot were not replaced... Why?
You can try with the Unicode category: separator, space, combined with whitespace:
String input = "\u0020\u00A0\u1680\u2000\u2001\t"; //etc. 17 characters
System.out.println(input.replaceAll("[\\p{Zs}\\s]+", " "));
Output
[1 space]
See here for the list of characters in category Zs.

Regex split with a delimiter that contains no others on it's side [duplicate]

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Split a string by commas but ignore commas within double-quotes using Javascript
(17 answers)
Regex to replace a string not within quotes (single or double)
(1 answer)
Closed 9 years ago.
I'm stuck with this problem with regular expression.
Suppose i have a string that i've read from a file that contains.
first_name, Hello, "test Drive"
then i'll just split it using , as delimiter. and i'll get
myString[0] = "first_name";
myString[1] = "Hello";
myString[2] = "\"test Drive\"";
My problem is when the system read a string with a , inside the double quotes
first_name, Hello, "test, Drive"
i get
myString[0] = "first_name";
myString[1] = "Hello";
myString[2] = "\"test"
myString[3] = "Drive\"";
My Question
How would i split a string using , as delimiter with a condition that no " are present on it's left and right side.. or is there some workaround that will be much easier?
Thanks.
It looks like you're working on an CSV-file. Have you already considered to use one of the CSV-libraries to do this (like opencsv or supercsv)?
if you know the definite regex limit until then where you want to split, then below will work for you
String test = "first_name, Hello, \"test, Drive\"";
String[] tests = test.split(", ", 3);
System.out.println("1 " + tests[0]);
System.out.println("1 " + tests[1]);
System.out.println("1 " + tests[2]);
output :-
1 first_name
2 Hello
3 "test,Drive"
if you dont know the limit then input string should be in below format where if you can format the string within qutoes which has only (,) without space.. where other text delimiters with one comma (,) and space ( )
String test = "first_name, Hello, \"test,Drive\"";
String[] tests = test.split(", ");
System.out.println("1 " + tests[0]);
System.out.println("1 " + tests[1]);
System.out.println("1 " + tests[2])
output :-
1 first_name
2 Hello
3 "test,Drive"

Categories