Results 1 to 6 of 6
  1. #1
    ksurya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Extract substrings from a string where substring position differs

    Str 1 :
    ^0609|1|2834.56150|07710.32664|17.170|330.60|!

    Str 2 :
    FC03 00140700 00007D04 0002E702 0001624F 00C02A9D

    Str 3 :
    ^id=PTTRK30ES861001000245502&POS=<3.1><1259.429640 ><07735.382010><084038><260312>@

    They are all having the same type of data(U1 L1 L2 T1 T2 S1)

    U1|L1|L2|T1|T2|S1|! in Str 1 ( delimiter | )

    S1 U1 L1 L2 T2 T1 ins Str 2 (delimiter space)

    ^id=U1&POS=<T1><L1><L2><S1><T2> ( delimiter >< )

    I can have more than these string formats .

    I want to parse strings and store them in a list and eventually store
    it in a table.

    The problem is strings have substrings which appear in different
    positions and have different delimiters . I have given examples of some of the strings.
    I need a common parser which will work with any string format and also what is the best way to store them.

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Extract substrings from a string where substring position differs

    Good luck with that.
    The idea that you could create something that will work with "any String format" is pie in the sky.
    Please do not ask for code as refusal often offends.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Extract substrings from a string where substring position differs

    If you can't define the rules, you can't parse the String. Simple as that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ksurya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Extract substrings from a string where substring position differs

    Quote Originally Posted by ksurya View Post
    Str 1 :
    ^0609|1|2834.56150|07710.32664|17.170|330.60|!

    Str 2 :
    FC03 00140700 00007D04 0002E702 0001624F 00C02A9D

    Str 3 :
    ^id=PTTRK30ES861001000245502&POS=<3.1><1259.429640 ><07735.382010><084038><260312>@

    They are all having the same type of data(U1 L1 L2 T1 T2 S1)

    U1|L1|L2|T1|T2|S1|! in Str 1 ( delimiter | )

    S1 U1 L1 L2 T2 T1 ins Str 2 (delimiter space)

    ^id=U1&POS=<T1><L1><L2><S1><T2> ( delimiter >< )

    I can have more than these string formats .

    I want to parse strings and store them in a list and eventually store
    it in a table.

    The problem is strings have substrings which appear in different
    positions and have different delimiters . I have given examples of some of the strings.
    I need a common parser which will work with any string format and also what is the best way to store them.

    Thanks

    Thanks. I will have different parsers for each format. After using split, what is the best data structure to hold the split data to be inserted into table?

  5. #5
    ksurya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Extract substrings from a string where substring position differs

    For a string ^0609|1|2834.56150|07710.32664|

    I can parse using split .

    Is it possible to have a predefined lsit or something which will hold

    U1 = 1
    L1 = 3
    L3 = 2
    L4 = 4

    and will populate it with the exact values grabbing the substring from position( 1, 3, 2, 4).

    U1 = 0609


    L1 = 2834.56150


    L3 = 1


    L4 = 07710.32664


    Any ideas on how to do this ?

  6. #6
    Vizoere is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Extract substrings from a string where substring position differs

    I'm not sure if I'm answering your question or not, but it sounds like a good use of Maps. I'm still learning, so I'm sure this could be done nicer, but a basic example implementation would be something like:

    Java Code:
    import java.util.*;
    
    public class SplitString {
        public static void main(String[] args) {
            HashMap<String, String> testMap = new HashMap<String, String>();
            String testString = "^0609|1|2834.56150|07710.32664|";
    
            populateMap(testMap, testString.split("[|]"));
    
            System.out.println("U1: " + testMap.get("U1"));
            System.out.println("L1: " + testMap.get("L1"));
            System.out.println("L3: " + testMap.get("L3"));
            System.out.println("L4: " + testMap.get("L4"));
        }
        private static void populateMap(HashMap<String, String> m, String[] s) {
            m.put("U1", s[0]);
            m.put("L3", s[1]);
            m.put("L1", s[2]);
            m.put("L4", s[3]);
        }
    }
    (Edit: Forgot to wrap code in code tags)

Similar Threads

  1. Split string and joining substrings
    By KarlNorway in forum New To Java
    Replies: 28
    Last Post: 01-27-2012, 10:51 PM
  2. Replies: 6
    Last Post: 12-06-2010, 09:26 AM
  3. Replies: 9
    Last Post: 08-31-2010, 05:21 AM
  4. Replies: 5
    Last Post: 06-22-2010, 09:34 AM
  5. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM

Posting Permissions

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