Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Location
    India
    Posts
    4

    Question Unanswered: trim() function regardings..

    hello people,
    I have a doubt in trim() function. I executed the following codelet.

    String s="ram".trim();
    String k="ram ".trim();

    if(s==k)
    System.out.println("yes");
    else
    System.out.println("no");

    I got "no" output..

    if I use equals() function in if checking then I get "yes" output.Please let me know the things happen here.

  2. #2
    Join Date
    Mar 2004
    Posts
    16

    Re: trim() function regardings..

    Originally posted by Ramesh JB
    hello people,
    I have a doubt in trim() function. I executed the following codelet.

    String s="ram".trim();
    String k="ram ".trim();

    if(s==k)
    System.out.println("yes");
    else
    System.out.println("no");

    I got "no" output..

    if I use equals() function in if checking then I get "yes" output.Please let me know the things happen here.
    s and k are two different objects, hence it is normal that == operator returns false!!!
    Instead, using equalsmethod on String means that you want to check if the content of the strings is the same... "ram" and "ram ".trim() have the same value ("ram") even if they are stored in two different objects (so VM uses two different pointer)

  3. #3
    Join Date
    Mar 2004
    Location
    India
    Posts
    4

    Thumbs up Thanx

    Thank you danyver...for your answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •