Java String Split splitMacros(String s)

Here you can find the source of splitMacros(String s)

Description

This parses the given string with the following form.

License

Open Source License

Parameter

Parameter Description
s String to parse

Return

List of tokens

Declaration

public static List<String> splitMacros(String s) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//from   w w  w . j a v a  2  s . c  o m
     * This parses the given string with the following  form.
     * <pre>some text ${macro1} more text ... ${macro2} ... ${macroN} end text</pre>
     * It returns a list that flip-flops between the text and the macro:<pre>
     *  [some text, macro1, more text, ..., macro2, ..., macroN, end text]
     * </pre>
     *
     * @param s String to parse
     * @return List of tokens
     */
    public static List<String> splitMacros(String s) {
        List<String> tokens = new ArrayList<String>();
        int idx1 = s.indexOf("${");
        while (idx1 >= 0) {
            int idx2 = s.indexOf("}", idx1);
            if (idx2 < 0) {
                break;
            }
            tokens.add(s.substring(0, idx1));
            tokens.add(s.substring(idx1 + 2, idx2));
            s = s.substring(idx2 + 1);
            idx1 = s.indexOf("${");
        }
        if (s.length() > 0) {
            tokens.add(s);
        }
        return tokens;
    }
}

Related

  1. splitList(String list)
  2. splitList(String string)
  3. splitLists(String s)
  4. splitListToParts(final List list, final int times)
  5. splitLongUnicodeString(String message, List result)
  6. splitManagedBean(String el)
  7. splitMultiMessageString( String multiMessageString, String messageSplitter)
  8. splitName(String name)
  9. splitNames(final String string)