How To Split String Method In Salesforce Apex?

1.9K    Asked by AlGerman in Salesforce , Asked on Nov 18, 2022

Need to split a string by the first occurrence of a specific character like '-' in 'this-is-test-data '. So what I want is when I do a split it will return 'this' in zero indexes and rest in the first index.

It is used to break your apex string split. Showing an example below with a small case about it. Maybe this can help you.


Example:

Let suppose you are getting something 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

Your Answer

Interviews

Parent Categories