Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a string?

In a program I'm working on, a textfield has to display some text at some point.

output.setText( outputString );
outputString = "";

output is a JTextField. These lines of code are in a method, and when it is called the first time, it works perfectly fine. However, when it is called another time, the original outputString text still remains. Why does this happen, and how can I fix it?

Okay, I think it happens because strings are immutable. The thing is, outputString never changes, so it still has the text from the initial method call.

How do I, somehow, change the text in the string?

like image 253
kullalok Avatar asked Jun 19 '12 13:06

kullalok


People also ask

What is erase in string?

std::string::erase in C++The function erases a part of the string content, shortening the length of the string.

What does Clear () do in C++?

The C++ function std::list::clear() destroys the list by removing all elements from the list and sets size of list to zero.


3 Answers

Setting the text to the contents of your variable does not set up a permanent relationship between that variable and that text field, if you want to clear the text, you could use

output.setText("");
like image 150
Theodore Murdock Avatar answered Oct 17 '22 21:10

Theodore Murdock


You have to clear the text from the JTextField object. Sending a variable as an argument with setText() does not tie that variable to the object. It simply copies the string.

output.setText("");

If the text is null or empty, has the effect of simply deleting the old text.

http://docs.oracle.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText%28java.lang.String%29

like image 26
Chris Dargis Avatar answered Oct 17 '22 21:10

Chris Dargis


I think you are misunderstanding something very important. Java Strings are immutable that means that you can't change them.

"How do I, somehow, change the text in the string?"

You can't. The text in the string cannot be changed. It is immutable. Any "solution" that involves changing the text in a String WON'T WORK in Java. (Got that?)

When you do this:

output.setText(outputString);
outputString = "";

the assignment does not change the value that is displayed in the text field. It just changes the String that the local variable outputString refers to.

And when you do this:

output.setText(""); 
output.setText(outputString);

it does not cause outputString to change. It just changes the displayed text to nothing and then immediately changes it to whatever outputString currently refers to.

If you want to change the value displayed in the text field to nothing, you JUST do this:

output.setText("");

Perhaps the other thing that you've got wrong in your thinking is that you think that this:

output.setText(outputString);

sets up a relationship between the text field output and the variable outputString ... such that when the user types into the field, the outputString variable is magically updated. That is NOT so. In fact, it CANNOT be so, because you cannot pass the address of variable.

In fact, output.setText(outputString); just passes the value of outputString to the text box object. If and when the user types something into the box, the characters are stored somewhere else, and only returned to your code ... as a new String ... when your code calls output.getText().

like image 22
Stephen C Avatar answered Oct 17 '22 22:10

Stephen C