How to compare two Strings in java without considering spaces?
java, string
Solution
You can try to create a new string by replacing all empty spaces.
if(a.replaceAll("\\s+","").equalsIgnoreCase(b.replaceAll("\\s+",""))) {
// this will also take care of spaces like tabs etc.
}
then compare.
Problem
I have one example. ``` public class Test { public static void main(String[] args) { String a="VIJAY KAKADE"; String b="VIJAY KAKADE"; if(a.equalsIgnoreCase(b)){ System.out.println("yes"); }else{ System.out.println("no"); } } } ``` I need to check these strings without considering spaces. How do I achieve this? How do I ignore spaces in the strings when I compare them?