Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Problem

 
m brymer
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using getParameter() to grab a hidden field on a jsp. My problem is that the string coming back is separated by commas including an extra one on the end (ex. value1,value2,value3,) this is playing havoc with my substring. It seems to think there is another value after the last comma and is bringing me back a blank. So I guess my question is what is the best way to get rid of/ignore this last comma? Any suggestions would be appreciated.
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
couldn't you just test for a null value at the end of the list?
 
m brymer
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I was already doing this with my if statement but it doesn't seem to be including it anyway.
 
Thejesh Nagarathna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use string tokenizer and use , as your seaparator.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about

if (string != null && string.endsWith(","))
string = string.substring(0,string.length()-1);
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think a reusable utility will be worth while over your lifetime, you might implement something from the REXX language: strip( string, whichend, what ). To strip trailing commas you could say s = Utility.strip(s, "T", ","). I used to use this to remove leading zeros all the time, but can't say I've used my Java implementation much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic