This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Problem

 
m brymer
Ranch Hand
Posts: 64
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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: 2399
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic