Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trimming characters and/or blanks  RSS feed

 
Bill Raterink
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an easy (trans: one line) way to trim ONLY the leading or only the trailing blanks/chars from a string or stringbuffer? ...or am I forced to ust an array or 2 strings & "shift the bytes one by one? (I think) I can do this, but I thought I'd check to see if there was an "educational" moment handy. The trim & trimtosize functions don't seem to be what I'm looking for. What I think I need is an RTrim or LTrim command.
You saw that one comming, didn't you? Sorry, I'm still on the UP side of the curve.
Thanks for the help, & BTW that stringbuffer suggestion works great. -Bill.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably can use the replaceFirst() or replaceAll() methods if you are
using JDK 1.4.
From my understanding for left trim you can do:

and for the rigth trim you can do:

Hope those work
 
Bill Raterink
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wirianto,
Thank you so much for the quick response. I'll give that a try right now. I had used the replaceAll but couldn't get it to work correctly with the null string (""). I'm sure it had something to do with the way I was defining my chars/bytes. That was decades ago (in learning terms), actually about 3 months, just about one of my first javaranch posts.
Thanks again, -Bill.
 
Bill Raterink
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wirianto, It doesn't seem to like the replaceFirst/replaceALL methods. I't possible I'm on an older version of the JDK, but I don't have any choice, I MUST stay back in the dark ages for compatibility reasons.
Here's what I get now.
Also I didn't use the s+ as in your example, can you explain what it means?, Instead I had already defined a Quote, Bangs(!), & Blnks " ",
Thanks, -Bill.

C:\Dev\JavaProjects\EditFBmsg>javac FileEdit.java
FileEdit.java:384: Method replaceFirst(char, java.lang.String) not found in class java.lang.String.
cleanedInput = lineOfInput.replaceFirst(Bangs,"");
^
FileEdit.java:385: Method replaceAll(char, char) not found in class java.lang.String.
Comment1[Indx] = cleanedInput.replaceAll(Quote,Blank);
^
2 errors
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Raterink:
Wirianto, It doesn't seem to like the replaceFirst/replaceALL methods. I't possible I'm on an older version of the JDK, but I don't have any choice, I MUST stay back in the dark ages for compatibility reasons.
Here's what I get now.
Also I didn't use the s+ as in your example, can you explain what it means?, Instead I had already defined a Quote, Bangs(!), & Blnks " ",
Thanks, -Bill.

C:\Dev\JavaProjects\EditFBmsg>javac FileEdit.java
FileEdit.java:384: Method replaceFirst(char, java.lang.String) not found in class java.lang.String.
cleanedInput = lineOfInput.replaceFirst(Bangs,"");
^
FileEdit.java:385: Method replaceAll(char, char) not found in class java.lang.String.
Comment1[Indx] = cleanedInput.replaceAll(Quote,Blank);
^
2 errors


As shown on your exception, the reason it failed is because
replaceFirst and replaceAll expect 2 String parameters.
The first string is a regular expression(regex for short) which describe on what to replace, the second string are what do you want to replace it with.
In regex, "\s" means any whitespaces(tab, enter, cr, linefeed, etc)
and the "+" sign means "one or more occurance of the previous character".
So "\s+" means "one or more whitespaces", since in Java "\" is an escape
character, when you pass it in you have to pass in "\\s+".
In the right trim example the regex is "\\s+$", where the "$" sign means "at the end of the line", so the total translation is "find one or more whitespaces at the end of the line".
Check out the java.util.regex.* classes, especially Pattern class. Also there are tons of information on the web regarding regex, it is a commonly use pattern-matching system on computer.
 
Bill Raterink
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wirianto, Thanks again. I'll look up that regex. I did get the program working using the substring method. Since I want to drop the quotes around the record, I used the substring(1,length()-1) & it worked super. Also I needed to drop the quote around all 7 of the records, but only needed to drop the 2 Bang signs (!) from the first 3, hence my replacing them with spaces & then doing the trim. Problem was, I didn't want to trim the END. In fact I had to ensure a 60 byte length. Thanks again for the help. This has been a FANTASTIC site for me. I only hope I can repay the favor some day.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem at all.
This sure is a great site.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!