Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove character from string in Kotlin

I am trying to create an Android calculator which uses strings in Kotlin. I do not know how to delete a comma (or the negative) if it already contains one.

Here is my code which adds the comma correctly but doesn't delete it if the user clicks again:

if (!buClickValue.contains(".")) {
    buClickValue += "."
} else {
    buClickValue.replace(".", "")
}
like image 379
Marcus Mondel Avatar asked Mar 18 '18 03:03

Marcus Mondel


People also ask

How do I remove a character from a string in Kotlin?

To remove first N characters from a String in Kotlin, use String. drop() method.

How do I remove words from string in Kotlin?

You can remove part of the string by replacing it with empty string, but note that this will keep any surrounding characters including the surrounding spaces.

How do you remove the first character from string Kotlin?

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

How to remove last n characters from string in Kotlin?

How to Remove Last N Characters from String in Kotlin? To remove last N characters from a String in Kotlin, use String.dropLast () method. Given a string str1, and if we would like to remove last n characters from this string str1, call dropLast () method on string str1 and pass the integer n as argument to the method as shown below.

How to get the substring of a string in Kotlin?

In this tutorial, we will learn different ways to do that in Kotlin. One way to solve this is by getting the substrings before and after the give index. Put the new character in between them and it will give the new string. We will use string.substring method to get one substring :

How to remove last n characters from string in Java?

Given a string str1, and if we would like to remove last n characters from this string str1, call dropLast () method on string str1 and pass the integer n as argument to the method as shown below. dropLast () method returns a new string with the last n characters removed from the given string.

How to remove unwanted characters from a string in PHP?

toRegex () method is used to convert one string to a regular expression. replace method is used to replace all characters matched by that regex with space. filter is another way to remove unwanted characters from a string.


2 Answers

The replace() method is designed to return the value of the new String after replacing the characters. In your case, the value obtained after replacing the characters is never reassigned back to the original variable.

Specifically in your else clause, the line should be changed to -

buClickValue = buClickValue.replace(".", "") 
like image 76
Ameya Pandilwar Avatar answered Sep 21 '22 08:09

Ameya Pandilwar


The more logical technic is not to replace but to filter

buClickValue = buClickValue.filterNot { it == "."[0]) } 

Or to extend

filtered = ".,;:"  buClickValue = buClickValue.filterNot { filtered.indexOf(it) > -1 } 
like image 39
Luc-Olivier Avatar answered Sep 20 '22 08:09

Luc-Olivier