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
Related
This question already has answers here:
Trim a string based on the string length
(11 answers)
Closed 1 year ago.
Say I have an unknown string of 100+ characters and I want to remove the last characters before the 100th character and replace them with ..., deleting every character after the 100th. ie:
I would like to remove the last three characters before the 100th character and replace them with a period and deleting everything after.
would become:
I would like to remove the last three characters before the 100th character and replace them with....
What is the best way to tackle this problem?
Let String original = ... denote the first text. Then I suspect the following String is what you seek:
String result = String.format("%s%s", original.substring(0, Math.min(100, original.length())), original.length() > 100 ? "..." : "")
This question already has answers here:
Regexp to remove specific number of occurrences of character only
(2 answers)
Closed 2 years ago.
I was wondering how I could split a String by : but not :: using String#split(String)
I am using Java if it makes a difference.
I looked around a lot and I couldn't find anything, and I'm not familiar with Regex...
Example:
coolKey:cool::value should return ["coolKey", "cool::value"]
cool::key:cool::value should return ["cool::key", "cool::value"]
You could try splitting on (?<!:):(?!:):
String input = "cool::key:cool::value";
String[] parts = input.split("(?<!:):(?!:)");
System.out.println(Arrays.toString(parts));
This prints:
[cool::key, cool::value]
The regex used here says to split when:
(?<!:) the character which precedes is NOT colon
: split on colon
(?!:) which is also NOT followed by colon
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to strip a string of all sequences that begin with a string "GUGU" and and end with something like "AGAG". The closest I've gotten is
replaceAll("GUGU(.*)AGAG", "")
but all that does is replace the "largest" instance. Meaning if there are multiple occurrences of GUGU*AGAG in a string, it only matches the outermost. So what could I do to get this to work for every instance of the regex in the string?
Use a reluctant rather than a greedy quantifier (see the documentation):
String s = "hello GUGU hello AGAG hello GUGU hello AGAG";
// greedy
System.out.println(s.replaceAll("GUGU(.*)AGAG", ""));
// prints "hello "
// reluctant
System.out.println(s.replaceAll("GUGU(.*?)AGAG", ""));
// prints "hello hello "
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:
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+", " "));