Here you can find the source of splitInts(String string, String separator)
Parameter | Description |
---|---|
string | a parameter |
separator | a parameter |
public static int[] splitInts(String string, String separator)
//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; } }