Results 1 to 6 of 6
- 05-05-2010, 01:16 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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.
- 05-05-2010, 02:01 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
No offense, but your approach is terrible. What's happening behind the scenes is this:
so no wonder it takes forever.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] }
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:
and you're done.Java Code:String r = newtext[0].substring(0, newtext[0].length()-1);
the ultimate way to take care of whitespace is this:
and you're done. trim() removes white space from both the beginning and the end of the string.Java Code:String r = newtext[0].trim();
cheers!
- 05-05-2010, 02:25 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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!
- 05-05-2010, 02:43 AM #4
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
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?
- 05-05-2010, 02:52 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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.
- 05-05-2010, 02:56 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
problem with "|" character while splitting text or string.
By fmuddy in forum Advanced JavaReplies: 3Last Post: 03-03-2010, 12:23 AM -
How to generate 8 character long alphanumeric string
By Basit56 in forum New To JavaReplies: 4Last Post: 12-31-2009, 11:05 AM -
Help with creating a variable from a character in string
By Mayur in forum New To JavaReplies: 7Last Post: 10-12-2009, 11:16 PM -
how to get next character/string
By doha786 in forum New To JavaReplies: 3Last Post: 03-28-2009, 04:04 AM -
Inserting string with slash character into database
By Java Tip in forum Java TipReplies: 0Last Post: 02-07-2008, 08:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks