1 package ca.uhn.hl7v2.util;
2
3
4
5
6 public class StringUtil {
7
8
9
10
11
12 public static int countLines(String theString) {
13 int retVal = 1;
14
15 for (int i = 0; i < theString.length(); i++) {
16 char nextChar = theString.charAt(i);
17 if (i > 0 && nextChar == '\n' && theString.charAt(i - 1) == '\r') {
18 continue;
19 }
20 if (nextChar == '\r' | nextChar == '\n') {
21 retVal++;
22 }
23 }
24
25 return retVal;
26 }
27
28
29
30
31
32 public static String chomp(String theLine) {
33 int i;
34 int start = theLine.length() - 1;
35 for (i = start; i >= 0; i--) {
36 char nextChar = theLine.charAt(i);
37 if (nextChar != '\r' && nextChar != '\n') {
38 break;
39 }
40 }
41
42 if (i == start) {
43 return theLine;
44 } else {
45 return theLine.substring(0, i + 1);
46 }
47 }
48
49
50
51
52
53 public static void validateNotEmpty(String theValue) {
54 if (theValue == null || theValue.length() == 0) {
55 throw new IllegalArgumentException();
56 }
57 }
58
59
60
61
62
63 public static String replace(String theString, String theMatch, String theReplacement) {
64 StringBuilder buf = new StringBuilder(theString.length());
65 int start = 0, end;
66 while ((end = theString.indexOf(theMatch, start)) != -1) {
67 buf.append(theString, start, end).append(theReplacement);
68 start = end + theMatch.length();
69 }
70 buf.append(theString.substring(start));
71 return buf.toString();
72 }
73
74 public static String[] concatenate(String[] array1, String[] array2) {
75 if (array1 == null) return array2;
76 if (array2 == null) return array1;
77 String[] result = new String[array1.length + array2.length];
78 System.arraycopy(array1, 0, result, 0, array1.length);
79 System.arraycopy(array2, 0, result, array1.length, array2.length);
80 return result;
81 }
82
83
84
85
86 public static boolean isBlank(String theString) {
87 if (theString == null) {
88 return true;
89 }
90
91 for (int i = 0; i < theString.length(); i++) {
92 if (!Character.isWhitespace(theString.charAt(i))) {
93 return false;
94 }
95 }
96
97 return true;
98 }
99
100
101
102
103 public static boolean isNotBlank(String theString) {
104 return !isBlank(theString);
105 }
106
107 }