Java String Split splitAndTrim(String str)

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

Description

Splits the lines of a string, and trims each line.

License

Apache License

Declaration

public static List<String> splitAndTrim(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 See AUTHORS file.//from  w ww. j a va 2s  .c  o m
 * 
 * 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.Arrays;
import java.util.List;

public class Main {
    /**
     * Splits the lines of a string, and trims each line.
     */
    public static List<String> splitAndTrim(String str) {
        String[] strs = str.split("\n");
        List<String> list = Arrays.asList(strs);
        trim(list);
        for (int i = list.size() - 1; i >= 0; i--)
            if (list.get(i).isEmpty())
                list.remove(i);
        return list;
    }

    /**
     * Trims every string of a collection.
     */
    public static void trim(List<String> strs) {
        for (int i = 0, n = strs.size(); i < n; i++)
            strs.set(i, strs.get(i).trim());
    }

    /**
     * Trims every string of an array.
     */
    public static void trim(String[] strs) {
        for (int i = 0, n = strs.length; i < n; i++)
            strs[i] = strs[i].trim();
    }
}

Related

  1. split(String toSplit)
  2. split(String value)
  3. split(String value)
  4. split_fields(String str)
  5. split_str(String input, String split)
  6. splitAndTrim(String str, String splitBy)
  7. splitAndUnEscape(String str)
  8. splitArgs(String args, int expectedArgs)
  9. splitArguments(final String str)