View Javadoc

1   package ca.uhn.hl7v2.util;
2   
3   /**
4    * Various string utility methods
5    */
6   public class StringUtil {
7   
8   	/**
9   	 * Counts the number of lines in a string by counting the number of "\n" or
10  	 * "\r" or "\r\n" sequences which appear in it
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  	 * Removes any line separators (\r ot \n) from the end of a string and
30  	 * returns that string
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  	 * Throws an IllegalArgumentException if the value is an empty string or
51  	 * null
52  	 */
53  	public static void validateNotEmpty(String theValue) {
54  		if (theValue == null || theValue.length() == 0) {
55  			throw new IllegalArgumentException();
56  		}
57  	}
58  
59  	/**
60  	 * Search within a string and replace one substring with another. Based on
61  	 * the method within Commons-Lang StringUtils.
62  	 */
63  	public static String replace(String theString, String theMatch, String theReplacement) {
64  		StringBuffer buf = new StringBuffer(theString.length());
65  		int start = 0, end = 0;
66  		while ((end = theString.indexOf(theMatch, start)) != -1) {
67  			buf.append(theString.substring(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  	 * Returns <code>true</code> if the string is null, or contains no non-whitespace characters
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)) == false) {
93  				return false;
94  			}
95  		}
96  		
97  		return true;
98  	}
99  
100 	/**
101 	 * Inverse of {@link #isBlank(String)}
102 	 */
103 	public static boolean isNotBlank(String theString) {
104 		return !isBlank(theString);
105 	}
106 
107 }