Java examples for java.lang:Assert
Splits the argument on white space.
//package com.java2s; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { String value = "java2s.com"; System.out.println(java.util.Arrays.toString(split(value))); }//from w w w . ja va 2 s . c o m /** * An empty string array instance. */ private final static String[] EMPTY_STRING_ARRAY = {}; /** * Splits the argument on white space. * * @param value the attribute value * @return a string array with zero or more strings none of which * is the empty string and none of which contains white space characters. */ public static String[] split(String value) { if (value == null || "".equals(value)) { return EMPTY_STRING_ARRAY; } int len = value.length(); List<String> list = new LinkedList<>(); boolean collectingSpace = true; int start = 0; for (int i = 0; i < len; i++) { char c = value.charAt(i); if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { if (!collectingSpace) { list.add(value.substring(start, i)); collectingSpace = true; } } else { if (collectingSpace) { start = i; collectingSpace = false; } } } if (start < len) { int end = len; for (int i = 1; len > i; i++) { char c = value.charAt(len - i); if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { end--; continue; } break; } list.add(value.substring(start, end)); } return list.toArray(EMPTY_STRING_ARRAY); } }