Removes spaces and words that begin with a specific character from a Java string
•
Java
Remove spaces and words beginning with specific characters from Java strings
Code spaces are eliminated by the following code areas:
String str1= "This is symbel for sNow and silk. Grapes are very dear" String str2=str1.replaceAll(" ",""); System.out.println(str2);
It gives this output: –
output: ThisissymbelforsNowandsilk.Grapesareverydear
But I want to eliminate all the words in STR1, start with char's' (symbol snow silk) and char'd '(Dear), and get the following output: –
output: Thisisforand.Grapesarevery
How to modify this code?
Solution
Do it in this way.
String str1= "This is symbel for sNow and silk. Grapes are very dear"; System.out.print(str1.replaceAll("[sd][a-z]+|[ ]+",""));
Explanation
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码