aspose file tools*
The moose likes Beginning Java and the fly likes getting a substring from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting a substring from a string" Watch "getting a substring from a string" New topic
Author

getting a substring from a string

Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Hello everyone,

Please help me out with this .

The below URL is a String

&cardId=11084430&batchId=10&promotionId=xxxx&promoterId=x&status=dormant&cardValue=xx.xx&key=DA7FEBA4F6F8A2E28FC773BE429FBBA1.grovepc.3

&cardId=11084430&batchId=1&promotionId=xxxx&promoterId=x&status=dormant&cardValue=xx.xx&key=DA7FEBA4F6F8A2E28FC773BE429FBBA1.grovepc.3

i am trying to get batchId=10 and batchId=1 from the above string

this is the code i used



Every time i get prizeDesc = prize description 1 printed,

why is it that it is not differenciating the batchId=1 and batchId=10. Please let me know

any alternatives please.

thanks
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
batchId=10 contains the string batchId=1
You could test for batchId=10 first

Or perhaps use a StringTokenizer class.
[ March 07, 2008: Message edited by: Bob Good ]
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Bob Good:
batchId=10 contains the string batchId=1
You could test for batchId=10 first

Or perhaps use a StringTokenizer class.

[ March 07, 2008: Message edited by: Bob Good ]



Thanks Bob,

will try that out
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

String.split should suffice:

This even allows you to create a Map out of all options, although you must keep in mind that a single key can occur multiple times in these kinds of request parameters.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Rob Prime:
String.split should suffice:

This even allows you to create a Map out of all options, although you must keep in mind that a single key can occur multiple times in these kinds of request parameters.


Thanks, Will try that out too


Thanks for the reply.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I would split the string by &
take that collection and split each by =
and put all those in a Map

then process to your heart content with much more readable code


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
You can also use a regular expression:

Whatever the batch ID number is, it becomes group 1 in the pattern, because it's inside the first (and only) pair of parentheses listed in the pattern.
[ March 07, 2008: Message edited by: Mike Simmons ]
 
Don't get me started about those stupid light bulbs.
 
subject: getting a substring from a string