简体   繁体   中英

Java: String splitting

I have the following string:

     Mr John Smith Dickson <john@yahoo.com>

I want to split it into three parts:

1st part - Mr
2nd part - John Smith Dickson
3rd part - john@yahoo.com

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

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.

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 john@yahoo.com";    
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.

String mister = "Mr John Smith Dickson - john@yahoo.com";
String[] misters = mister.split(" ");

First part :

String first = misters[0];

Second :

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

Third :

String third = misters[5];
String s = "Mr John Smith Dickson<john@yahoo.com>";
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);
}

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];

Use some Regex.

Mr:

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

John Smith Dickson

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

Email

".*<(.*)>"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM