com.mucommander.util
Class StringUtils

java.lang.Object
  extended by com.mucommander.util.StringUtils

public class StringUtils
extends java.lang.Object

This class contains convenience methods for working with strings.

Author:
Maxence Bernard, Nicolas Rinaudo

Method Summary
static java.lang.String capitalize(java.lang.String s)
          Capitalizes the given string, making its first character upper case, and the rest of them lower case.
static boolean endsWith(java.lang.String a, char[] b)
          Equivalent of String.endsWith(String) using a char[].
static boolean endsWithIgnoreCase(java.lang.String a, char[] b)
          Returns true if a ends with b regardless of the case.
static boolean endsWithIgnoreCase(java.lang.String a, java.lang.String b)
          Returns true if a ends with b regardless of the case.
static boolean equals(java.lang.String s1, java.lang.String s2, boolean caseSensitive)
          Compares the two specified strings and returns true if both strings are equal.
static boolean equals(java.lang.String s1, java.lang.String s2, java.util.Locale locale)
          This method is a locale-aware version of java.lang.String#equals(Object).
static boolean matches(java.lang.String a, char[] b, int posA)
          Returns true if the substring of a starting at posA matches b.
static boolean matchesIgnoreCase(java.lang.String a, char[] b, int posA)
          Returns true if the substring of a starting at posA matches b regardless of the case.
static boolean matchesIgnoreCase(java.lang.String a, java.lang.String b, int posA)
          Returns true if the substring of a starting at posA matches b regardless of the case.
static int parseIntDef(java.lang.String s, int def)
          Parses the string argument as a signed decimal integer.
static java.lang.String replaceCompat(java.lang.String s, java.lang.String target, java.lang.String replacement)
          Replaces each occurrence of the target string in the given string with the specified replacement string, and returns the resulting string.
static boolean startsWithIgnoreCase(java.lang.String a, java.lang.String b)
          Returns true if a starts with b regardless of the case.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

endsWithIgnoreCase

public static boolean endsWithIgnoreCase(java.lang.String a,
                                         java.lang.String b)
Returns true if a ends with b regardless of the case.

This method has a known bug under some alphabets with peculiar capitalisation rules such as the Georgian one, where Character.toUpperCase(a) == Character.toUpperCase(b) doesn't necessarily imply that Character.toLowerCase(a) == Character.toLowerCase(b). The performance hit of testing for this exceptions is so huge that it was deemed an acceptable issue.

Note that this method will return true if b is an emptry string.

Parameters:
a - string to test.
b - suffix to test for.
Returns:
true if a ends with b regardless of the case, false otherwise.

matchesIgnoreCase

public static boolean matchesIgnoreCase(java.lang.String a,
                                        java.lang.String b,
                                        int posA)
Returns true if the substring of a starting at posA matches b regardless of the case.

This method has a known bug under some alphabets with peculiar capitalisation rules such as the Georgian one, where Character.toUpperCase(a) == Character.toUpperCase(b) doesn't necessarily imply that Character.toLowerCase(a) == Character.toLowerCase(b). The performance hit of testing for this exceptions is so huge that it was deemed an acceptable issue.

Note that this method will return true if b is an emptry string.

Parameters:
a - string to test.
b - suffix to test for.
posA - position in a at which to look for b
Returns:
true if a ends with b regardless of the case, false otherwise..
Throws:
java.lang.ArrayIndexOutOfBoundsException - if a.length is smaller than posA.

endsWithIgnoreCase

public static boolean endsWithIgnoreCase(java.lang.String a,
                                         char[] b)
Returns true if a ends with b regardless of the case.

This method has a known bug under some alphabets with peculiar capitalisation rules such as the Georgian one, where Character.toUpperCase(a) == Character.toUpperCase(b) doesn't necessarily imply that Character.toLowerCase(a) == Character.toLowerCase(b). The performance hit of testing for this exceptions is so huge that it was deemed an acceptable issue.

Note that this method will return true if b is an emptry string.

Parameters:
a - string to test.
b - suffix to test for.
Returns:
true if a ends with b regardless of the case, false otherwise.

matchesIgnoreCase

public static boolean matchesIgnoreCase(java.lang.String a,
                                        char[] b,
                                        int posA)
Returns true if the substring of a starting at posA matches b regardless of the case.

This method has a known bug under some alphabets with peculiar capitalisation rules such as the Georgian one, where Character.toUpperCase(a) == Character.toUpperCase(b) doesn't necessarily imply that Character.toLowerCase(a) == Character.toLowerCase(b). The performance hit of testing for this exceptions is so huge that it was deemed an acceptable issue.

Note that this method will return true if b is an emptry string.

Parameters:
a - string to test.
b - suffix to test for.
posA - position in a at which to look for b
Returns:
true if a ends with b regardless of the case, false otherwise..
Throws:
java.lang.ArrayIndexOutOfBoundsException - if a.length is smaller than posA.

endsWith

public static boolean endsWith(java.lang.String a,
                               char[] b)
Equivalent of String.endsWith(String) using a char[].

Parameters:
a - String to test.
b - suffix to test.
Returns:
true if a ends with b.

matches

public static boolean matches(java.lang.String a,
                              char[] b,
                              int posA)
Returns true if the substring of a starting at posA matches b.

Parameters:
a - String to test.
b - substring to look for.
posA - position in a at which to look for b
Returns:
true if a contains b at position posA, false otherwise..

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String a,
                                           java.lang.String b)
Returns true if a starts with b regardless of the case.

Note that this method will return true if b is an emptry string.

Parameters:
a - string to test.
b - prefix to test for.
Returns:
true if a starts with b regardless of the case, false otherwise..

replaceCompat

public static java.lang.String replaceCompat(java.lang.String s,
                                             java.lang.String target,
                                             java.lang.String replacement)
Replaces each occurrence of the target string in the given string with the specified replacement string, and returns the resulting string. This method mimicks java.lang.String#replace(CharSequence, CharSequence) which was introduced in Java 1.5, but unlike the latter, this method can be used on any version of Java.

On Java 1.5 and up, this method delegates to java.lang.String#replace(CharSequence, CharSequence). On Java 1.4 or below, a custom implementation (that doesn't use Regexp) is used.

Parameters:
s - the string in which to replace ocurrences of target
target - the string to be replaced
replacement - the replacement for occurrences of target
Returns:
the resulting string

equals

public static boolean equals(java.lang.String s1,
                             java.lang.String s2,
                             java.util.Locale locale)
This method is a locale-aware version of java.lang.String#equals(Object). It returns true if the two given String are equal in the specified Locale.

This method is useful for testing text expressed in a language where two strings with an identical written representation can have a different String representation according to java.lang.String#equals(Object). Japanese is such a language for instance. This method uses the java.text.Collator class under the hood.

Parameters:
s1 - a String to compare
s2 - a String to compare
locale - the Locale to consider for testing the String
Returns:
true if the two given String are equal in the specified Locale

equals

public static boolean equals(java.lang.String s1,
                             java.lang.String s2,
                             boolean caseSensitive)
Compares the two specified strings and returns true if both strings are equal. This method handles null values with no risk of a NullPointerException. The comparison is case-sensitive only if requested.

In other words, this method returns true if strings are either both null or equal according to String#equals(String) for case-sensitive comparison, or String#equalsIgnoreCase(String) for case-insensitive comparison.

Parameters:
s1 - string to compare, potentially null
s2 - string to compare, potentially null
caseSensitive - true for case-sensitive comparison, false for case-insensitive comparison
Returns:
true if strings are equal or both null

parseIntDef

public static int parseIntDef(java.lang.String s,
                              int def)
Parses the string argument as a signed decimal integer. If the string cannot be parsed a default value is returned.

Parameters:
s - a String containing the int representation to be parsed
def - a default value if string cannot be parsed
Returns:
the integer value represented by the argument or default value if it cannot be parsed

capitalize

public static java.lang.String capitalize(java.lang.String s)
Capitalizes the given string, making its first character upper case, and the rest of them lower case. This method reeturns an empty string if null or an empty string is passed.

Parameters:
s - the string to capitalize
Returns:
the capitalized string


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard