You need to sign in to do that
Don't have an account?
ranveer 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?
index0 ,majestic badger should be output
index1,fluffy bunny should be output
index2, scary bear .....like this how i can this?
If your String is like below
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;
try below code.
Let us know if this will help you
All Answers
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
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
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
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;
}
}
If your String is like below
String str = '(majestic badger, fluffy bunny, scary bear, chicken)' ;
try below code.
Let us know if this will help you
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
It removes the parenthesis of the string because it returns the substring that occurs between the two specified Strings