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
ranveer singh 8ranveer singh 8 

Splitting of string

Hi guys,  i have a sting str = (majestic badger, fluffy bunny, scary bear, chicken) ,now i want to split it in such a way that if  i use
 index0 ,majestic badger  should be output
index1,fluffy bunny should be output
index2, scary bear .....like this how i can this?
Best Answer chosen by ranveer singh 8
Amit Chaudhary 8Amit Chaudhary 8
I Executed above code. It giving me below out put
String str = 'majestic badger, fluffy bunny, scary bear, chicken' ;
List<String> strList = str.split(',');
For(String s : strList )
{
 System.debug('---------->'+s);
}


 System.debug('---------->'+strList[0]);
 System.debug('---------->'+strList[1]);
Output
37.0 APEX_CODE,DEBUG
Execute Anonymous: 
Execute Anonymous: String str = 'majestic badger, fluffy bunny, scary bear, chicken' ;
Execute Anonymous: List<String> strList = str.split(',');
Execute Anonymous: For(String s : strList )
Execute Anonymous: {
Execute Anonymous:  System.debug('---------->'+s);
Execute Anonymous: }
Execute Anonymous: 
Execute Anonymous: 
Execute Anonymous:  System.debug('---------->'+strList[0]);
Execute Anonymous:  System.debug('---------->'+strList[1]);

16:00:10.11 (12001027)|EXECUTION_STARTED
16:00:10.11 (12007316)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:00:10.11 (12771841)|USER_DEBUG|[6]|DEBUG|---------->majestic badger
16:00:10.11 (12798537)|USER_DEBUG|[6]|DEBUG|----------> fluffy bunny
16:00:10.11 (12820075)|USER_DEBUG|[6]|DEBUG|----------> scary bear
16:00:10.11 (12847535)|USER_DEBUG|[6]|DEBUG|----------> chicken
16:00:10.11 (12878602)|USER_DEBUG|[10]|DEBUG|---------->majestic badger
16:00:10.11 (12903714)|USER_DEBUG|[11]|DEBUG|----------> fluffy bunny
16:00:10.11 (12964681)|CODE_UNIT_FINISHED|execute_anonymous_apex
16:00:10.11 (14292710)|EXECUTION_FINISHED

If your String is like below
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;

try below code.
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;
str=str.remove('(');
str=str.remove(')');

List<String> strList = str.split(',');
For(String s : strList )
{
 System.debug('---------->'+s);
}


 System.debug('---------->'+strList[0]);
 System.debug('---------->'+strList[1]);

Let us know if this will help you
 

All Answers

ranveer singh 8ranveer singh 8
+ing to above comments

if  i am directly splitting str.split(',');
then i am getting result in str[0] and str[3] as (majestic badger and chicken) respectively here i donot want that bracket symbol....how can we do this
Jennifer Dos Reis ICOJennifer Dos Reis ICO

You could use the split method of the String Class.

For example: str.split(',', 4)

In this page there are some examples 

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

Amit Chaudhary 8Amit Chaudhary 8
just try below code.

String str = 'majestic badger, fluffy bunny, scary bear, chicken' ;
List<String> strList = str.split(',');
For(String s : strList )
{
 System.debug('---------->'+s);
}


 System.debug('---------->'+strList[0]);
 System.debug('---------->'+strList[1]);

Let us know if that will work for u
ranveer singh 8ranveer singh 8
....@ amit thanks for help but as per ur suggestion we will be getting (majestic badger as output in strList[0] where as i need only majestic badger as output.
ranveer singh 8ranveer singh 8
@Jennifer Dos Reis ICO thanks for help ...........i got it ......

we need to code like this

global class AnimalLocator{
     
    public static object requiredstring;
    public static string result; 
    public static string name;
    public static string[] strary;
    public static string[] strary1;
    public static string str;
    public static string output;
    
  public static string getAnimalNameById(integer n){
   
      HTTP p = new HTTP();
      HTTPRequest req = new HTTPRequest();
      req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/?id='+n);
      req.setMethod('GET');
      
      HTTPResponse res = new HTTPResponse();
      res = p.send(req);
      
      result = res.getBody();
      
      system.debug('status code is:'+res.getStatusCode());
      system.debug('status is:'+res.getStatus()); 
      system.debug('result generated is:'+res.getBody());
      system.debug('---result---'+result);
      
      Map<String, object> m = (Map<String, object>)JSON.deserializeUntyped(result);
      system.debug('------deserializedoutput(m)-----------'+m);
      
      
      requiredstring = m.get('animals');
      system.debug('------requiredstring-------'+requiredstring);
      
      // here requiredstring is of type object i.e = (majestic badger, fluffy bunny, scary bear, chicken)
      // To convert object into string
      name = string.valueOf(requiredstring);
      
      // now splitting the string at ( and replacing ) with null
      strary = name.split('\\(');
     str =  strary[1].replace(')','');
      system.debug('------splitted strary-------'+strary);
     
     strary1 = str.split(',');
      output = strary1[n];
      system.debug('------output-------'+output);
      return output;
      
  }


 
Amit Chaudhary 8Amit Chaudhary 8
I Executed above code. It giving me below out put
String str = 'majestic badger, fluffy bunny, scary bear, chicken' ;
List<String> strList = str.split(',');
For(String s : strList )
{
 System.debug('---------->'+s);
}


 System.debug('---------->'+strList[0]);
 System.debug('---------->'+strList[1]);
Output
37.0 APEX_CODE,DEBUG
Execute Anonymous: 
Execute Anonymous: String str = 'majestic badger, fluffy bunny, scary bear, chicken' ;
Execute Anonymous: List<String> strList = str.split(',');
Execute Anonymous: For(String s : strList )
Execute Anonymous: {
Execute Anonymous:  System.debug('---------->'+s);
Execute Anonymous: }
Execute Anonymous: 
Execute Anonymous: 
Execute Anonymous:  System.debug('---------->'+strList[0]);
Execute Anonymous:  System.debug('---------->'+strList[1]);

16:00:10.11 (12001027)|EXECUTION_STARTED
16:00:10.11 (12007316)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:00:10.11 (12771841)|USER_DEBUG|[6]|DEBUG|---------->majestic badger
16:00:10.11 (12798537)|USER_DEBUG|[6]|DEBUG|----------> fluffy bunny
16:00:10.11 (12820075)|USER_DEBUG|[6]|DEBUG|----------> scary bear
16:00:10.11 (12847535)|USER_DEBUG|[6]|DEBUG|----------> chicken
16:00:10.11 (12878602)|USER_DEBUG|[10]|DEBUG|---------->majestic badger
16:00:10.11 (12903714)|USER_DEBUG|[11]|DEBUG|----------> fluffy bunny
16:00:10.11 (12964681)|CODE_UNIT_FINISHED|execute_anonymous_apex
16:00:10.11 (14292710)|EXECUTION_FINISHED

If your String is like below
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;

try below code.
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;
str=str.remove('(');
str=str.remove(')');

List<String> strList = str.split(',');
For(String s : strList )
{
 System.debug('---------->'+s);
}


 System.debug('---------->'+strList[0]);
 System.debug('---------->'+strList[1]);

Let us know if this will help you
 
This was selected as the best answer
Jennifer Dos Reis ICOJennifer Dos Reis ICO

Grate @ranveer singh 8, There are many ways to achieve it. You also could use the remove method as @Amit Chaudhary 8 or you could use the substringBetween(open, close) method of the String Class. 

Example

String s1 = '( magestic badger, fluffy bunny )';

String s2 = s1.substringBetween('(',')');


It removes the parenthesis of the string because it returns the substring that occurs between the two specified Strings
ranveer singh 8ranveer singh 8
Thank you amit and jennifer