Doing minus operation on string

For convenience, Java supports string concatenation with the '+' sign. This is the one binary operator with a class type as an operand. See String concatenation operator in the Java Language Specification.

Java does not support an overload of the '-' operator between a String and a char.

Instead, you can remove a character from a string by adding the substrings before and after.


Strings don’t have any kind of character subtraction that corresponds to concatenation with the + operator. You need to take a substring from the start of the string to one before the end, instead; that’s the entire string except for the last character. So:

sentence = sentence.substring(0, sentence.length() - 1);

Tags:

Java

String