Results 1 to 20 of 27
- 04-09-2008, 11:21 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 4
- Rep Power
- 0
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, 03:55 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you want to extract, a string name||pno||id or the words in contain?
- 04-10-2008, 04:58 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 4
- Rep Power
- 0
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, 06:58 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-10-2008, 07:23 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 4
- Rep Power
- 0
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, 07:46 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
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, 09:45 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
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, 10:45 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
game,
can you tell me the difference b/w StringTokenizer as well as split() method
- 04-10-2008, 11:13 AM #9
Yes game I agree. I didn't even think anyone used StringTokenizer anymore!Use String.split() method. It can take array of strings as arguments. StringTokenizer can/may be deprecated in the present/future releases.
Here is a quick example of the Split method:
Output:Java Code:String myString = "a||b||c"; String[] split = myString.split("||"); System.out.println(split[1]+","+split[4]+","+split[7]);
Java Code:a,b,c
Did this post help you? Please
me! :cool:
- 04-10-2008, 11:15 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-10-2008, 11:18 AM #11
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
Eranga,
Your 10th post is not clear. Can You Repeat it briefly
- 04-10-2008, 11:22 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Don are sure that what you have done is right. Simply try to loop the array and see what happened.
- 04-10-2008, 11:31 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Sure, why not. Simply compile this code and run. Check the output, with initial strings.
You may comes-up with what I try to say.Java Code: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]); }
Eranga
- 04-10-2008, 11:39 AM #14
Yeah you are right. Looping through the arrays will give you problems..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.Last edited by DonCash; 04-10-2008 at 12:17 PM.
Did this post help you? Please
me! :cool:
- 04-10-2008, 11:59 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 :)
- 04-10-2008, 12:15 PM #16
I didnt know you couldn't split with multiple characters like || actually. I thought the Array placement was quite strange..
How about this?
There wont be any problems with looping through the arrays then...Java Code:String myString = "a||b||c"; myString = myString.replace("||", ","); String[] split = myString.split(","); System.out.println(split[0]+split[1]+split[2]);Last edited by DonCash; 04-10-2008 at 12:17 PM.
Did this post help you? Please
me! :cool:
- 04-10-2008, 12:34 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yep, that's true and what I try to give as a hint is that to our original question poster. :) Still no response there. :)
- 04-10-2008, 12:39 PM #18
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
Hei Eranga,
What Is Wrong If I Use StringTokenizer?
- 04-10-2008, 12:42 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 :)
- 04-10-2008, 01:33 PM #20
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
Parse XMLfile as String using SAX parser
By Jack in forum XMLReplies: 4Last Post: 02-22-2009, 04:31 PM -
how to Parse int to a string variable
By raj reddy in forum Java ServletReplies: 10Last Post: 01-09-2009, 07:41 PM -
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 06:32 AM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-23-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks