File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes trim method help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

trim method help

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.
Consider these values "apple fruit, grapes fruit, onion veg , meat non-veg". Now how can i trim just the spaces or tabs before and after the comma. I need the spaces in between the values. I have tried using the split method and then trim the spaces to do this i have to split each and every value is there any other way.


Thanks and Regards
alexander.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Do you need an array, or just a string with the spaces removed?

Either way, the regular expression to use is ",\\s+" - a comma followed by one or more occurrences of whitespace. Splitting would be done as follows:
Replacing:


This thread is more suitable for the intermediate forum, moving there.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Let's continue here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: trim method help
 
Similar Threads
Counting words in a String
How to remove spaces
arrays
java trim function is not working when reading excel data
Setting JList Selections from String[]