Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By shall

Thread: String trim() member function not working

  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default String trim() member function not working

    I ran across an issue with the trim function not working in my main code, so I made a little test program and got the same results. Why isn't the trim function removing the first white space (byte = 32).

    Java Code:
            myString = " defined(OPENSSL_SYS_VMS)";
            
            myString.trim();
            
            byte[] bytesFromString = myString.getBytes("UTF-8");
    
            for (byte byteFromString: bytesFromString)
            {
                System.out.println(byteFromString);
            }
    Output:
    32
    100
    101
    102
    105
    110
    101
    100
    40
    79
    80
    69
    78
    83
    83
    76
    95
    83
    89
    83
    95
    86
    77
    83
    41

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: String trim() member function not working

    I found the problem.

    trim() doesn't change the original string. I should have assigned the result of myString.trim() to a new string.
    sunde887 likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: String trim() member function not working

    First of all, the trim() method works. Just saying, but it does. "Unexpected output from trim()" might better have expressed the problem.

    And led us to examine the trim() API docs to see what the devil we should expect trim() to do. "Returns a copy of the string, with leading and trailing whitespace omitted".

    The important bit is "a copy of". In your code you obtain the byte array from myString - the original string - not from the copy returned by trim().

    [Edit] too slow! :( I'm glad you've you got it figured out. This is behaviour consistent with the other String methods, and methods of so-called (and much favoured) immutable classes.
    Last edited by pbrockway2; 04-29-2012 at 08:31 AM.

Similar Threads

  1. The function append() not working.
    By gautham0209 in forum AWT / Swing
    Replies: 4
    Last Post: 11-20-2011, 08:32 AM
  2. Trim() doesn't trim()
    By trl in forum New To Java
    Replies: 4
    Last Post: 08-29-2011, 08:55 AM
  3. Trim for null string
    By laosu in forum New To Java
    Replies: 3
    Last Post: 02-08-2011, 07:56 AM
  4. String.trim() method help.....
    By arson09 in forum New To Java
    Replies: 6
    Last Post: 04-23-2010, 02:19 AM
  5. String trim
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:35 PM

Posting Permissions

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