• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think split is your best option.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could investigate regular expressions.
 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic