8

I'm trying to make a calculator in Android Studio with Kotlin and I trying to allow users to make numbers negative and positive, but I got only half of it, making number negative, but can't make it so it turns back to positive, for it I need to remove first char in string, someone knows how to do it?

var firstNumber: String = ...
if (firstNumber[0] == '-') {
   firstNumber.
}
1
  • 2
    If you’re making a calculator you should work with Ints, Longs, or BigDecimals, not Strings. Commented Jan 22, 2022 at 22:47

3 Answers 3

20

You can use drop() of String class to remove first char of string.

firstName = firstName.drop(1);
Sign up to request clarification or add additional context in comments.

Comments

7

You could use substring:

firstNumber = firstNumber.substring(1);

1 Comment

thanks bro it worked!
1

Use

trimStart(c:Char)

E.g. given


    var s1="0myString"   

and

    var s2="0000myString"

you will get same result for result in next two expressions

val  result = s1.trimStart('0')  //"myString" 

or for

val result = s2.trimStart('0')   //"myString" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.