You need to sign in to do that
Don't have an account?

How to remove zeros in the string.
Hi guys,
my problem is there are two strings with some value i need to remove the zeros before the number and add both the strings .
Ex:-
String S=000578;
String j=080554321;
String k=s+j ( not addition);
k = s + j;
57880554321 = 578+ 80554321;
it means it shoul remove all the zeros before the number.
Some times s=0578;
j=00080554321;
then also the out put should be same as above "k"
Thanks
frank
can you explain me inn brief were u do this???
Use replaceFirst() on each string to strip the leading zeros, using the regular expression '^0+' which means, "one or more zeros at the start of the string".
k = s.replaceFirst( '^0+', '') + j.replaceFirst( '^0+', '');
Hi frank,
Try this code
String S=000578;
s= s.replace('0', '');
for more : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm
thanks
Anu
Hi ani123,
This actual used in lead object.
Where i had 2 fields country code and phone number.
so what i need if.they enter country code like 91.
And phone number like 999999999.
so it should return 91999999999 .
if they enter country code like 091.
and phone number like 091-999999999.
Then also it shoul return 91999999999 .
Thanks
Frank
Anu, you do realize that the string replace() method would remove ALL zeros from the string, right?
The OP wants to remove leading zeros from a string, which is different than removing all zeros.
Hi Frank,
Try something in formula field LIK THIS TO TRIM UR first 3 values OF country code from ur contact number
TRIM(LEFT( Field_1__c ,3))
Regards,
ANI
for example:
String.valueOf(integer.valueOf(strWithLeadingZeroes));
The regex searches for one or more zeroes at the start of the string and replaces them with an empty string.
Would this work then?
myStr = myStr .replaceAll( '\\s+', '').replaceFirst('^0+','')
Btw this one would not work as requested because if would remove all "0" found, not just the ones at the beggining
String S=000578;
s= s.replace('0', '');