Ceeya
Ceeya

Reputation: 65

Searching for a digit and delete it in my int variable

I want to search a digit in my int variable and delete it.

Here is little part of the code (not finished, because i'm still on the implementation). I noticed that there are many use cases. So do you know an easier way to delete the digit?

public int getStringtoIntForEthType(int OxAB){
        String myInt = Integer.toString(OxAB);
        if(Integer.toString(OxAB).contains("x")){
            myInt = myInt.substring(2);
        }
        StringBuilder myIntBuilder = new StringBuilder(myInt);

        for(int a = 0; a<=myInt.length();a++){
            if(a-1 < 0 && myIntBuilder.charAt(0)!=0 && myIntBuilder.charAt(a)==0){

            }
        }

        return Integer.parseInt(myIntBuilder.toString());
    }

Upvotes: 0

Views: 70

Answers (1)

user5342366
user5342366

Reputation:

To delete all existence of that Digit from the number here is a function :-

public int DeleteDigit(int number, int numberToDel)
{
    String Num = "" + number;
    Num = Num.replace(numberToDel + "", "");

    if(Num.length != 0)
        return Integer.parseInt(Num);
    return 0;
}

this would return an integer without the digit

Upvotes: 1

Related Questions