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
SFDC pvSFDC pv 

How to convert comma separated number into a list

xx__c obj = [select xxnumber from xx__c];
in obj i will get the xxnumber's with comma seperated values (xxnumber = 12345,6789,09876). Here i need to split the comma's and add only the numbers to the list. 
Best Answer chosen by SFDC pv
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
! assume the "xxnumber" field is a String field, and that you want a list of numbers (not Strings).  You can separate the substring into a list of Strings using the "split" method, then convert those to numbers using the "Decimal.valueOf" method.  Something like this:

<pre>
Xx__c obj = [SELECT XxNumber__c FROM Xx__c LIMIT 1];
List<Decimal> numbers = new List<Decimal>();
for ( String num : obj.XxNumber__c.split( ',' ) )
{
    numbers.add( Decimal.valueOf( num ) );
}
</pre>

All Answers

Abhishek Saxena 106Abhishek Saxena 106

HI SFDC pv,

Can you explain a bit more with example so that i can help you out with your requirement.

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
! assume the "xxnumber" field is a String field, and that you want a list of numbers (not Strings).  You can separate the substring into a list of Strings using the "split" method, then convert those to numbers using the "Decimal.valueOf" method.  Something like this:

<pre>
Xx__c obj = [SELECT XxNumber__c FROM Xx__c LIMIT 1];
List<Decimal> numbers = new List<Decimal>();
for ( String num : obj.XxNumber__c.split( ',' ) )
{
    numbers.add( Decimal.valueOf( num ) );
}
</pre>
This was selected as the best answer
SFDC pvSFDC pv
Hi Abhishek,

My requirement is, User will enter the number's in Text field on xxx object with comma seperated values I want to make an API Call for each number and create records.Here i cannot send number with comma as a request so i need to split the comma and send only the number as request also API Call accepts only one number as a request at a time.

For eg.. User is giving the values like 12345, 67890,34567 . I want to get the response for each number and insert records on the YYY object.Since am sending three numbers as a request i need to get the response for 3 no's and insert 3 records. 

Please let me know in case if you need any further details on this. 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
This is what you're looking for.  The split method separates substrings into and array using the regular expression that you specify.  The first line does this using comma as the separator.  The second is forgiving if the user also types spaces.
<pre>
List<String> numbers = obj.XXNumber__c.split( ',' );
List<String> numbers = obj.XXNumber__c.split( '\\s*,\\s*' );
</pre>
SFDC pvSFDC pv
Hi @Glyn,

Thanks for your reply I tried both options provided above but unfortunately that doesn't help.  I still get o/p like (12345, 67890,34567 )
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Could you post your code, please?
SFDC pvSFDC pv
Hi Glyn, Thanks for your help i directly iterated the obj.XXNumber__c.split( ',' ); to the string num and i haven't add it to the list and its working fine now . Since i added it to the list it was coming like (12345) and then (12345,67890).  

I have tried the below code and its working fine
for ( String num : obj.XxNumber__c.split( ',' ) )
{    

  //Passed num directly to the response (Haven't add it to decimal like you mentioned above)
}
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
OK, great!  Adding to a list was just an example to show how you might use it.  I didn't know enough about your use case to know how you should use it.  I'm glad you got it working.  Could you select my post as the solution, please?  Thank you!