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.)
How do you remove all whitespace from a string in Java, including tabs?
How to remove whitespace from the beginning and end of a string in Java?
How can I remove all whitespace from a Java string using Java Streams?
java.lang.String class has method substring not substr , thats the error in your program.
Moreover you can do this in one single line if you are ok in using regular expression.
a.replaceAll("\\s+","");
Why not use a regex for this?
a = a.replaceAll("\\s","");
In the context of a regex, \s will remove anything that is a space character (including space, tab characters etc). You need to escape the backslash in Java so the regex turns into \\s. Also, since Strings are immutable it is important that you assign the return value of the regex to a.
There are two flavors of replace() - one that takes chars and one that takes Strings. You are using the char type, and that's why you can't specify a "nothing" char.
Use the String verison:
gtg = gtg.replace("\t", "");
Notice also the bug I corrected there: your code replaces chars from the original string over and over, so only the last replace will be effected.
You could just code this instead:
public static String removeWhitespace(String s) {
return s.replaceAll("\\s", ""); // use regex
}
Try this code,
public class Main {
public static void main(String[] args) throws Exception {
String s = " Test example hello string replace enjoy hh ";
System.out.println("Original String : "+s);
s = s.replace(" ", "");
System.out.println("Final String Without Spaces : "+s);
}
}
Output :
Original String : Test example hello string replace enjoy hh
Final String Without Spaces : Testexamplehellostringreplaceenjoyhh
Another way by using char array :
public class Main {
public static void main(String[] args) throws Exception {
String s = " Test example hello string replace enjoy hh ";
System.out.println("Original String : "+s);
String ss = removeWhitespace(s);
System.out.println("Final String Without Spaces : "+ss);
}
public static String removeWhitespace(String s) {
char[] charArray = s.toCharArray();
String gtg = "";
for(int i =0; i<charArray.length; i++){
if ((charArray[i] != ' ') && (charArray[i] != '\t') &&(charArray[i] != '\n')) {
gtg = gtg + charArray[i];
}
}
return gtg;
}
}
Output :
Original String : Test example hello string replace enjoy hh
Final String Without Spaces : Testexamplehellostringreplaceenjoyhh
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);
}