function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj R.Raj R. 

How can I split a string by a semicolon and then get the first and last name in the string?

Hi,

I have a picklist(Multiselect) field, userPkList, that contains a list users in the format of "LastName, FirstName". Each value that is select is separated by a semicolon and the very last entry does not contain a semicolon. 

Example (LastName, FirstName) 1:
 "Schmo, Joe; Allen, Bary; Queen, Oliver"

Example (LastName, FirstName) 2:
"Queen, Oliver, Allen, Bary; Schmo, Joe; Last1, First1; LastAbc, FirstAbc"

I need help in figuring out a way to parse this picklist (multiselect) field so that I can pull each individual Last and First name and then run a query to get the user record associated to that user. I cannot figure out a good way to do this.

Example of desired logic:

//get userPkList values into a string
//parse string so i can separate all the values based on the semicolon (;) so that i now have a list<string> that is in the format of "lastName, FirstName" per record in the list<string>
//for every string in the list<string> I am going to split the string to get first and last name
//then insert first + ' ' + last into a separate List<String>
//using the new List<String>, obtain all the users in the systems.
 
Best Answer chosen by Raj R.
Raj R.Raj R.
Hi Ajay and Rohitsfdc,

I have implemented a combination fo your ideas. Below is what I ended up using to get the job done. 
 
String str = picklistFields;
String fullName = '';
String first;
String last;

List<String> fullNames = new List<String>();

for(string s : str) {
fullName = s.split(',');

first = fullName[fullName.size() -1);
last = fullName[fullName.size()-2);

//remove all trailing or leading whitespaces
first = first.trim();
last = last.trim(); 

fullName.add(first + ' ' + last);

}//end for 

List<User> usr = [Select Id, name from User Where Name IN: fullNames];

 

All Answers

rohitsfdcrohitsfdc
Hello,

Try this code
 
String fieldvalue = your picklist field;
list<String> firstname = new list<String> ();
list<String> lastname= new list<String> ();

 for(str names : fieldvalue.split(';'){
list<string> temp = (names.split(',');
if(temp!=null)
lastname.add(temp[0]);
if(temp.size()>1)
lastname.add(temp[1]);
}

If it does help, please mark like/mark this as solved.

Thanks
Ajay K DubediAjay K Dubedi
Hi,
try this one maybe it helps you.
string fullName = "Adrian Rules";
var names = fullName.Split(' ');
string firstName = names[0];
string lastName = names[1];
Thanks
 
Raj R.Raj R.
Hi Ajay and Rohitsfdc,

I have implemented a combination fo your ideas. Below is what I ended up using to get the job done. 
 
String str = picklistFields;
String fullName = '';
String first;
String last;

List<String> fullNames = new List<String>();

for(string s : str) {
fullName = s.split(',');

first = fullName[fullName.size() -1);
last = fullName[fullName.size()-2);

//remove all trailing or leading whitespaces
first = first.trim();
last = last.trim(); 

fullName.add(first + ' ' + last);

}//end for 

List<User> usr = [Select Id, name from User Where Name IN: fullNames];

 
This was selected as the best answer
SelvaRathinamSelvaRathinam
Hi ,
Please try this one.

String alpha = 'A,B';
String alpha1 = 'C,D';
String alpha2 = 'E,F';

List<String> lstAlpha = new List<string>();
lstAlpha.add(alpha);
lstAlpha.add(alpha1);
lstAlpha.add(alpha2);

System.debug('------'+lstAlpha);

for(string a:lstAlpha){
List<string> b=a.split(',');
string c = string.join(b,',');
List<String> d= c.split(',');
system.debug('-------'+d[0]+'-----'+d[1]);
}