1 9 package com.ziclix.python.sql.pipe.csv; 10 11 17 public class CSVString { 18 19 22 public static final String DELIMITER = ","; 23 24 private CSVString() { 25 } 26 27 30 public static String toCSV(String string) { 31 return toCSV(string, CSVString.DELIMITER); 32 } 33 34 37 public static String toCSV(String string, String delimiter) { 38 39 String res = replace(string, "\"", "\"\""); 40 41 if ((res.indexOf("\"") >= 0) || (string.indexOf(delimiter) >= 0)) { 42 res = "\"" + res + "\""; 43 } 44 45 return res; 46 } 47 48 61 public static String replace(String original, String search, String replace, boolean all) { 62 63 String valReturn = new String (""); 64 int l = original.length(); 65 int lo = search.length(); 66 int i = 0; 67 int j; 68 69 while (i <= l) { 70 j = original.indexOf(search, i); 71 72 if (j == -1) { 73 valReturn = valReturn.concat(original.substring(i, l)); 74 i = l + 1; } else { 76 valReturn = valReturn.concat(original.substring(i, j)); 77 valReturn = valReturn.concat(replace); 78 i = j + lo; 79 80 if (!all) { valReturn = valReturn.concat(original.substring(i, l)); 82 i = l + 1; } 84 } 85 } 86 87 return valReturn; 88 } 89 90 100 public static String replace(String original, String search, String replace) { 101 return replace(original, search, replace, true); 102 } 103 104 113 public static String replaceEndWith(String original, String oldSuffix, String newSuffix) { 114 115 if (original.endsWith(oldSuffix)) { 116 String st = original.substring(0, original.length() - oldSuffix.length()); 117 118 return st.concat(newSuffix); 119 } else { 120 return original; 121 } 122 } 123 } 124 | Popular Tags |