You need to sign in to do that
Don't have an account?
Ashishyadav
split text in salesforce
How can we split street field into two different fields via Apex coding.
I know there is a way to do it through split method dividing using delimiters ex:
String s1 = 'Hello Max wrwer 345345';
//String s2 = s1.left(3);
List<String> s3 = s1.split(' ',2);
//List<String> s3 = s1.split('\n');
System.debug('s2'+s3);
System.debug('s2'+s3[0]);
System.debug('s2'+s3[1]);
But How can we split on the basis of Number of characters for example Before 8th character it should store in field1 and after 8th to 30th character in field 2 and so on ....
Could you please help me here
I know there is a way to do it through split method dividing using delimiters ex:
String s1 = 'Hello Max wrwer 345345';
//String s2 = s1.left(3);
List<String> s3 = s1.split(' ',2);
//List<String> s3 = s1.split('\n');
System.debug('s2'+s3);
System.debug('s2'+s3[0]);
System.debug('s2'+s3[1]);
But How can we split on the basis of Number of characters for example Before 8th character it should store in field1 and after 8th to 30th character in field 2 and so on ....
Could you please help me here
You can use substring function to split based on number of charaters.
Example:
'hamburger'.substring(4, 8); // Returns "urge" 'smiles'.substring(1, 5); // Returns "mile"
You can also use left(integer) and right(integer) , mid(startIndex, length) method of string class to achieve your output.
See this.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm
Please see all String method in below link
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm
For example, for String s = 'boo:and:foo':s.split(':', 2) results in {'boo', 'and:foo'}
s.split(':', 5) results in {'boo', 'and', 'foo'}
s.split(':', -2) results in {'boo', 'and', 'foo'}
s.split('o', 5) results in {'b', '', ':and:f', '', ''}
s.split('o', -2) results in {'b', '', ':and:f', '', ''}
s.split('o', 0) results in {'b', '', ':and:f'}
Please let us know if this will help you
Thanks @Amit, I have already referred that , it does not help much when number of characters comes into act.
@Dushyant Sonwar : I havent tried that yet,
@Jayant Jadhav: Thanks es , that what I was trying earlier , but there is one condition if length of string is 100 charachers and Iam doing something like this
address1 =spliting[1].substring(0,70);
address2 =spliting[1].substring(71,150);
It will throw an error as number of characters is 100 only : System.StringException: Ending position out of bounds: 150
Ok, Then we can use if statement to avoid this exception. Try below code.
Let me know if this works for you.