Search code examples
javastringeclipse-juno

string.trim() not functional?


It may look a Stupid question, but It is giving wrong output !!

The trim() is supposed to delete the 'space' or 'new line' character at the beginning or end of the String, but it isn't working.

String str=str.getText().toString();
str.trim();
if(str.equalsIgnoreCase("STRING"))
    mytextview.setText("True");
else
    mytextview.setText("False");

If I'm Entering ---> " STRING " (With many spaces ), I am getting False as the Answer .... I'm using Eclipse Juno for my Androing Project, are there any exceptions ? Please Help.


Solution

  • String str=str.getText().toString();
    str.trim();
    

    should be:

    String str=str.getText().toString();
    str = str.trim();
    

    Strings are immutable in Java. An operation like trim() creates a new String.