Android String Trim splitAndTrim(String str, String delims)

Here you can find the source of splitAndTrim(String str, String delims)

Description

split And Trim

License

Apache License

Parameter

Parameter Description
str the string to split. Must not be null.
delims the delimiter characters. Each character in the string is individually treated as a delimiter.

Return

an array of tokens. Will not return null. Leading/trailing whitespace is removed from the tokens.

Declaration

@Deprecated
public static String[] splitAndTrim(String str, String delims) 

Method Source Code

//package com.java2s;
/**//ww w  . j  av a 2  s  .com
 * Copyright (c) 2000, Google Inc.
 *
 * Licensed 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.
 */

import java.util.StringTokenizer;

public class Main {
    /**
     * @param str the string to split.  Must not be null.
     * @param delims the delimiter characters. Each character in the
     *        string is individually treated as a delimiter.
     * @return an array of tokens. Will not return null. Leading/trailing
     *        whitespace is removed from the tokens.
     * @deprecated see the detailed instructions under
     *     {@link #split(String, String, boolean)}
     */
    @Deprecated
    public static String[] splitAndTrim(String str, String delims) {
        return split(str, delims, true);
    }

    /**
     * @param str the string to split.  Must not be null.
     * @param delims the delimiter characters. Each character in the
     *        string is individually treated as a delimiter.
     * @return an array of tokens. Will not return null. Individual tokens
     *        do not have leading/trailing whitespace removed.
     * @deprecated see the detailed instructions under
     *     {@link #split(String, String, boolean)}
     */
    @Deprecated
    public static String[] split(String str, String delims) {
        return split(str, delims, false);
    }

    /**
     * This method is deprecated because it is too inflexible, providing
     * only a very specific set of behaviors that almost never matches exactly
     * what you intend. Prefer using a {@link Splitter}, which is more flexible
     * and consistent in the way it handles trimming and empty tokens.
     *
     * <ul>
     * <li>Create a {@link Splitter} using {@link Splitter#on(CharMatcher)} such
     *     as {@code Splitter.on(CharMatcher.anyOf(delims))}.
     * <li><i>If</i> you need whitespace trimmed from the ends of each segment,
     *     adding {@code .trimResults()} to your splitter definition should work
     *     in most cases. To match the exact behavior of this method, use
     *     {@code .trimResults(CharMatcher.inRange('\0', ' '))}.
     * <li>This method silently ignores empty tokens in the input, but allows
     *     empty tokens to appear in the output if {@code trimTokens} is
     *     {@code true}. Adding {@code .omitEmptyStrings()} to your splitter
     *     definition will filter empty tokens out but will do so <i>after</i>
     *     having performed trimming. If you absolutely require this method's
     *     behavior in this respect, Splitter is not able to match it.
     * <li>If you need the result as an array, use {@link
     *     com.google.common.collect.Iterables#toArray(Iterable, Class)} on the
     *     {@code Iterable<String>} returned by {@link Splitter#split}.
     * </ul>
     *
     * @param str the string to split.  Must not be null.
     * @param delims the delimiter characters. Each character in the string
     *        is individually treated as a delimiter.
     * @param trimTokens if true, leading/trailing whitespace is removed
     *        from the tokens
     * @return an array of tokens. Will not return null.
     * @deprecated
     */
    @Deprecated
    public static String[] split(String str, String delims,
            boolean trimTokens) {
        StringTokenizer tokenizer = new StringTokenizer(str, delims);
        int n = tokenizer.countTokens();
        String[] list = new String[n];
        for (int i = 0; i < n; i++) {
            if (trimTokens) {
                list[i] = tokenizer.nextToken().trim();
            } else {
                list[i] = tokenizer.nextToken();
            }
        }
        return list;
    }
}

Related

  1. trimStart(String s)
  2. trim(String str)
  3. trimToNull(String str)
  4. trimEnd(String s)
  5. nullsafeTrim(String str)
  6. trimScheme(String uri)
  7. ltrim(final String text)