aspose file tools*
The moose likes Java in General and the fly likes how do you strip out certain groups of characters from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how do you strip out certain groups of characters from a String" Watch "how do you strip out certain groups of characters from a String" New topic
Author

how do you strip out certain groups of characters from a String

Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
for exapmle...

String date = "11-Feb-2005";
String day;
String month;
String year;

how would you strip out '11' from date to assign it to 'day', and 'Feb' to assign it to 'month' and '2005' to assign it to 'year'.

in my program the variable 'date' will always be in the format of:
ist two digits are numbers followed by '-'
then three digits (letters) followed by '-'
then four digits that are numbers.

i think it has something got to do with charAt or something, im not sure how to do it.

any ideas?


Whats in a name?
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
You're probably going to want to use SimpleDateFormat.

Create a SimpleDateFormatObject with the format string in which you are interested. Use that object to parse the string into a Date. Once you have the date object you can use its methods to extract day, month, etc.


Fun programming etcetera!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Parsing strings is fun and keeps you off the streets at night.

Since you have reliable delimiters you could use String.split (JDK1.4) or StringTokenizer or Scanner (JDK5).

Since you have reliable field lengths, you could use String.substring().

Since you have a date you can make DateFormat do all the work as suggested above, but then you have to get back the strings and maybe worry about leading zeros and such.

For recreation and education you might try all of those and see which you like best. Feel free to post some code if you try something that won't quite work. Let us know how it goes!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I think split is your best option.


Grails Consultant
http://dangertree.net
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Or you could investigate regular expressions.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
yea the split thing worked and so did the StringTokenizer. im using both now. its nice to learn new things like that.

these dates is driving me insaine,
they input into oracle in one format, when you select them from oracle its a different format, java dates is another format, then i have to change them to another different java format to compare dates, then change them all back. jesus.

i have another question in here regarding the gregorianCalenger (in a new post).
maybe you can help me out with that.


thanks for your help
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I wrote a helper class to help me out with date formats and put it in my utilities project. It's been helpful to have all date/string transformations in one place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do you strip out certain groups of characters from a String