1 15 package org.apache.hivemind.util; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 26 public class StringUtils 27 { 28 29 36 public static String [] split(String input) 37 { 38 if (input == null) 39 return new String [0]; 40 41 List strings = new ArrayList (); 42 43 int startx = 0; 44 int cursor = 0; 45 int length = input.length(); 46 47 while (cursor < length) 48 { 49 if (input.charAt(cursor) == ',') 50 { 51 String item = input.substring(startx, cursor); 52 strings.add(item); 53 startx = cursor + 1; 54 } 55 56 cursor++; 57 } 58 59 if (startx < length) 60 strings.add(input.substring(startx)); 61 62 return (String []) strings.toArray(new String [strings.size()]); 63 } 64 65 72 73 public static String capitalize(String input) 74 { 75 if (input.length() == 0) 76 return input; 77 78 char ch = input.charAt(0); 79 80 if (Character.isUpperCase(ch)) 81 return input; 82 83 return String.valueOf(Character.toUpperCase(ch)) + input.substring(1); 84 } 85 86 public static String join(String [] input, char separator) 87 { 88 if (input == null || input.length == 0) 89 return null; 90 91 StringBuffer buffer = new StringBuffer (); 92 93 for (int i = 0; i < input.length; i++) 94 { 95 if (i > 0) 96 buffer.append(separator); 97 98 buffer.append(input[i]); 99 } 100 101 return buffer.toString(); 102 } 103 104 108 public static String replace(String string, String pattern, String replacement) 109 { 110 StringBuffer sbuf = new StringBuffer (); 111 int index = string.indexOf(pattern); 112 int pos = 0; 113 int patternLength = pattern.length(); 114 for(; index >= 0; index = string.indexOf(pattern, pos)) 115 { 116 sbuf.append(string.substring(pos, index)); 117 sbuf.append(replacement); 118 pos = index + patternLength; 119 } 120 sbuf.append(string.substring(pos)); 121 122 return sbuf.toString(); 123 } 124 125 } 126 | Popular Tags |