-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringHandling.java
More file actions
375 lines (351 loc) · 10.2 KB
/
Copy pathStringHandling.java
File metadata and controls
375 lines (351 loc) · 10.2 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// ============================================================================
//
// Copyright (c) 2006-2015, Talend Inc.
//
// This source code has been automatically generated by_Talend Open Studio for Big Data
// / Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
package routines;
public class StringHandling {
/**
* Determines whether the expression is sorted alphabetically or not.
*
* {talendTypes} boolean | Boolean
*
* {Category} StringHandling
*
* {param} string("abcdefg") input: String need to be known whether is sorted alphabetically or not.
*
* {example} ALPHA("abcdefg") # true
*/
public static boolean ALPHA(String input) {
if (input != null) {
char[] val = input.toCharArray();
for (int i = 0; i < val.length - 1; i++) {
if (val[i] > val[i + 1]) {
return false;
}
}
return true;
}
return false;
}
/**
* Determines whether the expression is an alphabetic or nonalphabetic.
*
* {talendTypes} boolean | Boolean
*
* {Category} StringHandling
*
* {param} string("abc") input: String need to be known whether is an alphabetic or not.
*
* {example} ALPHA("abc") # true
*/
public static boolean IS_ALPHA(String input) {
if (input != null) {
char[] val = input.toCharArray();
for (int i = 0; i < val.length; i++) {
if (!Character.isLetter(val[i])) {
return false;
}
}
return true;
}
return false;
}
/**
* Substitutes an element of a string with a replacement element.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hello world!") oldStr: The whole string.
*
* {param} string("world") newStr: Regx.
*
* {param} string("guy") replacement: Replacement.
*
* {example} CHANGE("hello world!","world","guy") # hello world
*/
public static String CHANGE(String oldStr, String newStr, String replacement) {
if (oldStr == null || newStr == null || replacement == null)
return oldStr;
else
return oldStr.replaceAll(newStr, replacement);
}
/**
* Evaluates the number of times a substring is repeated in a string.
*
* {talendTypes} int | Integer
*
* {Category} StringHandling
*
* {param} string("hello world!") string: The whole string.
*
* {param} string("world") subString: subString.
*
* {example} COUNT("hello world!","world") # 1
*/
public static int COUNT(String string, String subString) {
if (string == null || subString == null){
return 0;
} else{
int counter = 0;
int i = -1;
while ((i = string.indexOf(subString, i + 1)) != -1) {
counter++;
}
return counter;
}
}
/**
* Converts all uppercase letters in an expression to lowercase.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("Hello") string: String
*
* {example} DOWNCASE("Hello") # hello
*/
public static String DOWNCASE(String string) {
return string == null ? null : string.toLowerCase();
}
/**
* Converts all lowercase letters in an expression to uppercase.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("Hello") string: String
*
* {example} UPCASE("Hello") # HELLO
*/
public static String UPCASE(String string) {
return string == null ? null : string.toUpperCase();
}
/**
* Encloses an expression in double quotation marks.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("Hello") string: String
*
* {example} DQUOTE("hello") # "hello"
*/
public static String DQUOTE(String string) {
return string == null ? null : ("\"" + string + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Substitutes an element of a string with a replacement element.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hello world!") oldStr: The whole string.
*
* {param} string("world") newStr: Regx.
*
* {param} string("guy") replacement: Replacement.
*
* {example} EREPLACE("hello world!","world","guy") # hello world
*/
public static String EREPLACE(String oldStr, String newStr, String replacement) {
return CHANGE(oldStr, newStr, replacement);
}
/**
* Returns the starting column position of a specified occurrence of a particular substring within a string
* expression.
*
* {talendTypes} int | Integer
*
* {Category} StringHandling
*
* {param} string("hello world!") string: string.
*
* {param} string("hello") element: element
*
* {example} INDEX("hello world!","hello") # 0
*/
public static int INDEX(String string, String element) {
if (string == null || element == null)
return -1;
else
return string.indexOf(element);
}
/**
* Specifies a substring consisting of the first n characters of a string.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hello world!") string: String.
*
* {param} int(5) index : index
*
* {example} LEFT("hello world!",5) # hello
*/
public static String LEFT(String string, int index) {
return string == null ? null : string.substring(0, Math.min(string.length(), index));
}
/**
* Specifies a substring consisting of the last n characters of a string.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hello world!") string: String
*
* {param} int(6) index : Index
*
* {example} RIGHT("hello world!",6) # world!
*/
public static String RIGHT(String string, int index) {
return string == null ? null : string.substring(string.length() - Math.min(string.length(), index));
}
/**
* Calculates the length of a string.
*
* {talendTypes} int | Integer
*
* {Category} StringHandling
*
* {param} string("hello world!") string:
*
* {example} LEN("hello world!") # 12
*/
public static int LEN(String string) {
return string == null ? -1 : string.length();
}
/**
* Generates a string consisting of a specified number of blank spaces.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} int(2) i: amount of blank space need to generate.
*
* {example} SPACE(2) # " "
*/
public static String SPACE(int i) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < i; j++) {
buffer.append(" "); //$NON-NLS-1$
}
return buffer.toString();
}
/**
* Encloses an expression in single quotation marks.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hellow world!") string:
*
* {example} SQUOTE("hellow world!") # 'hello world!'
*/
public static String SQUOTE(String string) {
return string == null ? null : ("'" + string + "'"); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Generates a particular character string a specified number of times.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string('a') string: character
*
* {param} int(6) int: times
*
* {example} SPACE("hellow world!",2) # hello world!
*/
public static String STR(char letter, int i) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < i; j++) {
buffer.append(letter);
}
return buffer.toString();
}
/**
* Deletes extra blank spaces and tabs from a character string.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string(" hellow world! ") string: string.
*
* {example} TRIM(" hellow world! ") # hello world!
*/
public static String TRIM(String string) {
return string == null ? null : string.trim();
}
/**
* Deletes all blank spaces and tabs after the last nonblank character in an expression.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("hellow world! ") string: string.
*
* {example} BTRIM("hellow world! ") # helloworld!
*/
public static String BTRIM(String string) {
if (string == null){
return null;
} else {
char[] val = string.toCharArray();
int len = val.length;
while (len > 0 && val[len - 1] <= ' ') {
len--;
}
return string.substring(0, len);
}
}
/**
* Deletes all blank spaces and tabs up to the first nonblank character in an expression.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string(" hellow world!") string: string.
*
* {example} FTRIM(" hellow world!") # hello world!
*/
public static String FTRIM(String string) {
if (string == null){
return null;
} else {
char[] val = string.toCharArray();
int st = 0;
int len = val.length;
while ((st < len) && (val[st] <= ' ')) {
st++;
}
return string.substring(st);
}
}
}