• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring  RSS feed

 
mike hunt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I get the string of all the characters before the -, in a string.
number of characters before the hyphen can vary, so I can't have a static number in the ().

ie. I need everything before the hyphen.

abc-1234 .substring(0,3) would work in this case, but the next time
there could be 4 characters before the hyphen. How to I recognize where the hyphen is to get -1 that?

thanks
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Java Ranch, Mike. You probably missed the JavaRanch Naming Policy on your way in, but we ask that displayed names are not obviously fake. You can change your name here. Thanks and enjoy your time at the ranch.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method, indexOf, that might help you.
 
mike hunt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it thanks.
 
Ramasubbhu Allur Kuppusamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

The best way to go about this problem is to use the StringTokenizer() Object to get the tokens seperated by a delimiter.
In your case, the delimiter happens to be - .
I use this object very often at work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!