Splits the provided text into an array, separator string specified. Returns a maximum of max substrings. : String Split « Data Type « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Data Type » String Split 
2. 32. 10. Splits the provided text into an array, separator string specified. Returns a maximum of max substrings.
import java.util.ArrayList;
import java.util.List;

public class Main {



  /**
   * Splits the provided text into an array, separator string specified.
   * Returns a maximum of <code>max</code> substrings.
   *
   * The separator(s) will not be included in the returned String array.
   * Adjacent separators are treated as one separator.
   *
   * A <code>null</code> input String returns <code>null</code>.
   * A <code>null</code> separator splits on whitespace.
   *
   * <pre>
   * StringUtils.splitByWholeSeparator(null, *, *)               = null
   * StringUtils.splitByWholeSeparator("", *, *)                 = []
   * StringUtils.splitByWholeSeparator("ab de fg", null, 0)      = ["ab", "de", "fg"]
   * StringUtils.splitByWholeSeparator("ab   de fg", null, 0)    = ["ab", "de", "fg"]
   * StringUtils.splitByWholeSeparator("ab:cd:ef", ":", 2)       = ["ab", "cd:ef"]
   * StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
   * StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
   * </pre>
   *
   @param str  the String to parse, may be null
   @param separator  String containing the String to be used as a delimiter,
   *  <code>null</code> splits on whitespace
   @param max  the maximum number of elements to include in the returned
   *  array. A zero or negative value implies no limit.
   @return an array of parsed Strings, <code>null</code> if null String was input
   */
  public static String[] splitByWholeSeparatorString str, String separator, int max ) {
      return splitByWholeSeparatorWorker(str, separator, max, false);
  }
  /**
   * Performs the logic for the <code>splitByWholeSeparatorPreserveAllTokens</code> methods.
   *
   @param str  the String to parse, may be <code>null</code>
   @param separator  String containing the String to be used as a delimiter,
   *  <code>null</code> splits on whitespace
   @param max  the maximum number of elements to include in the returned
   *  array. A zero or negative value implies no limit.
   @param preserveAllTokens if <code>true</code>, adjacent separators are
   * treated as empty token separators; if <code>false</code>, adjacent
   * separators are treated as one separator.
   @return an array of parsed Strings, <code>null</code> if null String input
   @since 2.4
   */
  private static String[] splitByWholeSeparatorWorker(String str, String separator, int max, 
                                                      boolean preserveAllTokens
  {
      if (str == null) {
          return null;
      }

      int len = str.length();

      if (len == 0) {
          return new String[0];
      }

      if ((separator == null|| ("".equals(separator))) {
          // Split on whitespace.
          return splitWorker(str, null, max, preserveAllTokens);
      }

      int separatorLength = separator.length();

      ArrayList substrings = new ArrayList();
      int numberOfSubstrings = 0;
      int beg = 0;
      int end 0;
      while (end < len) {
          end = str.indexOf(separator, beg);

          if (end > -1) {
              if (end > beg) {
                  numberOfSubstrings += 1;

                  if (numberOfSubstrings == max) {
                      end = len;
                      substrings.add(str.substring(beg));
                  else {
                      // The following is OK, because String.substring( beg, end ) excludes
                      // the character at the position 'end'.
                      substrings.add(str.substring(beg, end));

                      // Set the starting point for the next search.
                      // The following is equivalent to beg = end + (separatorLength - 1) + 1,
                      // which is the right calculation:
                      beg = end + separatorLength;
                  }
              else {
                  // We found a consecutive occurrence of the separator, so skip it.
                  if (preserveAllTokens) {
                      numberOfSubstrings += 1;
                      if (numberOfSubstrings == max) {
                          end = len;
                          substrings.add(str.substring(beg));
                      else {
                          substrings.add("");
                      }
                  }
                  beg = end + separatorLength;
              }
          else {
              // String.substring( beg ) goes from 'beg' to the end of the String.
              substrings.add(str.substring(beg));
              end = len;
          }
      }

      return (String[]) substrings.toArray(new String[substrings.size()]);
  }


  /**
   * Performs the logic for the <code>split</code> and 
   * <code>splitPreserveAllTokens</code> methods that return a maximum array 
   * length.
   *
   @param str  the String to parse, may be <code>null</code>
   @param separatorChars the separate character
   @param max  the maximum number of elements to include in the
   *  array. A zero or negative value implies no limit.
   @param preserveAllTokens if <code>true</code>, adjacent separators are
   * treated as empty token separators; if <code>false</code>, adjacent
   * separators are treated as one separator.
   @return an array of parsed Strings, <code>null</code> if null String input
   */
  private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
      // Performance tuned for 2.0 (JDK1.4)
      // Direct code is quicker than StringTokenizer.
      // Also, StringTokenizer uses isSpace() not isWhitespace()

      if (str == null) {
          return null;
      }
      int len = str.length();
      if (len == 0) {
          return new String[0];
      }
      List list = new ArrayList();
      int sizePlus1 = 1;
      int i = 0, start = 0;
      boolean match = false;
      boolean lastMatch = false;
      if (separatorChars == null) {
          // Null separator means use whitespace
          while (i < len) {
              if (Character.isWhitespace(str.charAt(i))) {
                  if (match || preserveAllTokens) {
                      lastMatch = true;
                      if (sizePlus1++ == max) {
                          i = len;
                          lastMatch = false;
                      }
                      list.add(str.substring(start, i));
                      match = false;
                  }
                  start = ++i;
                  continue;
              }
              lastMatch = false;
              match = true;
              i++;
          }
      else if (separatorChars.length() == 1) {
          // Optimise 1 character case
          char sep = separatorChars.charAt(0);
          while (i < len) {
              if (str.charAt(i== sep) {
                  if (match || preserveAllTokens) {
                      lastMatch = true;
                      if (sizePlus1++ == max) {
                          i = len;
                          lastMatch = false;
                      }
                      list.add(str.substring(start, i));
                      match = false;
                  }
                  start = ++i;
                  continue;
              }
              lastMatch = false;
              match = true;
              i++;
          }
      else {
          // standard case
          while (i < len) {
              if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                  if (match || preserveAllTokens) {
                      lastMatch = true;
                      if (sizePlus1++ == max) {
                          i = len;
                          lastMatch = false;
                      }
                      list.add(str.substring(start, i));
                      match = false;
                  }
                  start = ++i;
                  continue;
              }
              lastMatch = false;
              match = true;
              i++;
          }
      }
      if (match || (preserveAllTokens && lastMatch)) {
          list.add(str.substring(start, i));
      }
      return (String[]) list.toArray(new String[list.size()]);
  }

}
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.