简体   繁体   English

Java:字符串拆分

[英]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 第三部分-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. \\ w表示任何单词字符。 The + means 1 or more. +表示1或更大。 \\s means any whitespace character. \\ s表示任何空格字符。 Things in () are captured. ()中的内容被捕获。 The regex you would use in java code is 您将在Java代码中使用的正则表达式是

(\\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(" "); 您将要使用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: Java有一个String.split(String regex)方法,您可以像这样使用:

String s = "Mr John Smith Dickson"; String s =“约翰·史密斯·迪克森先生”; String[] parts = s.split(" "); String []个部分= 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]; 字符串标题= part [0]; String name = part[1] + " " + part[2] + " " + part[3]; 字符串名称= 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: 如果您始终在字符串开头使用Mr,Mrs,Dr之类的缩写,则可以使用正则表达式:

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

Regex in action. 正则表达式在行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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