简体   繁体   English

Java字符串拆分多个分隔符

[英]Java string split with multiple delimeters

What would be the best way to split this string directly after the CN= to store both the first and last name in separate fields as shown below? 如下所示,在CN=之后直接拆分此字符串以将名字和姓氏都存储在单独的字段中的最佳方法是什么?

String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com"
String firstName"Paul"
String lastName="Sebula"

Don't re-invent the wheel. 不要重新发明轮子。 Assuming these are well-formed DN's, see the accepted answer on this question for how to parse without directly writing your own regex: Parsing the CN out of a certificate DN 假设这些是格式良好的DN,请参见此问题的可接受答案,以了解如何在不直接编写自己的正则表达式的情况下进行解析: 从证书DN中解析CN

Once you've extracted the CN , then you can apply some of the other parsing techniques suggested (use the Java StringTokenizer or the String.split() method as others here have suggested if it's known to be separated only by spaces). 提取CN ,您可以应用建议的其他一些解析技术(如果已知仅用空格分隔,则使用Java StringTokenizerString.split()方法,如此处其他建议的那样)。 That assumes that you can make assumptions (eg. the first element in the resulting array is the firstName ,the last element is the lastName and everything in between is middle names / initials) about the CN format. 假设您可以对CN格式进行假设(例如,结果数组中的第一个元素是firstName ,最后一个元素是lastName ,中间的所有内容都是中间名/缩写)。

You can use split : 您可以使用split

String distinguisedName = "CN=Paul Sebula,OU=BAE,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=baesystems,DC=com";
String[] names = distinguisedName.split(",")[0].split("=")[1].split(" ");
String firstName = names[0]; 
String lastName= names.length > 2 ? names[names.length-1] : names[1];
System.out.println(firstName + " " + lastName);

See IDEONE demo , output: Paul Sebula . 参见IDEONE演示 ,输出: Paul Sebula

This also accounts for just 2 names (first and last only). 这也仅占2个名称(仅名字和姓氏)。 Note how last name is accessed it being the last item in the array. 请注意,姓氏是数组中的最后一项,如何访问。

public static void main(String[] args) {

    String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com";
    String splitResult[]=distinguisedName.split(",")[0].split("=");
    String resultTwo[]=splitResult[1].split("\\.");
    String firstName=resultTwo[0].split(" ")[0].trim();
    String lastName=resultTwo[1].trim();
    System.out.println(firstName);
    System.out.println(lastName);

}

output 输出

Paul 保罗

Sebula bul

In steps: 步骤:

String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com";
String fullName = distinguisedName.substring(3, distinguisedName.indexOf(','));
String[] nameParts = fullName.split(" ");
String firstName = nameParts[0];
String lastName = nameParts[nameParts.length-1];

This will work for cases where the middle name/initial are not present as well. 这对于中间名/首字母也不存在的情况也适用。

String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com"

String[] commaSplit = distinguisedName.split(',');
String[] whitespaceSplit = commaSplit[0].split(' ');
String firstName = whitespaceSplit[0].substring(3);
String lastName = whiteSpaceSplit[2];

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

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