Java String Tokenize getStringTokens(String str)

Here you can find the source of getStringTokens(String str)

Description

get String Tokens

License

Open Source License

Declaration

public static String[] getStringTokens(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  w  w  .j a v  a2  s  .  c  o m*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

public class Main {
    public static String[] getStringTokens(String str) {
        // No ESC, return API results
        if (str.indexOf("\\,") < 0) //$NON-NLS-1$
        {
            return str.split(","); //$NON-NLS-1$
        }

        ArrayList<String> list = new ArrayList<String>();
        char[] charArray = (str + ",").toCharArray(); //$NON-NLS-1$
        int startIndex = 0;
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c == ',') {
                if (charArray[i - 1] != '\\' && i > 0) {
                    list.add(str.substring(startIndex, i).replaceAll("\\\\,", ",") //$NON-NLS-1$ //$NON-NLS-2$
                            .trim());
                    startIndex = i + 1;
                }
            }
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

  1. getFileName(StringTokenizer s)
  2. getNameTokens(final String names)
  3. getParamFloat(StringTokenizer s)
  4. getParamString(StringTokenizer s)
  5. getSQLTokens(String query)
  6. getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)
  7. getTokens(final String s)
  8. getTokens(String msg, String delim)
  9. getTokens(String str, String delim)