Question

In Java, str=str.replaceAll("\\W", " "); what does the \\ and W means ? Thank you !

In Java, str=str.replaceAll("\\W", " ");
what does the \\ and W means ? Thank you !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,Let me know if you need more information:-

===========================================

StringReplace.java

============================================

public class StringReplace {

   public static void main(String[] args) {

       String str = new String(
               "__ a ! . 1   élève BCD GOÄ_432 GOÄ_432 ; : - ఆది - A a gefräßig gefräßig gefrä GHD \\W ` ~ @ # $ % ^ & * ( ) - _ + = ] [ { } \\ | ");
       String str1 = new String(str);
       System.out.println(str);
       str = str.replaceAll("\\W", "|NONWORD|");
       // --> \W means A non word character
       System.out.println("[" + str + "]");
       System.out.println();
       // --> \w matches word characters
       str1 = str1.replaceAll("\\w", "|WORD|");
       System.out.println("[" + str1 + "]");

       // --> \ means you are skipping character next to it.
       String str3 = "\\5\\7"; // Here actual string value is --> \\5\\7
       System.out.println("\5\6");
       System.out.println("Actual:"+str3);
       // to skip the values you are mentioning \ before \5 and \7
       String replace = "\\\\";
       System.out.println(replace);
       str3 = str3.replaceAll("\\\\", "-");
       // the actual value i want to replace in the above string is --> \ so i
       // am giving \\
       // now \5\7 will become --> -5-7
       System.out.println("Value: " + str3);

       // Description:
       // \ --> escape character in java Strings.
   }

}

==============================================

OUTPUT:-

================================================

__ a ! . 1   élève BCD GOÄ_432 GOÄ_432 ; : - ఆది - A a gefräßig gefräßig gefrä GHD \W ` ~ @ # $ % ^ & * ( ) - _ + = ] [ { } \ |
[__|NONWORD|a|NONWORD||NONWORD||NONWORD||NONWORD||NONWORD|1|NONWORD||NONWORD|l|NONWORD|ve|NONWORD|BCD|NONWORD|GO|NONWORD|_432|NONWORD|GO|NONWORD|_432|NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD|A|NONWORD|a|NONWORD|gefr|NONWORD||NONWORD|ig|NONWORD|gefr|NONWORD||NONWORD|ig|NONWORD|gefr|NONWORD||NONWORD|GHD|NONWORD||NONWORD|W|NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD|_|NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD||NONWORD|]

[|WORD||WORD| |WORD| ! . |WORD|   é|WORD|è|WORD||WORD| |WORD||WORD||WORD| |WORD||WORD|Ä|WORD||WORD||WORD||WORD| |WORD||WORD|Ä|WORD||WORD||WORD||WORD| ; : - ఆది - |WORD| |WORD| |WORD||WORD||WORD||WORD|äß|WORD||WORD| |WORD||WORD||WORD||WORD|äß|WORD||WORD| |WORD||WORD||WORD||WORD|ä |WORD||WORD||WORD| \|WORD| ` ~ @ # $ % ^ & * ( ) - |WORD| + = ] [ { } \ | ]

Actual:\5\7
\\
Value: -5-7


=====================================================

===========================================

Thanks

Add a comment
Know the answer?
Add Answer to:
In Java, str=str.replaceAll("\\W", " "); what does the \\ and W means ? Thank you !
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT