Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse String - again 1

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

Thanks for the help on my last parsing question. I have a twist on this and not sure how to handle. Any help would be most appreciated.

I need to parse a string consisting of an account number and a unitID.
Below is the string I need to parse.


%20%20%20%20%20%20%20%20%20%20%20%201905872%28S2%29%5BSub%202%20Account%5D0


What needs to be extracted is:
A) 1905872
B) S2

There could be less occurances of %20 for each string. How would I extract these two items and assign to their own variables.

TIA,
Tim
 
It would be a lot easier to delimit your string using a pipe of something (replacing the %20 space marks), before your browser sent the HTTP string to your server application (using JavaScript or something).
 
OK... Any suggestions/ideas for JavaScript on how to do this?
 
using javascript, you want to assign the entire string to a variable.

var s = "%20%20%20%20%20%20";

2. then create all your variables with a prefix, like var.
so your variables will be called var1, var2, var3,
up to varN...(I hope you know how many variables you have)

3.

try {
var i = 1;
while (s.length() > 0){

smallStr = s.substr(0, s.indexOf("%20"));

document.getElementById("var" + i ).value = smallStr;

//get rid of the substr from s after you parse it
//to the variable
s= s.substr(smallStr.length + 1, sOut.length);
}
}
catch (Exception e){
//oppss.. running out of variables, so exit
}





~za~
You can't bring back a dead thread!
 
Hi All,

Try this URLDecoder.decode("string"); and then substring the result as you wanted.

Thanks,
venu


 
Hi All,

I am sorry, yes URLDecoder.decode(String) has been deprecated. But we can use
URLDecoder.decode(String,String).

But before that we need to make sure that the string is ecoded by x-
Then we can Parse a x- string using

URLDecoder.decode("%20%20%20%20%20%20%20%20%20%20%20%201905872%28S2%29%5BSub%202%20Account%5D0","UTF-8");

Cheers,
Venu
 
nice venu. you got a star!

~za~
You can't bring back a dead thread!
 
Thanks All for your replies. This is the way I did it. It may not be the most efficient but it works.

...try {

Vector allUnitID = Utils.parseStringToVector(rptObjs.getSelAccounts(), ",");
for(int i=0;i<allUnitID.size(); i++) {

String unitID = (String)allUnitID.elementAt(i);

String pattern = unitID.substring(0,3);
Integer lng = new Integer(unitID.length());

//Extracting &quot;%20&quot;s from unitID variable.
while(pattern.equals(&quot;%20&quot;)) {
String nUnitID = unitID.substring(3, lng.intValue());
Integer nlng = new Integer(nUnitID.length());
String nextPattern = nUnitID.substring(0,3);
pattern = nextPattern;
unitID = nUnitID;
lng = nlng;
}
//Parsing unitID(M, S1, S2) and account number from unitID.
StringTokenizer sTokenizer = new StringTokenizer(unitID, &quot;%&quot;);
accountNumber = sTokenizer.nextToken();

String tempUnitID = sTokenizer.nextToken();

Integer tempLng = new Integer(tempUnitID.length());

//Parsing M/S1/S2 out.
if(tempLng.intValue() == 4) {
//Sub1/Sub2 account.
newUnitID = tempUnitID.substring(2,4);
}else {
//Master account.
newUnitID = tempUnitID.substring(2,3);
}...


Thanks again All,

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top