Created
December 18, 2016 08:36
-
-
Save swapnilshrikhande/3c6e367bbfb328f168d80ca043c94e11 to your computer and use it in GitHub Desktop.
Split Contact Name Into First, Middle and Last name components.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String s = 'FirstName MiddleName LastName AnotherName'; | |
String[] arrayS = s.split(' ', 3); | |
System.debug ( 'First :- '+ getFirstName(arrayS) ); | |
System.debug ('Middle :-'+ getMiddleName(arrayS) ); | |
System.debug ('Last Name :-'+ getLastName(arrayS) ); | |
s = 'FirstName MiddleName LastName'; | |
arrayS = s.split(' ', 3); | |
System.debug ( 'First :- '+ getFirstName(arrayS) ); | |
System.debug ('Middle :-'+ getMiddleName(arrayS) ); | |
System.debug ('Last :-'+ getLastName(arrayS) ); | |
s = 'FirstName LastNameAnotherName'; | |
arrayS = s.split(' ', 3); | |
System.debug ( 'First :- '+ getFirstName(arrayS) ); | |
System.debug ('Middle :-'+ getMiddleName(arrayS) ); | |
System.debug ('Last Name :-'+ getLastName(arrayS) ); | |
s = 'Lastname'; | |
arrayS = s.split(' ', 3); | |
System.debug ( 'First :- '+ getFirstName(arrayS) ); | |
System.debug ('Middle :-'+ getMiddleName(arrayS) ); | |
System.debug ('Last Name :-'+ getLastName(arrayS) ); | |
String getFirstName(String[] arraySP){ | |
if(arraySP != null && arraySP.size() >= 2 ){ | |
return arrayS[0]; | |
} | |
return ''; | |
} | |
String getLastName(String[] arraySP){ | |
return arraySP == null || arraySP.isEmpty() ? '' : arraySP[arraySP.size() - 1]; | |
} | |
String getMiddleName(String[] arraySP){ | |
if( arraySP != null && arraySP.size() >= 3 ){ | |
return arraySP[1]; | |
} | |
return ''; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment