Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default Taking a character off the end of a string.

    Hello. I really need a solution for removing an unwanted carriage return at the end of a string. I have used one method, but it takes too long!!!!

    I am loading text files and putting the content into a JTextArea. However, I found that when I put the content of the string into the JTextArea there was always an extra carriage return put in at the end!

    My solution was to make a new string, but leave out the last character! I used the following code:
    // (The string concerned is called newtext)

    String r="";
    char c;
    for (int i = 0; i < (newtext[0].length()-1); i ++) {
    r += newtext[0].charAt(i);
    }

    This works really well, until I import a text file of a certain size and then the program effectively "freezes" because this process takes so long (in fact a really long time!)

    I would be very grateful for alternative solutions to removing the last character in the string!

    Thank you so much for helping me.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    No offense, but your approach is terrible. What's happening behind the scenes is this:

    Java Code:
    String r="";
    char c;
    for (int i = 0; i < (newtext[0].length()-1); i ++) {
      [COLOR="Red"]r = new StringBuffer().append(r).append(newText[0].charAt(i)).toString();[/COLOR]
    }
    so no wonder it takes forever.

    One fix is to re-work your code like this:

    Java Code:
    StringBuilder sb = new StringBuilder();
    char c;
    for (int i = 0; i < (newtext[0].length()-1); i ++) {
      sb.append(newText[0].charAt(i));
    }
    String r = sb.toString();

    try this and you'll see DRAMATIC improvement in performance.


    a better still approach is this:

    Java Code:
    String r = newtext[0].substring(0, newtext[0].length()-1);
    and you're done.


    the ultimate way to take care of whitespace is this:

    Java Code:
    String r = newtext[0].trim();
    and you're done. trim() removes white space from both the beginning and the end of the string.



    cheers!

  3. #3
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    Thank you so much for your post. I am sure that you have delivered the solution.

    The trim() function is a good idea, however, I cannot rule out the possibility that someone might intentionally have a space at the start of a text file!!!

    I believe this then makes the best solution:
    String r = newtext[0].substring(0, newtext[0].length()-1);

    So, I assume that this one line will do it all for me!

    Thank you so much!

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    iluxa - thank you for posting your solution. It worked and I am very grateful to you for taking the time to give the answer!

    Out of interest - I commented in my previous post that trim() might not be suitable here. However, I was wondering is there a trimend function?

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    not that I know about... Not in standard Java libraries anyway. You can do the trick with regular expressions, but that will definitely not help the performance of your code.

  6. #6
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    iluxa - can I just check for clarity that when you write "that will not help the performance of my code" are you referring to the idea of trimEnd()?

Similar Threads

  1. Replies: 3
    Last Post: 03-03-2010, 01:23 AM
  2. Replies: 4
    Last Post: 12-31-2009, 12:05 PM
  3. Replies: 7
    Last Post: 10-13-2009, 12:16 AM
  4. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 05:04 AM
  5. Replies: 0
    Last Post: 02-07-2008, 09:57 AM

Posting Permissions

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