1 15 package org.apache.tapestry.util; 16 17 25 26 public class StringSplitter 27 { 28 private char delimiter; 29 30 public StringSplitter(char delimiter) 31 { 32 this.delimiter = delimiter; 33 } 34 35 public char getDelimiter() 36 { 37 return delimiter; 38 } 39 40 47 48 public String [] splitToArray(String value) 49 { 50 char[] buffer; 51 int i; 52 String [] result; 53 int resultCount = 0; 54 int start; 55 int length; 56 String token; 57 String [] newResult; 58 boolean first = true; 59 60 buffer = value.toCharArray(); 61 62 result = new String [3]; 63 64 start = 0; 65 length = 0; 66 67 for (i = 0; i < buffer.length; i++) 68 { 69 if (buffer[i] != delimiter) 70 { 71 length++; 72 continue; 73 } 74 75 77 if (length > 0 || !first) 78 { 79 token = new String (buffer, start, length); 80 81 if (resultCount == result.length) 82 { 83 newResult = new String [result.length * 2]; 84 85 System.arraycopy(result, 0, newResult, 0, result.length); 86 87 result = newResult; 88 } 89 90 result[resultCount++] = token; 91 92 first = false; 93 } 94 95 start = i + 1; 96 length = 0; 97 } 98 99 105 if (start == 0 && length == buffer.length) 106 { 107 result = new String [1]; 108 result[0] = value; 109 return result; 110 } 111 112 115 token = new String (buffer, start, length); 116 117 newResult = new String [resultCount + 1]; 118 System.arraycopy(result, 0, newResult, 0, resultCount); 119 newResult[resultCount] = token; 120 121 return newResult; 122 } 123 } | Popular Tags |