aspose file tools
The moose likes Beginning Java and the fly likes Removing Padding of a string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Removing Padding of a string" Watch "Removing Padding of a string" New topic
Author

Removing Padding of a string

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 514
Is there an easy way to remove padding from a string.. I have a string with underscores on it... is there a command somewhere to remove this without having me to create a new method to remove it.
Example 223-222-322__
needs to be 223-222-322
Thanks..
Dale

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Dale
If the underscores are at the same place all the time you can use the StringBuffer delete() method.
it takes two arguments (both ints) the index to start at he index +1 to end at. If the strings are always the same length you can just use constant ints or use the length() method to determine the length and get rid of the two characters at the end. If they are not always at the end then I think you'll have to write your own method.
hth
Dave


Dave
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 514
ah.. okay.. No, the account numbers vary in length. Thanks for your quick reply
Dale
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Dale.
Whether or not you should write your own method depends on how sure you want to be that you retain all your data after the removal.
For example, at first blush, you could use
String raw = "223-222-322__";
String pure = raw.substring( 0, raw.indexOf( "_" ) );
but then you'll lose data if
String raw = "223-222-_322_";
because indexOf() retrieves the position of the first occurrence of the search String.
It looks to me like you may be reading in fields scraped from a 3270 terminal and trying to draw out the relevant data. Isn't it possible that some of the data you read may have underscores in them? I.e., "USERNAME art_metzer____"? The more I look at it, the more I think you should write your own method, Dale.
Here's how I would do it: I would start from the end of your raw String, look for the first non-underscore character, then use math and substring() to trim that much off the end of your original raw String to yield the purified String.
There may a better way to do this....I'll mull it over a little.
Good luck.
Art
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
I think that sounds good Art. A 'trim' class, maybe with specific methods trimLeft() and trimRight(). You could even overload it and use the same class to right or left adjust String text.
Just a thought...
------------------
I'm a soldier in the NetScape Wars...
Joel


Wait a minute, I'm trying to think of something clever to say...<p>Joel
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Removing Padding of a string
 
Similar Threads
leading zeros
NX: Update Record - padding problem
How To Make FIELDSETs Share Their Borders
first 3 digits of your phone number
How to remove fieldset margin in IE?