Here you can find the source of tokenize(String source, char separator)
Parameter | Description |
---|---|
source | The string we will break into tokens |
separator | The separator character |
public static String[] tokenize(String source, char separator)
//package com.java2s; public class Main { /**/* w ww. ja v a2 s.c o m*/ Break up a string into tokens This method will take a source string and break it into substrings. The substrings are separated from each other by a single separator char in the source. If there are two adjacent separator charactors, or the first or last character in source is a separator, then a token of an empty string will result. @param source The string we will break into tokens @param separator The separator character @return an array of tokens, or null if source is null */ public static String[] tokenize(String source, char separator) { if (source == null) { return null; } // we'll allocate an overlarge array and then copy later int slen = source.length(); String[] scratch = new String[slen + 1]; // maximum possible size int ntokens = 0; int start = 0; while (true) { if (start >= slen) { // the last character must have been a separator // add an empty token scratch[ntokens++] = ""; break; } int sepx = source.indexOf(separator, start); if (sepx < 0) { // we've run off the end, no more separators scratch[ntokens++] = source.substring(start); break; } // the normal case scratch[ntokens++] = source.substring(start, sepx); start = sepx + 1; } // reallocate to the size we've actually got String[] result = new String[ntokens]; System.arraycopy(scratch, 0, result, 0, ntokens); return result; } }