001 package org.wdssii.core; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 /** 007 * @author lakshman 008 * 009 */ 010 public class StringUtil { 011 /** returns s.length() if no characters are sep. */ 012 public static int find_first_of(String s, char sep, int start) { 013 for (int i = start; i < s.length(); ++i) 014 if (s.charAt(i) == sep) 015 return i; 016 return s.length(); 017 } 018 019 /** returns s.length() if all characters are sep. */ 020 public static int find_first_not_of(String s, char sep, int start) { 021 for (int i = start; i < s.length(); ++i) 022 if (s.charAt(i) != sep) 023 return i; 024 return s.length(); 025 } 026 027 /** 028 * splits the input string into an array of strings assuming they are 029 * space-separated 030 */ 031 public static List<String> split(String s) { 032 char sep = ' '; 033 return split(s, sep); 034 } 035 036 /** splits the input string into an array of strings. */ 037 public static List<String> split(String s, char sep) { 038 List<String> result = new ArrayList<String>(); 039 int startPos = 0; 040 int endPos = 0; 041 do { 042 startPos = find_first_not_of(s, sep, endPos); 043 endPos = find_first_of(s, sep, startPos); 044 if (endPos > startPos) 045 result.add(s.substring(startPos, endPos)); 046 } while (endPos > startPos); 047 048 return result; 049 } 050 051 public static List<String> splitOnFirst(String s, char sep) { 052 List<String> result = new ArrayList<String>(); 053 int startPos = 0; 054 int endPos = 0; 055 056 startPos = find_first_not_of(s, sep, endPos); 057 endPos = find_first_of(s, sep, startPos); 058 059 if (endPos > startPos) { 060 result.add(s.substring(startPos, endPos)); 061 result.add(s.substring(endPos + 1, s.length())); 062 } 063 064 return result; 065 } 066 067 }