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
MaheemSamMaheemSam 

Split semi colon separated value into list and search in list

Hi, 

   String Alpha = ' A; B; C; D'; 
   String SearchString = 'A'
   I want to split the above string value as 

     A
     B
     C
     D

    Also Please suggest me how to search a string in list. 

Thanks
Sudhir
   

   
Best Answer chosen by MaheemSam
sfdcMonkey.comsfdcMonkey.com
No there is no standard function available which is returns value instead of boolean.
but it's simple logic you can get contains value something like this :
String Alpha = 'A; B; C; D';

List<String> parts = Alpha.split(';');

system.debug('parts' + parts);

Set<String> convertInSet = new Set<String>(parts);

String FinalValue;

if(convertInSet.contains('A')){
	FinalValue = 'A';
}
else if(convertInSet.contains('B')){
    FinalValue = 'B';
}
else if(convertInSet.contains('C')){
    FinalValue = 'C';
}
else if(convertInSet.contains('D')){
    FinalValue = 'D';
}

system.debug('Contains return value is :' + FinalValue);

Thanks
let me inform if it helps you :)
 

All Answers

David @ ConfigeroDavid @ Configero

Look at the string methods, mainly split()

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_split

You can search the values by adding them into a Set<> of Strings or a Map, depending on your implementation needs.


Please mark this answer as correct if it helped you.

sfdcMonkey.comsfdcMonkey.com
Hi Sudhir,

for split string use below method :
String Alpha = 'A; B; C; D'; 
List<String> parts = Alpha.split(';');

system.debug('parts' + parts);

for search any string in list use below code :
String Alpha = 'A; B; C; D'; 
List<String> parts = Alpha.split(';');

system.debug('parts' + parts);

Set<String> convertInSet = new Set<String>(parts);
if(convertInSet.contains('A')){
    system.debug('Yes string is in list');
}
i hope it helps you.
  kindly Let me inform if it helps you and close your query by choosing best answer if you got your right answer so it can helps others
thanks
sfdcmonkey.com
 
MaheemSamMaheemSam
Hi Piyush,

    Thanks for the reply need a clarification
  
    convertInSet.contains returns true Is there a way to return value instead of a boolean.  Please let me know

Thanks
Sudhir


 
sfdcMonkey.comsfdcMonkey.com
No there is no standard function available which is returns value instead of boolean.
but it's simple logic you can get contains value something like this :
String Alpha = 'A; B; C; D';

List<String> parts = Alpha.split(';');

system.debug('parts' + parts);

Set<String> convertInSet = new Set<String>(parts);

String FinalValue;

if(convertInSet.contains('A')){
	FinalValue = 'A';
}
else if(convertInSet.contains('B')){
    FinalValue = 'B';
}
else if(convertInSet.contains('C')){
    FinalValue = 'C';
}
else if(convertInSet.contains('D')){
    FinalValue = 'D';
}

system.debug('Contains return value is :' + FinalValue);

Thanks
let me inform if it helps you :)
 
This was selected as the best answer