@@ -46,14 +46,61 @@ public class DtStringUtil {
4646
4747 private static ObjectMapper objectMapper = new ObjectMapper ();
4848
49-
5049 /**
5150 * Split the specified string delimiter --- ignored quotes delimiter
5251 * @param str
5352 * @param delimiter
5453 * @return
5554 */
56- public static List <String > splitIgnoreQuota (String str , char delimiter ){
55+ public static List <String > splitIgnoreQuota (String str , char delimiter ) {
56+ List <String > tokensList = new ArrayList <>();
57+ boolean inQuotes = false ;
58+ boolean inSingleQuotes = false ;
59+ int bracketLeftNum = 0 ;
60+ StringBuilder b = new StringBuilder ();
61+ char [] chars = str .toCharArray ();
62+ int idx = 0 ;
63+ for (char c : chars ) {
64+ char flag = 0 ;
65+ if (idx > 0 ) {
66+ flag = chars [idx - 1 ];
67+ }
68+ if (c == delimiter ) {
69+ if (inQuotes ) {
70+ b .append (c );
71+ } else if (inSingleQuotes ) {
72+ b .append (c );
73+ } else if (bracketLeftNum > 0 ) {
74+ b .append (c );
75+ } else {
76+ tokensList .add (b .toString ());
77+ b = new StringBuilder ();
78+ }
79+ } else if (c == '\"' && '\\' != flag && !inSingleQuotes ) {
80+ inQuotes = !inQuotes ;
81+ b .append (c );
82+ } else if (c == '\'' && '\\' != flag && !inQuotes ) {
83+ inSingleQuotes = !inSingleQuotes ;
84+ b .append (c );
85+ } else if (c == '(' && !inSingleQuotes && !inQuotes ) {
86+ bracketLeftNum ++;
87+ b .append (c );
88+ } else if (c == ')' && !inSingleQuotes && !inQuotes ) {
89+ bracketLeftNum --;
90+ b .append (c );
91+ } else {
92+ b .append (c );
93+ }
94+ idx ++;
95+ }
96+
97+ tokensList .add (b .toString ());
98+
99+ return tokensList ;
100+ }
101+
102+ public static List <String > splitField (String str ) {
103+ final char delimiter = ',' ;
57104 List <String > tokensList = new ArrayList <>();
58105 boolean inQuotes = false ;
59106 boolean inSingleQuotes = false ;
@@ -106,7 +153,6 @@ public static List<String> splitIgnoreQuota(String str, char delimiter){
106153 return tokensList ;
107154 }
108155
109-
110156 public static String replaceIgnoreQuota (String str , String oriStr , String replaceStr ){
111157 String splitPatternStr = oriStr + "(?=(?:[^\" ]*\" [^\" ]*\" )*[^\" ]*$)(?=(?:[^']*'[^']*')*[^']*$)" ;
112158 return str .replaceAll (splitPatternStr , replaceStr );
0 commit comments