Results 1 to 6 of 6

Thread: Split a string using separators

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Split a string using separators

    Hello,
    I am completely confused in designing with the String. I want to split a string using spaces as separators, but ignoring the portions of text between quotation marks (" ").
    For example:
    Code:
    String myString "aaaa bbbb \" cc cc \ "dddd"
    myString.split (MonExp)
    the chain of origin
    Code:
    aaaa bbbb "cc cc" dddd
    the expected decomposition
    Code:
    yyyy
    bbbb
    "cc cc"
    dddd
    Can you help me with this. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Split a string using separators

    Hello,
    I have a code with me , just check it out. If it works for your then it will great
    Code:
    Public class RegexSpaceSplit {
        Public static void hand(String[] args) {
            ArrayList <String> test = new ArrayList <String>();
     
            / / Test cases 0
            test = Cutting("  \ t \ n \ t");
            System.out.System.out.println("Case 0");
            for (String substring: test) {
                System.out.System.out.println(substring);
            }
     
            / / Test case 1
            test = Cutting("   \" aaaa bbbb\" \" cc cc\" dd \"\"ee\ n\ tff \"gg\"\"hh\" ii ");
            System.out.System.out.println("\ nCase 1 ");
            for (String substring: test) {
                System.out.System.out.println(substring);
            }
     
            / / Test case 2
            test = Cutting("aa bb bb \"cc cc\" dd\ tdd \"eee\" f g \"hh\" \"ii\"\"dd\" kk ");
            System.out.System.out.println("\ nCase 2 ");
            for (String substring: test) {
                System.out.System.out.println(substring);
            }
        }
     
        Public static ArrayList <String> Cutting(String arr2) {
            String regtest = "([\"]{1}[\\S\\s &&[^(\")]]*[\"]{1})";
            ArrayList <String> arr1s = getChainesSansGuillemets(
                    arr2, regtest);
            ArrayList <String> chainesAvecGuillemets = getChainesAvecGuillemets(
                    arr2, regtest);
            ArrayList <String> chde = new ArrayList <String>();
     
            / / Case 0
            / / The string is empty
            if (arr2.trim().length() == 0)
                return chde;
    
            / / Case 1
            / / The first string contains quotation marks
            else if (arr2.trim().charAt(0) == '\"') {
                arr1s.remove(0);
                while ((chainesAvecGuillemets.size() > 0)
                        && (arr1s.size() > 0)) {
                    chde.add(chainesAvecGuillemets.remove(0));
                    for (String s: splitSpace(arr1s.remove(0)))
                        chde.add(s);
                }
            }
     
       
            ArrayList <String> chre = new ArrayList <String>();
            while (match.find())
                for (int i = 0; I <match.GroupCount(); I + +)
                    chre.add(match.group(i));
            return chre;
        }

  3. #3
    Join Date
    Dec 2009
    Posts
    213

    Re: Split a string using separators

    Hello,
    From my side I imagined an alternative which is a little "hack" but give the shortest code that I am thinking:
    1 - I search with regular expression portions of code between quotes
    2 - I replaced by a string of unique and unlikely but that does not contain spaces
    3 - I did my split ( "") on the spaces
    4 - I re-replace my chains by their initial values.
    In the example, give her, Initial string:
    Code:
    aaaa bbbb "cc cc" dddd "ee" ffff
    I prepared
    Code:
    aaaa bbbb unlikely $ _1 $ dddd $ $ _2 unlikely ffff
    unlikely with $ _1 $ <=> "cc cc" and unlikely to $ _2 $ <=> "ee"

  4. #4
    Join Date
    Dec 2009
    Posts
    211

    Re: Split a string using separators

    Hi,
    I went through your code and I have come to some results
    After Split ( ""), here is the result
    Code:
    yyyy
    bbbb
    unlikely _1 $ $
    dddd
    $ $ _2 unlikely
    ffff
    The Chain final here
    Code:
    yyyy
    bbbb
    "cc cc"
    dddd
    "ee"
    ffff
    So, I do not think this is going to work with you. I will recommend you to use something different.

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Split a string using separators

    Hello,
    I have found another simpler solution for this. I walk my channel space by space and I built my result word by word. When I fall on the quote character, I go directly to the next quote. Good with our three solution more or less "hack" I suggest that the problem is "solved", but if someone with a "beautiful" solution with the regular expression perfect who is all interstate once, I'm interested anyway!

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Split a string using separators

    Hello,
    Not bad idea, but if special attention as improbable is not impossible!
    Knowing that you have the regular expression to detect parts in quotes, in this way
    Code:
        ([\ "] (1) [\ \ S \ \ s &&[^( \ ")]]*[ \"] (1))
    And even a method that allows you to retrieve a list of phrases in quotation marks, you have all the cards to develop your idea. And finally best of luck with your code.

Similar Threads

  1. Tokens used to split string
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 04:55 AM
  2. How to use Menu Separators in Java?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 11-02-2010, 04:07 AM
  3. How to split the string in c++?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 09:29 PM
  4. Split a string with "\" in javascript
    By Logan 2 in forum Software Development
    Replies: 6
    Last Post: 22-01-2010, 10:42 AM
  5. How to use StringTokenizer to split a string in java?
    By Baazigar in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 09:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,669,613.26148 seconds with 16 queries