Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    raghu408 is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    raghu408 is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.


  5. #5
    raghu408 is offline Member
    Join Date
    Apr 2008
    Posts
    4
    Rep Power
    0

    Default

    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

  6. #6
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    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.

  7. #7
    game is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    Use String.split() method. It can take array of strings as arguments. StringTokenizer can/may be deprecated in the present/future releases.

    regards,

    game.

  8. #8
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    game,
    can you tell me the difference b/w StringTokenizer as well as split() method

  9. #9
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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:

    Java Code:
    String myString = "a||b||c";
    String[] split = myString.split("||");
    System.out.println(split[1]+","+split[4]+","+split[7]);
    Output:
    Java Code:
    a,b,c
    Did this post help you? Please me! :cool:

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  11. #11
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Eranga,
    Your 10th post is not clear. Can You Repeat it briefly

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Don are sure that what you have done is right. Simply try to loop the array and see what happened.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    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]);
            }
    You may comes-up with what I try to say.

    Eranga

  14. #14
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    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..
    Last edited by DonCash; 04-10-2008 at 12:17 PM.
    Did this post help you? Please me! :cool:

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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 :)

  16. #16
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    I didnt know you couldn't split with multiple characters like || actually. I thought the Array placement was quite strange..

    How about this?

    Java 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...
    Last edited by DonCash; 04-10-2008 at 12:17 PM.
    Did this post help you? Please me! :cool:

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, that's true and what I try to give as a hint is that to our original question poster. :) Still no response there. :)

  18. #18
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei Eranga,
    What Is Wrong If I Use StringTokenizer?

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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 :)

  20. #20
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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("\\|\\|");

Page 1 of 2 12 LastLast

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. Replies: 4
    Last Post: 02-22-2009, 04:31 PM
  3. how to Parse int to a string variable
    By raj reddy in forum Java Servlet
    Replies: 10
    Last Post: 01-09-2009, 07:41 PM
  4. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 06:32 AM
  5. Replies: 1
    Last Post: 07-23-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •