Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008, 12:21 AM
Member
 
Join Date: Apr 2008
Posts: 4
raghu408 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-10-2008, 04:55 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 05:58 AM
Member
 
Join Date: Apr 2008
Posts: 4
raghu408 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 07:58 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 08:23 AM
Member
 
Join Date: Apr 2008
Posts: 4
raghu408 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 08:46 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 10:45 AM
Member
 
Join Date: Apr 2008
Posts: 2
game is on a distinguished road
Use String.split() method. It can take array of strings as arguments. StringTokenizer can/may be deprecated in the present/future releases.

regards,

game.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-10-2008, 11:45 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
game,
can you tell me the difference b/w StringTokenizer as well as split() method
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-10-2008, 12:13 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Quote:
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:

Code:
String myString = "a||b||c"; String[] split = myString.split("||"); System.out.println(split[1]+","+split[4]+","+split[7]);
Output:
Code:
a,b,c
__________________
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-10-2008, 12:15 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-10-2008, 12:18 PM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
Eranga,
Your 10th post is not clear. Can You Repeat it briefly
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-10-2008, 12:22 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-10-2008, 12:31 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by javarishi View Post
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.

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]); }
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-10-2008, 12:39 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-10-2008, 12:59 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-10-2008, 01:15 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
I didnt know you couldn't split with multiple characters like || actually. I thought the Array placement was quite strange..

How about this?

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-10-2008, 01:34 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-10-2008, 01:39 PM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
Hei Eranga,
What Is Wrong If I Use StringTokenizer?
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-10-2008, 01:42 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-10-2008, 02:33 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
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("\\|\\|");
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes