+ Start a Discussion
SFDC 2017SFDC 2017 

How to use mid() in Apex class?

Hi All,
I have one requirement that i need to use mid() String function in Apex class.I have a set called zip and in that i am adding all the Account.Billingpostal code .for this i need to fetch only first 5 to return.I used like zip.add(String.mid(Account.BillingPostalCode, 1, 5));
But error i scoming as variable string doesnot exist.How to do this mid() and add it to the set called Zip with all account Postal code.

​Kindly help me on this  
 
Anil kumar GorantalaAnil kumar Gorantala
try this way
String s = acc.BillingPostalCode;
String s2 = s.mid(1, 5);
zip.add(s2);
Rupal KumarRupal Kumar
mid(startIndex, length)
Returns a new String that begins with the character at the specified zero-based startIndex with the number of characters specified by length.
This method is similar to the substring(startIndex) and substring(startIndex, endIndex) methods, except that the second argument is the number of characters to return.
Example-
String s = 'abcde';
String s2 = s.mid(2, 3);
​System.assertEquals( 'cde', s2);

Thanks
Rupal Kumar
http://mirketa.com