Splits a string around matches of the given delimiter character. : String Split « Data Type « Java Tutorial






import java.util.StringTokenizer;

/*

 Derby - Class org.apache.derby.iapi.util.PropertyUtil

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to you under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

public class Main {

  /**
   * Splits a string around matches of the given delimiter character.
   *
   * Where applicable, this method can be used as a substitute for
   * <code>String.split(String regex)</code>, which is not available
   * on a JSR169/Java ME platform.
   *
   * @param str the string to be split
   * @param delim the delimiter
   * @throws NullPointerException if str is null
   */
  static public String[] split(String str, char delim)
  {
      if (str == null) {
          throw new NullPointerException("str can't be null");
      }

      // Note the javadoc on StringTokenizer:
      //     StringTokenizer is a legacy class that is retained for
      //     compatibility reasons although its use is discouraged in
      //     new code.
      // In other words, if StringTokenizer is ever removed from the JDK,
      // we need to have a look at String.split() (or java.util.regex)
      // if it is supported on a JSR169/Java ME platform by then.
      StringTokenizer st = new StringTokenizer(str, String.valueOf(delim));
      int n = st.countTokens();
      String[] s = new String[n];
      for (int i = 0; i < n; i++) {
          s[i] = st.nextToken();
      }
      return s;
  }


}








2.32.String Split
2.32.1.Split string
2.32.2.Split a String
2.32.3.Using split() with a space can be a problem
2.32.4." ".split(" ") generates a NullPointerException
2.32.5.String.split() is based on regular expression
2.32.6.String split on multicharacter delimiter
2.32.7.Split by dot
2.32.8.Split up a string into multiple strings based on a delimiter
2.32.9.Splits a string around matches of the given delimiter character.
2.32.10.Splits the provided text into an array, separator string specified. Returns a maximum of max substrings.
2.32.11.Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators.