true if the buffer contains the text.
+ */
+ public static int areEquals( char[] charArray, int index, String text )
+ {
+ if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) ||
+ ( index < 0 ) || ( text == null ) )
+ {
+ return NOT_EQUAL;
}
else
{
- byte[] data = text.getBytes();
+ char[] data = text.toCharArray();
- return areEquals( byteArray, index, data );
+ return areEquals( charArray, index, data );
}
}
@@ -374,7 +483,7 @@
( charArray2.length == 0 ) ||
( charArray2.length > ( charArray.length + index ) ) )
{
- return -1;
+ return NOT_EQUAL;
}
else
{
@@ -382,7 +491,7 @@
{
if ( charArray[index++] != charArray2[i] )
{
- return -1;
+ return NOT_EQUAL;
}
}
@@ -410,7 +519,7 @@
( byteArray2.length == 0 ) ||
( byteArray2.length > ( byteArray.length + index ) ) )
{
- return -1;
+ return NOT_EQUAL;
}
else
{
@@ -418,7 +527,7 @@
{
if ( byteArray[index++] != byteArray2[i] )
{
- return -1;
+ return NOT_EQUAL;
}
}
@@ -452,6 +561,29 @@
}
/**
+ * Test if the current character is equal to a specific character.
+ *
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param car The character we want to compare with the current buffer position
+ *
+ * @return true if the current character equals the given character.
+ */
+ public static boolean isCharASCII( char[] chars, int index, char car )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( chars[index] == car ) ? true : false );
+ }
+ }
+
+ /**
* Check if the current character is an Hex Char
* true if the current character is a Hex Char
+ */
+ public static boolean isHex( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ char c = chars[index];
+
+ if ( ( c > 127 ) || ( StringUtils.HEX[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
* Test if the current character is a digit
* true if the current character is an Alpha character
+ */
+ public static boolean isAlphaASCII( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ char c = chars[index++];
+
+ if ( ( c > 127 ) || ( StringUtils.ALPHA[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
* Test if the current character is a digit
* true if the current character is a Digit
+ */
+ public static boolean isDigit( char[] chars, int index )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+ ( index >= chars.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( chars[index] > 127 ) || ! StringUtils.DIGIT[chars[index]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * true if the current character is a Digit
+ */
+ public static boolean isDigit( char[] chars )
+ {
+ if ( ( chars == null ) || ( chars.length == 0 ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( chars[0] > 127 ) || ! StringUtils.DIGIT[chars[0]] ) ? false : true );
+ }
+ }
+
+ /**
* Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
* "".
+ * @since 2.0
+ */
+ public static final String EMPTY = "";
+
+ // Empty checks
+ //-----------------------------------------------------------------------
+ /**
+ * Checks if a String is empty ("") or null.
+ * + *
+ * StringUtils.isEmpty(null) = true
+ * StringUtils.isEmpty("") = true
+ * StringUtils.isEmpty(" ") = false
+ * StringUtils.isEmpty("bob") = false
+ * StringUtils.isEmpty(" bob ") = false
+ *
+ *
+ * NOTE: This method changed in Lang version 2.0. + * It no longer trims the String. + * That functionality is available in isBlank().
+ * + * @param str the String to check, may be null + * @returntrue if the String is empty or null
+ */
+ public static boolean isEmpty(String str) {
+ return str == null || str.length() == 0;
+ }
+
+ /**
+ * Checks if a String is not empty ("") and not null.
+ * + *
+ * StringUtils.isNotEmpty(null) = false
+ * StringUtils.isNotEmpty("") = false
+ * StringUtils.isNotEmpty(" ") = true
+ * StringUtils.isNotEmpty("bob") = true
+ * StringUtils.isNotEmpty(" bob ") = true
+ *
+ *
+ * @param str the String to check, may be null
+ * @return true if the String is not empty and not null
+ */
+ public static boolean isNotEmpty(String str) {
+ return str != null && str.length() > 0;
+ }
+
+ /**
+ * Removes spaces (char <= 32) from both start and
+ * ends of this String, handling null by returning
+ * null.
+ * StringUtils.trim(null) = null
+ * StringUtils.trim("") = ""
+ * StringUtils.trim(" ") = ""
+ * StringUtils.trim("abc") = "abc"
+ * StringUtils.trim(" abc ") = "abc"
+ *
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, null if null String input
+ */
+ public static String trim(String str) {
+ if ( isEmpty( str ) )
+ {
+ return str;
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+ int end = array.length;
+
+ while ( ( start < end ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ while ( ( end > start ) && ( array[end - 1] == ' ' ) )
+ {
+ end--;
+ }
+
+ return new String( array, start, ( end - start ) );
+ }
+
+ /**
+ * Removes spaces (char <= 32) from start
+ * of this String, handling null by returning
+ * null.
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ *
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, null if null String input
+ */
+ public static String trimLeft( String str ) {
+ if ( isEmpty( str ) )
+ {
+ return str;
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+
+ while ( ( start < array.length ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ return new String( array, start, array.length - start );
+ }
+ /**
+ * Removes spaces (char <= 32) from start
+ * of this array, handling null by returning
+ * null.
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ *
+ *
+ * @param chars the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimLeft( char[] chars, int pos ) {
+ if ( chars == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos < chars.length ) && ( chars[pos] == ' ' ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+
+ /**
+ * Removes spaces (char <= 32) from start
+ * of this array, handling null by returning
+ * null.
+ * StringUtils.trimLeft(null) = null
+ * StringUtils.trimLeft("") = ""
+ * StringUtils.trimLeft(" ") = ""
+ * StringUtils.trimLeft("abc") = "abc"
+ * StringUtils.trimLeft(" abc ") = "abc "
+ *
+ *
+ * @param bytes the byte array to be trimmed, may be null
+ * @return the position of the first byte which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimLeft( byte[] bytes, int pos ) {
+ if ( bytes == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+
+
+ /**
+ * Removes spaces (char <= 32) from end
+ * of this String, handling null by returning
+ * null.
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ *
+ *
+ * @param str the String to be trimmed, may be null
+ * @return the trimmed string, null if null String input
+ */
+ public static String trimRight( String str ) {
+ if ( isEmpty( str ) )
+ {
+ return str;
+ }
+
+ char[] array = str.toCharArray();
+ int start = 0;
+ int end = array.length;
+
+ while ( ( start < end ) && ( array[start] == ' ' ) )
+ {
+ start++;
+ }
+
+ return new String( array, start, ( end - start ) );
+ }
+
+ /**
+ * Removes spaces (char <= 32) from end
+ * of this array, handling null by returning
+ * null.
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ *
+ *
+ * @param chars the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimRight( char[] chars, int pos ) {
+ if ( chars == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos > 0 ) && ( chars[pos - 1] == ' ' ) )
+ {
+ pos--;
+ }
+
+ return pos;
+ }
+
+ /**
+ * Removes spaces (char <= 32) from end
+ * of this array, handling null by returning
+ * null.
+ * StringUtils.trimRight(null) = null
+ * StringUtils.trimRight("") = ""
+ * StringUtils.trimRight(" ") = ""
+ * StringUtils.trimRight("abc") = "abc"
+ * StringUtils.trimRight(" abc ") = " abc"
+ *
+ *
+ * @param chars the chars array to be trimmed, may be null
+ * @return the position of the first char which is not a space,
+ * or the last position of the array.
+ */
+ public static int trimRight( byte[] bytes, int pos ) {
+ if ( bytes == null )
+ {
+ return pos;
+ }
+
+ while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
+ {
+ pos--;
+ }
+
+ return pos;
+ }
+
+ // Case conversion
+ //-----------------------------------------------------------------------
+ /**
+ * Converts a String to upper case as per {@link String#toUpperCase()}.
+ * + *A null input String returns null.
+ * StringUtils.upperCase(null) = null
+ * StringUtils.upperCase("") = ""
+ * StringUtils.upperCase("aBc") = "ABC"
+ *
+ *
+ * @param str the String to upper case, may be null
+ * @return the upper cased String, null if null String input
+ */
+ public static String upperCase(String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.toUpperCase();
+ }
+
+ /**
+ * Converts a String to lower case as per {@link String#toLowerCase()}.
+ * + *A null input String returns null.
+ * StringUtils.lowerCase(null) = null
+ * StringUtils.lowerCase("") = ""
+ * StringUtils.lowerCase("aBc") = "abc"
+ *
+ *
+ * @param str the String to lower case, may be null
+ * @return the lower cased String, null if null String input
+ */
+ public static String lowerCase(String str) {
+ if (str == null) {
+ return null;
+ }
+ return str.toLowerCase();
+ }
+
+ // Equals
+ //-----------------------------------------------------------------------
+ /**
+ * Compares two Strings, returning true if they are equal.
nulls are handled without exceptions. Two null
+ * references are considered to be equal. The comparison is case sensitive.
+ * StringUtils.equals(null, null) = true
+ * StringUtils.equals(null, "abc") = false
+ * StringUtils.equals("abc", null) = false
+ * StringUtils.equals("abc", "abc") = true
+ * StringUtils.equals("abc", "ABC") = false
+ *
+ *
+ * @see java.lang.String#equals(Object)
+ * @param str1 the first String, may be null
+ * @param str2 the second String, may be null
+ * @return true if the Strings are equal, case sensitive, or
+ * both null
+ */
+ public static boolean equals(String str1, String str2) {
+ return str1 == null ? str2 == null : str1.equals(str2);
+ }
}