1.1 方法
public static String[] spiltArr(String arrStr) { // 去掉字符串所有的空格字符 arrStr = arrStr.replaceAll("\s*", ""); // 如果arrStr=[]返回[] if (arrStr.length() <= 2) { return new String[]{}; } // 如果arrStr=[1]返回[1] else if (arrStr.length() == 3) { return new String[]{String.valueOf(arrStr.charAt(1))}; } // [1,2]==>[1,2] else { String[] splits = arrStr.split(","); splits[0] = splits[0].substring(1); splits[splits.length - 1] = splits[splits.length - 1].substring(0, splits[splits.length - 1].length() - 1); return splits; } }
1.2 测试代码
public static void main(String[] args) { System.out.println(Arrays.toString(spiltArr("[0]"))); System.out.println(Arrays.toString(spiltArr("[1]"))); System.out.println(Arrays.toString(spiltArr("[1 ]"))); System.out.println(Arrays.toString(spiltArr("[1 ,2]"))); System.out.println(Arrays.toString(spiltArr("[1 , 3, 6]"))); System.out.println(Arrays.toString(spiltArr(" [ 1234567 ,37 ,4,5,6, 128900 ] "))); }
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)