|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-10-2008, 12:21 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 4
|
|
|
How to parse String effectively based on a dilimiter
If i have a string like :
update employee
set wtKey = name||pno||id
where id is not null
Then my API should return this when i pass "||"
name||pno||id
I tried doing it using ' ' as dilimiter and check to see if the parsed string has || but that won't work when the above update cmd is written in the following manner:
update employee
set wtKey = name || pno || id
where id is not null
(or)
update employee
set wtKey = name|| ' ' ||id
where id is not null
Is there any API or third party stuff that can perform such operation instead of we parsing it based on some assumptions.
Any help would be appreciated
|
|

04-10-2008, 04:55 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
|
What you want to extract, a string name||pno||id or the words in contain?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 05:58 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 4
|
|
|
Hi Eranga thanks for your response... My requirement here is to get the string name||pno||id : I am trying to parse a sql query to parse it for " name||pno||id " so that i can write some functionality to convert it to CONCAT(name,pno,id). As i said above i used space as delimiter and checking if the parsed string has " || " but thats not working in the case i mentioned above.
|
|

04-10-2008, 07:58 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
|
You can use the StringTokenizer class to do it. Since in your string contain = and || sign it's not difficult. But after finding tokens you have to rebuild the string. Did you get what I say.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 08:23 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 4
|
|
|
That would be an option if i just need to do this on this particular string but the way i want is like what ever sql you pass it should parse for such strings that has '||' so that i could convert them to SqlServer acceptable format .
This is a requirement because || is not allowed in sql server instead we need to use CONCAT
example a||b||c in oracle is equivalent to CONCAT(a,b,c) in SqlServer
|
|

04-10-2008, 08:46 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
Yaah Raghu Use String Tokenizer and make deffrent tokes of strings using delimeter as ||. After That Use Concatanation Operator to concat it, and use it.
|
|

04-10-2008, 10:45 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 2
|
|
|
Use String.split() method. It can take array of strings as arguments. StringTokenizer can/may be deprecated in the present/future releases.
regards,
game.
|
|

04-10-2008, 11:45 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
game,
can you tell me the difference b/w StringTokenizer as well as split() method
|
|

04-10-2008, 12:13 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
Use String.split() method. It can take array of strings as arguments. StringTokenizer can/may be deprecated in the present/future releases.
Yes game I agree. I didn't even think anyone used StringTokenizer anymore!
Here is a quick example of the Split method:
String myString = "a||b||c";
String[] split = myString.split("||");
System.out.println(split[1]+","+split[4]+","+split[7]);
Output:
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 12:15 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
|
I don't think it is easy. In split, argument must be a single delimiter. According to the string here talking about, it is '||'. But split required '|' only. But if you try, can do it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 12:18 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
Eranga,
Your 10th post is not clear. Can You Repeat it briefly
|
|

04-10-2008, 12:22 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
|
Don are sure that what you have done is right. Simply try to loop the array and see what happened.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 12:31 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Originally Posted by javarishi
Eranga,
Your 10th post is not clear. Can You Repeat it briefly
Sure, why not. Simply compile this code and run. Check the output, with initial strings.
String s1 = "update employee set wtKey = name || pno || id where id is not null";
String s2 = "update employee set wtKey = name # pno # id where id is not null";
String[] rs1 = s1.split("||");
for(int i = 0;i < rs1.length;i++){
System.out.println(rs1[i]);
}
String[] rs2 = s2.split("#");
for(int j = 0;j < rs2.length;j++){
System.out.println(rs2[j]);
}
You may comes-up with what I try to say.
Eranga
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 12:39 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
I don't think it is easy. In split, argument must be a single delimiter. According to the string here talking about, it is '||'. But split required '|' only. But if you try, can do it.
Yeah you are right. Looping through the arrays will give you problems..
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by DonCash : 04-10-2008 at 01:17 PM.
|
|

04-10-2008, 12:59 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Yep, even me initially explained all of them without exploring. But I remember that split can't use for multiple characters actually. I'm confusing by seen your code, that may be I have made a mistake.
Anyway, it's clear now 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 01:15 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
I didnt know you couldn't split with multiple characters like || actually. I thought the Array placement was quite strange..
How about this?
String myString = "a||b||c";
myString = myString.replace("||", ",");
String[] split = myString.split(",");
System.out.println(split[0]+split[1]+split[2]);
There wont be any problems with looping through the arrays then...
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by DonCash : 04-10-2008 at 01:17 PM.
|
|

04-10-2008, 01:34 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Yep, that's true and what I try to give as a hint is that to our original question poster. Still no response there. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 01:39 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
Hei Eranga,
What Is Wrong If I Use StringTokenizer?
|
|

04-10-2008, 01:42 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Nothing is wrong there. By look at the original string I can say, it's not easy. But it is really good way, rather working with arrays.
I hate ArrayOutOfBoundExceptions 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-10-2008, 02:33 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: State College, PA
Posts: 50
|
|
|
Your problem with using temp.split("||"); is simply that the argument is a regular expression not a string, char, or char array. You must give it the escape character which must be escaped itself. Use the following line to do it without replacing your || with something else:
temp.split("\\|\\|");
|
|
| Thread Tools |
|
|
| Display Modes |
|
| | |