Here you can find the source of tokenize(String s, String delimiters)
Parameter | Description |
---|---|
s | String to be tokenized |
delimiters | Delimiter character(s) |
public static String[] tokenize(String s, String delimiters)
//package com.java2s; import java.util.*; public class Main { /**/* ww w .jav a 2s . com*/ * Tokenize given string for specified delimiter(s). * * @param s String to be tokenized * @param delimiters Delimiter character(s) * @return Array of token strings */ public static String[] tokenize(String s, String delimiters) { if (s == null) { return new String[] {}; } StringTokenizer tokenizer = new StringTokenizer(s, delimiters); String result[] = new String[tokenizer.countTokens()]; int index = 0; while (tokenizer.hasMoreTokens()) { result[index++] = tokenizer.nextToken(); } return result; } public static String[] tokenize(String s, String delimiters, boolean trimTokens, boolean allowEmptyTokens) { if (s == null) { return new String[] {}; } StringTokenizer tokenizer = new StringTokenizer(s, delimiters, true); List<String> tokenList = new LinkedList<String>(); boolean lastWasSeparator = false; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); boolean isSeparator = delimiters.indexOf(token) >= 0; if (trimTokens) { token = token.trim(); } if (isSeparator) { if (lastWasSeparator && allowEmptyTokens) { tokenList.add(""); } } else if (!"".equals(token) || allowEmptyTokens) { tokenList.add(token); } lastWasSeparator = isSeparator; } if (lastWasSeparator && allowEmptyTokens) { tokenList.add(""); } return tokenList.toArray(new String[tokenList.size()]); } }