2

I have the following string:

     Mr John Smith Dickson <[email protected]>

I want to split it into three parts:

1st part - Mr
2nd part - John Smith Dickson
3rd part - [email protected]

I'm confused with how I might go about accomplishing this, can anyone help?

the above name is just sample, the name might be vary, eg. John, John Smith, John Smith Dickson

2
  • there is no email in the first string Commented Nov 18, 2010 at 2:44
  • do u simply want to split just for this name or in a general any name in such a format. Commented Nov 18, 2010 at 2:45

7 Answers 7

3

You should use a regex. You can capture the first word up until whitespace, then the next three words separated by whitespace, then the thing in the angle brackets.

this works

(\w+)\s+(\w+\s+\w+\s+\w+)\s*<(.*)>

\w means any word character. The + means 1 or more. \s means any whitespace character. Things in () are captured. The regex you would use in java code is

(\\w+)\\s+(\\w+\\s+\\w+\\s+\\w+)\\s*<(.*)>

tested here

 http://www.regexplanet.com/simple/index.html

note that you could do this with splits, but anytime you are splitting, then getting the tokens, then splitting tokens, then getting more tokens, then splitting again, you are doing something too complicated. Regex greatly simplifies things.

Sign up to request clarification or add additional context in comments.

1 Comment

i'm not well verse in regex, if u don't mine can u give sample regex for this, thanks
1

You'll want to use yourstring.split(" "); This will split the string into each word (based on the spaces). If you know each person has three names, you could then say the following:

String myString = "Mr John Smith Dickson [email protected]";    
String[] splitResult = myString.split(" ");
String title = splitResult[0]; \\ Mr
String name = splitResult[1]+" "+splitResult[2]+" "+splitResult[3]; \\ John Smith Dickson
String email = splitResult[4];

If you don't know how many names a person has, it becomes a little more complicated.

Comments

1
String mister = "Mr John Smith Dickson - [email protected]";
String[] misters = mister.split(" ");

First part :

String first = misters[0];

Second :

String second = misters[1] + " " + misters[2] + " " + misters[3];

Third :

String third = misters[5];

Comments

1
String s = "Mr John Smith Dickson<[email protected]>";
Pattern pattern = Pattern.compile("(\\w+)\\b(.+)<(.+)>");
Matcher matcher = pattern.matcher(s);

if (matcher.matches()) {
 String title = matcher.group(1);
 String name = matcher.group(2);
 String email = matcher.group(3);
}

Comments

0

Java has a String.split(String regex) method that you could use like so:

String s = "Mr John Smith Dickson"; String[] parts = s.split(" ");

That will give you a string Array of the parts seperated by the spaces. then you could do something likes

Sting title = part[0]; String name = part[1] + " " + part[2] + " " + part[3];

Comments

0

Use some Regex.

Mr:

"([A-Za-z]{2,3}) "

John Smith Dickson

"[A-zA-z] (.*)<"

Email

".*<(.*)>"

Comments

0

If you always have abbreviations like Mr, Mrs, Dr at the beginning of the string you can use the regex:

^([A-Z][a-z]{1,2})\s+([^<]*)<(.*?)>$

Regex in action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.