function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Priyangbada Basak 9Priyangbada Basak 9 

need to eliminate all the 1's from a number

I have used LEFT operator to get the first 1 from a number and remove it, for eg:

String number = '1111123456789';
number.LEFT(1) == '1'
String newNum = number.SUBSTRING(1);

Now i need to eliminate all the '1' that are present from the starting, so my output number would come as 23456789 and it would remove all the 1's present from first,in the example String number.

 

Best Answer chosen by Priyangbada Basak 9
Priyangbada Basak 9Priyangbada Basak 9

Thanks Shruthi...it worked but i forgot to mention that if 1 is in between any number, that should be kept...as in
if String num1 = '11115671245510'
then after eliminating the 1's from the left, output should be = 5671245510.

So i used below code and it worked for me.

String num1 = '11113451678';
Integer i;
String newNum;
for (i=0; i<=num1.length(); i++){
    if(num1.LEFT(1) == '1'){
        newNum = num1.SUBSTRING(1);
       num1 = newNum; 
    }
}

 

All Answers

Shruthi ChandrasekaranShruthi Chandrasekaran
Use the remove method to remove all the 1's from the string and you can't have the reserve keyword 'number' as the variable name. Change the variable name to something else.
String newNum = number1.remove('1');
Priyangbada Basak 9Priyangbada Basak 9

Thanks Shruthi...it worked but i forgot to mention that if 1 is in between any number, that should be kept...as in
if String num1 = '11115671245510'
then after eliminating the 1's from the left, output should be = 5671245510.

So i used below code and it worked for me.

String num1 = '11113451678';
Integer i;
String newNum;
for (i=0; i<=num1.length(); i++){
    if(num1.LEFT(1) == '1'){
        newNum = num1.SUBSTRING(1);
       num1 = newNum; 
    }
}

 

This was selected as the best answer