Home > front end >  Contains method of String failing for "." in Java
Contains method of String failing for "." in Java

Time:10-08

I have a string array as shown below -

String[] emails = {"test.email [email protected]","test.e.mail [email protected]","testemail [email protected]"};

I want to see if the string contains "."," " and "@". Based on this, I will do some processing on the string. The issue is contains for "." is failing even if the string has "." Also, contains is failing for " " and "@" if the string has "." before " " and "@".

I dont understand why so? Need help.

Below is the code -

public static void main(String[] args) {
        
        String[] emails = {"test.email [email protected]","test.e.mail [email protected]","testemail [email protected]"};
        
        if(emails == null || emails.length == 0)
            System.out.println(0);
        
        HashSet<String> emailsSet = new HashSet<>();
        
        for(int i = 0; i < emails.length; i  ){
            
            int splitIndex = emails[i].indexOf("@");
            
            if(splitIndex > 0){
                String localName = (emails[i].substring(0,splitIndex)).trim();
                String domainName = (emails[i].substring(splitIndex,emails[i].length())).trim();
                System.out.println(localName);
                System.out.println(domainName);
                
                if(domainName.length() <= 1)
                    System.out.println(0);
                
                if(localName.contains("."))
                    localName = (localName.replaceAll(".","")).trim();
                System.out.println(localName);
                
                int index = localName.indexOf(" ");
                
                if(index >= 0){
                    localName = (localName.substring(0,index)).trim(); 
                }
                System.out.println(localName);
                
                if(localName.length()>0){
                    emailsSet.add(localName domainName);
                }
               System.out.println(emailsSet); 
                
            }
      
            }
        System.out.println(emailsSet.size());
        }

Below is the output -

test.email alex
@leetcode.com


[]
test.e.mail bob.cathy
@leetcode.com


[]
testemail david
@lee.tcode.com
testemail david
testemail
[testemail@lee.tcode.com]
1

CodePudding user response:

replaceAll expects the regular expression to which this string is to be matched as the first argument. You need to escape dot, plus (and other regexp metacharacter) with a backslash.

Even with two backslashes, because Java string requires to escape a backslash with a backslash. It must look like this:

localName.replaceAll("\\.","")

According to regular expression a non-escaped dot means "any character".

https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended

CodePudding user response:

There are few special characters in Regex. If you have to use them in some regex as normal character you need to escape them (using \\ before them).

replaceAll method in String class has first parameter as Regex and here you are trying to replace ".", which itself is 1 out of those special characters. So you would need to escape it as below :

localName.replaceAll("\\.","")
  • Related