st.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).
st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.
The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.
Assign the value to a variable, if not used directly:
st = st.replaceAll("\\s+","")
Answer from Gursel Koca on Stack Overflowst.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).
st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.
The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.
Assign the value to a variable, if not used directly:
st = st.replaceAll("\\s+","")
replaceAll("\\s","")
\w = Anything that is a word character
\W = Anything that isn't a word character (including punctuation etc)
\s = Anything that is a space character (including space, tab characters etc)
\S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)
(Edit: As pointed out, you need to escape the backslash if you want \s to reach the regex engine, resulting in \\s.)
[Java] Removing extra whitespace from a string and counting words in a string?
How to remove zero width space (zwsp) (Unicode U+200B) from a text file?
How to convert list to string without brackets or spaces?
join method of strings.
','.join(test)
Although, since you've got integers in that list, you'll have to convert them to strings.
','.join(str(num) for num in test)More on reddit.com
[Java] Ignoring case, spaces, and punctuation while comparing strings
Hello all,
I am a college student taking my first programming class, and need help with some parts of an assignment, and was hoping I could get some help here. In the program we're writing, the user is prompted for a string, and then we must delete all extra whitespace between the words (so no trim), as well as making sure the sentence meets the minimum word count.
Here is what I have written for the word counter, but it seems really bulky, and it also doesn't count words if they are proceeded by a comma or other (valid) non-letter character:
//Error for a sentence with too few words.
int wordCountStart = 0;
int wordCount = 1;
char b = sentence.charAt(wordCountStart);
while (wordCountStart < sentence.length())
{
while ((b == ' ') || (b == '(') || (b == ')') || (b == '.') || (b == '?') || (b == '!') || (b == ',') || (b == '/') || (b == '"') || (b == '[') || (b == ']') || (b == '-') || (b == ':') || (b == ';') ||(b == '\''))
{
wordCountStart ++;
if (wordCountStart == sentence.length()) break;
b = sentence.charAt(wordCountStart);
}
while (Character.isLetter(b))
{
wordCountStart ++;
if (wordCountStart == sentence.length()) break;
b = sentence.charAt(wordCountStart);
}
if (b == ' ')
{
wordCount ++;
}
}
if (wordCount < 7)
{
JOptionPane.showMessageDialog(null, "Your sentence needs to contain at least 7 words.", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}