Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing minus operation on string

Tags:

java

string

I have a small problem with the minus operation in java. When the user press the 'backspace' key, I want the char the user typed, to be taken away from the word which exists. e.g word = myname and after one backspace word = mynam

This is kinda of what I have:

String sentence = "";
char c = evt.getKeyChar();
if(c == '\b') {
  sentence = sentence - c;
} else {
  sentence = sentence + c;
}

The add operation works. So if I add a letter, it adds to the existing word. However, the minus isn't working. Am I missing something here? Or doing it completely wrong?

like image 964
user1005253 Avatar asked Nov 30 '11 18:11

user1005253


People also ask

Can I subtract a string in Python?

The Python string doesn't have a subtraction operator.

Can we subtract string in C++?

Save this question. Show activity on this post.


1 Answers

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);
like image 97
Ry- Avatar answered Sep 20 '22 14:09

Ry-