You need to sign in to do that
Don't have an account?
Learning Apex (OOP)
string method "split" - How does it work?
Could someone please explain how the .split method works in a String?
I am facing this line in a code:
But how does it do it? How does this .split method works= I do not understand this bit: .split('@').get(1);
When looking at Salesforce String methods here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm , and searching by .split, I find the following method/description:
so, even if I do not understand how could this method possibly return a "list", I sort of guess that it returns a substring that is terminated by a regular expression that, in my line of code would be '@' ? But, this does not make sense in my line of code as the substring in it that terminates with '@' would in fact be firstName.LastName (not salesforce.com).
Also, what is the .get(1) doing in this line of code?
Thank you very much.
I am facing this line in a code:
String domain = myContact.Email.split('@').get(1);I believe it is trying to capture the domain of an email address (and assign it to the variable domain), so if the email address is, for example: firstName.LastName@salesforce.com, the above line of code is probably capturing the domain 'salesforce.com' and assigns it to the variable.
But how does it do it? How does this .split method works= I do not understand this bit: .split('@').get(1);
When looking at Salesforce String methods here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm , and searching by .split, I find the following method/description:
so, even if I do not understand how could this method possibly return a "list", I sort of guess that it returns a substring that is terminated by a regular expression that, in my line of code would be '@' ? But, this does not make sense in my line of code as the substring in it that terminates with '@' would in fact be firstName.LastName (not salesforce.com).
Also, what is the .get(1) doing in this line of code?
Thank you very much.
Hi,
It is used to break your string. Showing an example below with a small case about it. May be this can help you.
Example:
Let suppose you are getting somethig like address(F-206, Daffodils, Magarpatta, Pune, India, 411028) in one text field and you want to store this address in different segments like house no, Building name, Street, City, Country. for this you can use split function for this by given code.
String addressFull = 'F-206, Daffodils, Magarpatta, Pune, India, 411028';
String[] address = addressFull.split(',');
String houseNumber = address[0];
String buildingName = address[1];
String streetName = address[2];
String cityName = address[3];
String countryName = address[4];
System.debug(houseNumber);
System.debug(buildingName);
System.debug(streetName);
System.debug(cityName);
System.debug(countryName);
Output: please check debug logs after that, you will be able to see
All Answers
.get(1) is simply returning the element in the list which is at index position 1.
.get(0) will return you a firstName.lastName.
But, how could .split retun an Array of values (or a List of values)? would the split need to factor-in various regular expressions?
Any example to better explain would be useful.
Thank you.
Thank you.
Could you possibly give me an example where .split would give me a List of ... say... 5 String items?
Thank you very much.
Hi,
Please try this piece of code for split hope it will help you.
Hope it will help you.
Regards,
Suraj
1. Slice the input with character provided. for e.g 'happy mind, happy life'. and you are splitting it with ','(comma).
2. internal output would be happy mind & happy life. Now think how Salesforce would return you this 2 strings as an output.
That is the reason every split method on most of the programming language return list, array of string as a return type of split.
Hi,
It is used to break your string. Showing an example below with a small case about it. May be this can help you.
Example:
Let suppose you are getting somethig like address(F-206, Daffodils, Magarpatta, Pune, India, 411028) in one text field and you want to store this address in different segments like house no, Building name, Street, City, Country. for this you can use split function for this by given code.
String addressFull = 'F-206, Daffodils, Magarpatta, Pune, India, 411028';
String[] address = addressFull.split(',');
String houseNumber = address[0];
String buildingName = address[1];
String streetName = address[2];
String cityName = address[3];
String countryName = address[4];
System.debug(houseNumber);
System.debug(buildingName);
System.debug(streetName);
System.debug(cityName);
System.debug(countryName);
Output: please check debug logs after that, you will be able to see
Please check once sample codes below :
Thanks
Varaprasad