1

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?

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"
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Sean
  • 1,283
  • 9
  • 27
  • 43

5 Answers5

3

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

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). 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.

Community
  • 1
  • 1
Jacob Zwiers
  • 1,092
  • 1
  • 13
  • 33
  • Good answer. If a language has an available parser, use that parser. Unless you have the DN specification at hand, let others take care of the ugly details. – tucuxi Jul 10 '15 at 15:40
1

You can use 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.

This also accounts for just 2 names (first and last only). Note how last name is accessed it being the last item in the array.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1
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

KDP
  • 1,481
  • 7
  • 13
0

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.

GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
0
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];
dbillz
  • 210
  • 1
  • 7