Replace spaces in string with regex in Java [duplicate] - java

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.

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

Remove all occurrences of char "." from string in Java [duplicate]

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 4 years ago.
I would like to remove the period/decimal character from a String using Java.
String originalString = "1.2345";
originalString = originalString.replaceAll(".", "");
Printing originalString returns empty.
How can I remove . from originalString?
The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:
originalString = originalString.replaceAll("\\.", "");
String originalString = "1.2345";
originalString = originalString.replaceAll("\\.", "");

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

Using regex to trim a specific word in String [duplicate]

This question already has answers here:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.

How do I replace white spaces to nbsp? [duplicate]

This question already has answers here:
Java Regex that only replaces multiple whitepaces with Non-Breaking Spaces
(3 answers)
Closed 8 years ago.
How do I convert any white spaces to nbsp? This doesn't seem to work for me:
String temp = "This is practice";
temp = temp.replaceAll(" ", "nbsp");
It is literally adding the string nbsp in, how do I make it so it actually counts as a no break space?
To replace literally all "whitespace" (spaces, tabs, etc), do this:
temp = temp.replaceAll("\\s", " ");
\s is the regex for "whitespace".
Unicode 'no-break space' character has code 0xA0, so, instead of an HTML entity, you could use it directly: "\u00A0".
The correct string to use is " "
so your code should be
temp = temp.replace(" ", " ");

Categories