vr3w3c9
vr3w3c9

Reputation: 1178

Split String using delimiter in Java

I need help in splitting two email address which are seperated by a Delimiter 'AND'. I have issue when splitting, when the email address has got the characters'AND' in the email id. For eg, if the email address that needs to be split is something like the below. There are no whitespaces between the two email address.

'[email protected]@yahoo.co.in', and the delimiter is'AND' 

In the above case, there seems to be three items extracted instead of two. Can someone please help me solve this. Thanks in Advance

Upvotes: 0

Views: 6898

Answers (4)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35597

You can use " AND " as delimiter.

 String str="[email protected] AND [email protected]";
 String[] emailArr=str.split(" AND "); 

Or you can use following regex

  String str = "[email protected] AND [email protected]";
  Pattern p = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+
                                   (\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})");
  Matcher matcher = p.matcher(str);
  while (matcher.find()) {
     System.out.println(matcher.group(0));
  }

Out put

 [email protected]
 [email protected]

Upvotes: 2

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Use this :

String[] splits = text.split("\\s+AND\\s+");

Upvotes: 1

Monicka Akilan
Monicka Akilan

Reputation: 1549

Giving correct output

public class Test {
        public static void main(String args[]) {
            String text = "[email protected] AND [email protected] ";
            String[] splits = text.split(" AND ");
            for (int i = 0; i < splits.length; i++) {
                System.out.println("data :" + splits[i]);
            }
        }

    }

Output is

data :[email protected]    
data :[email protected] 

Upvotes: 1

Mathiewz
Mathiewz

Reputation: 185

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) the regular expression will be case sensitive

actually, the best is to use delimiters exression that you are sure will not be in the adress

Upvotes: 0

Related Questions