This question already has answers here:
Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
(32 answers)
Closed 8 years ago.
so I have been looking on here and I can find alot of solutions that either completely remove all white space or just remove spaces, or just remove tabs. Basically what I need/ want is a way to take a string, and turn all double spaces+ or tabs and turn them into a single space. ie
String temp = "This is a test for strings";
String result = "This is a test for strings";
any ideas? possible java library methods?
Use String.replaceAll:
String result = temp.replaceAll("\\s+", " ");
where \\s+ stands for more than one whitespace character.
You can use regExp with method #replaceAll other than that you can first use trim to remove leading and trailing spaces.
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " ");
Here \\s+ is regExp which means one or more spaces which will be replaced with single space by replaceAll method.
Try this:
String temp = "This is a test for strings";
String result = temp.replaceAll("\\s+", " "));
Related
This question already has answers here:
How to remove duplicate white spaces in string using Java?
(9 answers)
Closed 6 years ago.
A String can contain multiple spaces in a row - I need to replace multiple subsequent spaces by one single space char. The "problem" is that i cant know how many spaces there may encounter. The function I look for shall not only replace the first occurance of a found match, but all multiple equal characters in a String.
I searched a lot on the internet and tried the regex "X*? (X, zero or more times)" which I found unter "Reluctant quantifiers" on https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum
That didnt work: s1 = s1.replaceAll(" *?", " ");
Where s1 = "Hello World"; should be converted to s1 = "Hello World";
I'd be thankful for any help.
You can use replaceAll() that replaces whitespaces with just a single space.
String st = "helllo world"
System.out.println(st.replaceAll("\\s+"," "))
Output : helllo world
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I have a string "domain\cdsid", where "\" is the delimiter, all I want is to split the string and just print "cdsid".
Input string : "domain\cdsid"
Output string : "cdsid"
How do I do this ?
Try this (using split) :
String myText = "domain\\cdsid";
System.out.println(myText.split("\\\\")[1]);
Output :
cdsid
In Java String object "\" is used to define any escape sequence character like \n for new line, \t for tab, \\ for having a backslash in the String object.
So instead of writing String object as
String str = "domain\cdsid";
You have to write
String str = "domain\\cdsid";
The first option will give compile time error. Java will expect that after backslash their must be some escape sequence character but it is not their in first case. It will compile time error as
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
In the above compile time error each separate value is a escape sequence character in java.
So your final code will be
String str = "domain\\cdsid";
System.out.println(str.split("\\\\")[1]);
Hope this helps.
Splitting is a way that I will recommended to go when you need all the elements resulting of the operation... this is because the result will generate an Array of Strings (what a waste of memory generating an array to only get ONE element! :) dont you think??)
in your case something like regex or just substrings will gently provide you the correct answer..
consider:
String txt = "domain\\cdsid";
System.out.println(txt.substring(txt.indexOf("\\") + 1));
output:
cdsid
This question already has answers here:
String.split() *not* on regular expression?
(8 answers)
Closed 2 years ago.
In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.
Finding indices (by indexOf and lastIndexOf) does not use regex, so
String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3
gives me the index I need.
However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...
String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need either of the following:
A way to split into an array by a non-regex target
A way to "convert" a non-regex target string into a regex string
Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?
You need to escape your "target" in order to use it as a regex.
Try
String[] someStringSplit = someString.split(Pattern.quote(target));
and let me know if that helps.
String::split do split without regex if the regex is:
a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
two-char String and the first char is the backslash and the second is
not the ascii digit or ascii letter.
Please see String::split() source code for details.
For escaped '.' target it is going to be split without regex.
You can try this one.
String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);
while (tokenValue.hasMoreTokens()) {
System.out.println(tokenValue.nextToken());
}
This question already has answers here:
Replace/remove String between two character [duplicate]
(3 answers)
Closed 7 years ago.
I am interested in using the String.replaceAll function that will allow me to remove a specific sequence of a String.
Does the functionality of String.replaceAll replace the first occurrence of the String sequence given to, well replace? My assumption is yes.
Going about replacing the particular sub-string, how would I replace the characters in the provided String?
String sentence = "#red##blue##green#Hello#reset#";
The substring that I want to be removed is from the # to the other #, in this case, while running String.replace
String.replaceAll("#**PATTERN HERE SO NAME WON"T MATTER**#", "");
EDIT
Seeing replace and replaceAll makes since between the two differences, being one using regex.
My concern is removing the first occurrence of the expression given, being the original string of
String sentence = "#red##blue##green#Hello#reset#";
Would have to be ran 4 times in order to have "Hello" as the remaining member of the String.
Example:
Run 1 - replaceFirstOccurence("#regex#", "");
System.out.println(sentence); --> "#blue##green#Hello#reset#"
Run 2 - replaceFirstOccurence("#regex#", "");
System.out.println(sentence); --> "#green#Hello#reset#"
Run 3 - replaceFirstOccurence("#regex#", "");
System.out.println(sentence); --> "Hello#reset#"
Run 4 - replaceFirstOccurence("#regex#", "");
System.out.println(sentence); --> "Hello"
use standart method of String replaceAll(String regularExpression, String replacement)
for example:
"#some chars#".replaceAll("#[^#]*#", "##");
Alright to answer this question is actually extremely simple but the regex was the hard part.
Thanks to #waxtah for the regex that answered this question.
Using String.replaceFirst("#[^#]*#", "");
I was able to achieve my goal. Thanks to everyone here.
This question already has answers here:
Removing repeated characters in String
(4 answers)
Closed 8 years ago.
Lets say I have a string:
tttteeeeeeessssssttttttt
Using the power of regex, how can that string be turned into:
test
At first look it seems easy to do, but the current code (not regex) I have for it is not behaving well and im pretty sure regex is the way to go.
You can use:
str = str.replaceAll("([A-Za-z])\\1+", "$1");
RegEx Demo
Use string.replaceAll function.
strng.replaceAll("(.)\\1+", "$1");
The above regex captures the first character in the sequence of same characters and matches all the following one or more characters (which must be same as the one inside the capturing group) . Replacing those characters with the character inside group index 1 will give you the desired output.
Example:
System.out.println("tttteeeeeeessssssttttttt".replaceAll("(.)\\1+","$1" ));
Output:
test
(.)(?=\1)
Try this.Replace by empty string.See demo.
https://regex101.com/r/tX2bH4/41
str = str.replaceAll("(.)(?=\\1)", "");