Java String Split by Separator splitInts(String string, String separator)

Here you can find the source of splitInts(String string, String separator)

Description

Split a string using the provided separator, then convert the split components to ints.

License

Apache License

Parameter

Parameter Description
string a parameter
separator a parameter

Return

Array of ints parsed from the string.

Declaration

public static int[] splitInts(String string, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Geoscience Australia// www. j  a  v  a2 s . co 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.ArrayList;

import java.util.List;

public class Main {
    /**
     * Split a string using the provided separator, then convert the split
     * components to ints.
     * 
     * @param string
     * @param separator
     * @return Array of ints parsed from the string.
     */
    public static int[] splitInts(String string, String separator) {
        String[] split = string.trim().split(separator);
        List<Integer> ints = new ArrayList<Integer>(split.length);
        for (String s : split) {
            try {
                ints.add(Integer.valueOf(s.trim()));
            } catch (Exception e) {
            }
        }
        int[] is = new int[ints.size()];
        for (int i = 0; i < is.length; i++) {
            is[i] = ints.get(i);
        }
        return is;
    }
}

Related

  1. splitAndTrim(String tags, String separator)
  2. splitBySeparator(String path, String separator)
  3. splitByStringSeparator(String theString, String separatorString)
  4. splitDoubles(String str, String fieldSeparator)
  5. splitField(String fieldWithSeparator, String separator)
  6. splitList(String source, char separator)
  7. splitListBySeparator(String text, String separator)
  8. splitNotRegex(String str, String separatorChars)
  9. splitSmart(String s, char separator)