1 16 package org.jmanage.util; 17 18 import java.util.*; 19 import java.lang.reflect.Array ; 20 import java.net.URL ; 21 22 27 public class StringUtils { 28 29 35 public static String [] csvToStringArray(String csv){ 36 37 if(csv == null){ 38 return null; 39 } 40 StringTokenizer tokenizer = new StringTokenizer(csv, ","); 41 String [] array = new String [tokenizer.countTokens()]; 42 int index = 0; 43 while(tokenizer.hasMoreTokens()){ 44 array[index ++] = tokenizer.nextToken().trim(); 45 } 46 return array; 47 } 48 49 52 public static String stringArrayToCSV(String [] array){ 53 StringBuffer buff = new StringBuffer (); 54 for(int i=0; array != null && i<array.length; i++){ 55 buff.append(array[i]); 56 if(i < array.length){ 57 buff.append(","); 58 } 59 } 60 return buff.toString(); 61 } 62 63 public static String [] listToStringArray(List list){ 64 String [] output = new String [list.size()]; 65 int i=0; 66 for(Iterator it=list.iterator(); it.hasNext(); i++){ 67 output[i] = it.next().toString(); 68 } 69 return output; 70 } 71 72 public static List csvToList(String csv) { 73 if(csv == null){ 74 return null; 75 } 76 StringTokenizer tokenizer = new StringTokenizer(csv, ","); 77 List list = new ArrayList(tokenizer.countTokens()); 78 while(tokenizer.hasMoreTokens()){ 79 list.add(tokenizer.nextToken().trim()); 80 } 81 return list; 82 } 83 84 public static String toString(URL [] urls){ 85 StringBuffer buff = new StringBuffer (); 86 for(int i=0; i<urls.length; i++){ 87 buff.append(urls[i].toString()); 88 buff.append(";"); 89 } 90 return buff.toString(); 91 } 92 93 public static String padRight(String str, int totalLength) { 94 int strLen = str.length(); 95 StringBuffer buff = new StringBuffer (str); 96 for(int i=0;i<totalLength - strLen; i++){ 97 buff.append(' '); 98 } 99 return buff.toString(); 100 } 101 102 public static String getCharSeries(char ch, int length) { 103 char[] series = new char[length]; 104 Arrays.fill(series, ch); 105 return new String (series); 106 } 107 108 public static String htmlEscape(String str){ 109 StringBuffer buff = new StringBuffer (str.length()); 110 for(int i=0; i<str.length(); i++){ 111 final char ch = str.charAt(i); 112 if(ch == '"'){ 113 buff.append("""); 114 }else if(ch == '<'){ 115 buff.append("<"); 116 }else if(ch == '>'){ 117 buff.append(">"); 118 }else{ 119 buff.append(ch); 120 } 121 } 122 return buff.toString(); 123 } 124 } 125 | Popular Tags |