+ Start a Discussion
Learning Apex (OOP)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:
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:

User-added image
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.
Best Answer chosen by Learning Apex (OOP)
ravindra singh 17ravindra singh 17

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

Darshan R ChhajedDarshan R Chhajed
Split method return list of strings which got split by character mentioned in the method. 
.get(1) is simply returning the element in the list which is at index position 1.
.get(0) will return you a firstName.lastName.

 
Learning Apex (OOP)Learning Apex (OOP)
I am now guessing that the method .split retuns an Array? (hence the method .get(1) retuning the first position of such array?)

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.
Learning Apex (OOP)Learning Apex (OOP)
Hey Dashan,
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.
Suraj TripathiSuraj Tripathi

Hi,

Please try this piece of code for split hope it will help you.

String str = 'firstName.LastName@salesforce.com';
String[] str2 = str.split('@');
System.debug(str2[0]); // firstName.LastName
System.debug(str2[1]); // salesforce.com'

Hope it will help you.

Regards,

Suraj

Darshan R ChhajedDarshan R Chhajed
This is how split methods work. -
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.
 
ravindra singh 17ravindra singh 17

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 

 

 

This was selected as the best answer
v varaprasadv varaprasad
Hi ,

Please check once sample codes below :
===Example for adding multiple strings after whitespace ======

String s1='java string split method by javatpoint';  
String[] words=s1.split('\\s');//splits the string based on whitespace  
//using java foreach loop to print elements of string array  
for(String w:words){  
System.debug('All string in line : '+w);  
}  

===Example for getting based on index ======
string email = 'varaprasad@salesforce.com';
String uname = email.split('@').get(0);
String domain = email.split('@').get(1);
System.debug('==uname=='+uname);
System.debug('==domain=='+domain);

===Example for list of string =====
string domains = 'varaprasad@salesforce.com@gmail.com';
String[] mulDomains = domains.split('@');
System.debug('==mulDomains=='+mulDomains);


Thanks
Varaprasad