File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Problem" Watch "String Problem" New topic

String Problem

m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
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

Joined: Oct 27, 2004
Posts: 158
couldn't you just test for a null value at the end of the list?
m brymer
Ranch Hand

Joined: Jan 04, 2005
Posts: 64
I thought I was already doing this with my if statement but it doesn't seem to be including it anyway.
Thejesh Nagarathna

Joined: Feb 10, 2005
Posts: 3
You can use string tokenizer and use , as your seaparator.

My tutorials:<br /> <a href="" target="_blank" rel="nofollow"></a>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
What about

if (string != null && string.endsWith(","))
string = string.substring(0,string.length()-1);
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

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
I agree. Here's the link:
subject: String Problem
It's not a secret anymore!