Java string Util - Split the String with space

188 Views Asked by At

If i'm sending the string with numberic to below code, its returning with % symbol.

Example :

st1a - st%a Why numeric replacing with % here ? but if stack overflow , its coming up with stack as string and overflow as another string.

Please advise.

String aName =StringUtils.lowerCase(Number.replaceAll("[^a-zA-Z]", "%"));
3

There are 3 best solutions below

0
Henry Keiter On BEST ANSWER

If you're just trying to split the string around a space, use split:

String[] splitName = "qw234 ant".split(" "); // Produces {"qw234", "ant"}

Or you can treat runs of consecutive whitespace characters as a single delimiter, since split takes a regex:

String[] splitName = "qw234          ant".split("\\s+"); // Produces {"qw234", "ant"}

To fully understand what split is doing, take a look at the documentation in the link above, so you can modify this to split your strings however you like. I can't tell what else you might be asking, so without clarification, this is the best I can do for you.

0
Óscar López On

To remove all spaces anywhere in a string (at the start, at the end, in the middle) try this:

str = str.replaceAll("\\s","");
0
user2613077 On
newString[] = "qw23ant".split(23)

This should split your string into two parts, "qw" and, "ant" and save them in a string array, "newstring"