-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathCSVString.java
More file actions
120 lines (102 loc) · 3.79 KB
/
CSVString.java
File metadata and controls
120 lines (102 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.pipe.csv;
/**
* A utility class to aide in quoting CSV strings.
*
* @author brian zimmer
*/
public class CSVString {
/**
* The default delimiter.
*/
public static final String DELIMITER = ",";
private CSVString() {
}
/**
* Escape the string as needed using the default delimiter.
*/
public static String toCSV(String string) {
return toCSV(string, CSVString.DELIMITER);
}
/**
* Escape the string as needed using the given delimiter.
*/
public static String toCSV(String string, String delimiter) {
String res = replace(string, "\"", "\"\"");
if ((res.indexOf("\"") >= 0) || (string.indexOf(delimiter) >= 0)) {
res = "\"" + res + "\"";
}
return res;
}
/**
* Returns a new string resulting from replacing the first occurrence, or all occurrences, of
* search string in this string with replace string. If the string search does not occur in the
* character sequence represented by this object, then this string is returned.
*
* @param search the old string
* @param replace the new string
* @param all if {@code true} all occurrences of the search string are replaced; if
* {@code false} only the first occurrence
* @return a string derived from this string by replacing the first occurrence, or every
* occurrence, of {@code search} with {@code replace}.
*/
public static String replace(String original, String search, String replace, boolean all) {
String valReturn = new String("");
int l = original.length();
int lo = search.length();
int i = 0;
int j;
while (i <= l) {
j = original.indexOf(search, i);
if (j == -1) {
valReturn = valReturn.concat(original.substring(i, l));
i = l + 1; // Stop, no more occurrence.
} else {
valReturn = valReturn.concat(original.substring(i, j));
valReturn = valReturn.concat(replace);
i = j + lo;
if (!all) { // Stop, replace the first occurrence only.
valReturn = valReturn.concat(original.substring(i, l));
i = l + 1; // Stop, replace the first occurrence only.
}
}
}
return valReturn;
}
/**
* Returns a new string resulting from replacing all occurrences,
* of search string in this string with replace string.
* If the string search does not occur in the character sequence represented by this object,
* then this string is returned.
*
* @param search the old string
* @param replace the new string
* @return a string derived from this string by replacing every occurrence of search with replace.
*/
public static String replace(String original, String search, String replace) {
return replace(original, search, replace, true);
}
/**
* Returns a new string resulting from replacing the end of this String
* from oldSuffix to newSuffix.
* The original string is returned if it does not end with oldSuffix.
*
* @param oldSuffix the old suffix
* @param newSuffix the new suffix
* @return a string derived from this string by replacing the end oldSuffix by newSuffix
*/
public static String replaceEndWith(String original, String oldSuffix, String newSuffix) {
if (original.endsWith(oldSuffix)) {
String st = original.substring(0, original.length() - oldSuffix.length());
return st.concat(newSuffix);
} else {
return original;
}
}
}