blob: 08d4608bf6a619b80e04da2b95776ef981fab185 [file] [log] [blame]
//------------------------------------------------------------------------------
//Copyright (c) 2004, 2006 IBM Corporation. All Rights Reserved.
//------------------------------------------------------------------------------
package org.eclipse.epf.web.search.utils;
import java.util.List;
import com.ibm.icu.util.StringTokenizer;
/**
* Implements a utility class for managing strings.
*
* @author Kelvin Low
* @since 6.0
*/
public class StrUtil {
// Array of hex digits.
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* Private constructor to prevent this class from being instantiated. All
* methods in this class should be static.
*/
private StrUtil() {
}
/**
* Is the given string a null string?
* <p>
* A null string is defined as one that has a empty reference (i.e. = null)
* or has zero length.
*
* @param str
* The test string.
* @return true if the the given string is a null string.
*/
public static boolean isNull(String str) {
return str == null || str.length() == 0;
}
/**
* Is the given string a blank string?
* <p>
* A blank string is defined as one that has a empty reference (i.e. = null)
* or has zero length after the leading and trailing space characters are
* trimmed.
*
* @param str
* The test string.
* @return true if the the given string is a blank string.
*/
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
/**
* Is the given string an ASCII string?
*
* @param str
* The test string.
* @return true if the the given string is an ASCII string.
*/
public static boolean isAscii(String str) {
int len = (str == null) ? 0 : str.length();
for (int i = 0; i < len; i++) {
if (str.charAt(i) > 0x007E) {
return false;
}
}
return true;
}
/**
* Removes the leading and trailing space characters (' ') from the given
* string.
*
* @param str
* The source string.
* @return A string with no leading and trailing space characters.
*/
public static String trim(String str) {
return str == null ? null : str.trim();
}
/**
* Removes white space characters ('\t', '\n', ' ') from the given string.
*
* @param str
* The source string.
* @return A string with white space characters removed.
*/
public static String removeWhiteSpaceChars(String str) {
int len = (str == null) ? 0 : str.length();
for (int i = 0; i < len; i++) {
switch (str.charAt(i)) {
case '\t':
case '\n':
case ' ':
break;
default:
return str;
}
}
return ""; //$NON-NLS-1$
}
/**
* Removes end-of-line characters from the given string.
*
* @param str
* The source string.
* @return A string with no end-of-line characters.
*/
public static String removeEndOfLineChars(String str) {
if (isNull(str)) {
return str;
}
String lineSep = FileUtil.LINE_SEP;
int lineSepSize = FileUtil.LINE_SEP.length();
while (str.endsWith(lineSep)) {
str = str.substring(0, str.length() - lineSepSize);
}
return str;
}
/**
* Removes the given leading and trailing characters from the given string.
*
* @param str
* The source string.
* @param chars
* The characters to removed.
* @return A string with stripped off the given leading and trailing
* characters.
*/
public static String removeChars(String str, String chars) {
if (isNull(str)) {
return str;
}
int sizeOfChars = chars.length();
while (str.startsWith(chars)) {
str = str.substring(sizeOfChars);
}
while (str.endsWith(chars)) {
str = str.substring(0, str.length() - sizeOfChars);
}
return str;
}
/**
* Splits the given strings into an array of tokens based on the given
* separators.
*
* @param str
* The source string.
* @param sep
* The string separators.
* @param count
* The number of tokens to return.
* @return An array of string tokens.
*/
public static String[] split(String str, String sep, int count) {
if (str == null || count == 0 || count < -1) {
return null;
}
StringTokenizer tok = new StringTokenizer(str, sep, count == -1 ? false
: true);
if (count == -1) {
count = tok.countTokens();
}
String[] result = new String[count];
int i = 0;
while (tok.hasMoreTokens()) {
String t = tok.nextToken();
if (i < count) {
if ((t.length() == 1) && (sep.indexOf(t) != -1)) {
continue;
}
result[i++] = t;
} else {
result[count - 1] += t;
}
}
return result;
}
/**
* Splits the given strings into an array of tokens based on the given
* separators.
*
* @param str
* The source string.
* @param sep
* The string separators.
* @return An array of string tokens.
*/
public static String[] split(String str, String sep) {
return split(str, sep, -1);
}
/**
* Returns true if a the given sub-string matches a string token in the
* delimited source string.
*
* @param str
* The delimited source string.
* @param strSep
* The separators that delimit the source string.
* @param substr
* The delimited sub-string.
* @return true if the given substr is found in the source string.
*/
public static boolean contains(String str, String strSep, String substr) {
if (str == null || strSep == null || substr == null) {
return false;
}
String[] strs = split(str, strSep, -1);
if (strs != null) {
for (int i = 0; i < strs.length; i++) {
if (strs[i] != null && strs[i].equals(substr)) {
return true;
}
}
}
return false;
}
/**
* Returns true if a string token in the given sub-string matches a string
* token in the delimited source string.
*
* @param str
* The delimited source string.
* @param strSep
* The separators that delimit the source string.
* @param substr
* Tthe delimited sub-string.
* @param subStrSep
* The separators that delimit the sub-string.
* @return true if the given substr is found in the source string.
*/
public static boolean contains(String str, String strSep, String substr,
String subStrSep) {
if (str == null || strSep == null || substr == null
|| subStrSep == null) {
return false;
}
String[] strs = split(str, strSep, -1);
String[] substrs = split(substr, subStrSep, -1);
if (strs != null && substrs != null) {
for (int i = 0; i < strs.length; i++) {
if (strs[i] != null) {
for (int j = 0; j < substrs.length; j++) {
if (substrs[j] != null && substrs[j].equals(strs[i])) {
return true;
}
}
}
}
}
return false;
}
/**
* Replaces a substring within a string with another string. This just
* replaces the first occurrence of the string.
*
* @param str
* The source string.
* @param src
* The substring to replace.
* @param tgt
* The substring to use for the replacement.
* @return The result string.
*/
public static String replace(String str, String src, String tgt) {
if (isNull(str) || isNull(src)) {
return str;
}
String tmpStr = str;
int index;
while ((index = tmpStr.indexOf(src)) != -1) {
tmpStr = tmpStr.substring(0, index) + tgt
+ tmpStr.substring(index + src.length());
}
return tmpStr;
}
/**
* Converts all characters in a string to the given character.
*
* @param str
* The source string.
* @param ch
* The target character.
* @return The converted string.
*/
public static String convert(String str, char ch) {
if (isNull(str)) {
return str;
}
int len = str.length();
StringBuffer buf = new StringBuffer(len + 1);
for (int i = 0; i < len; i++) {
buf.append(ch);
}
return buf.toString();
}
/**
* Replaces the given set of characters in the given string with a target
* character.
*
* @param str
* The source string.
* @param srcChars
* The set of characters to replace.
* @param tgtChar
* The target character.
* @return The converted string.
*/
public static String replace(String str, String srcChars, char tgtChar) {
for (int i = 0; i < srcChars.length(); i++) {
str = str.replace(srcChars.charAt(i), tgtChar);
}
return str;
}
/**
* Gets the integer value of the given string.
*
* @param str
* The source string.
* @param defValue
* The default integer value.
* @param The
* integer value of the given string.
*/
public static int getIntValue(String str, int defValue) {
if (StrUtil.isBlank(str)) {
return defValue;
}
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
return defValue;
}
}
/**
* Returns an array of bytes representing the UTF-8 encoding of the given
* string.
*
* @param str
* The source string.
* @return A byte array containing the UTF-8 encoding of the given string.
*/
public static byte[] getUTF8Bytes(String str) {
char[] c = str.toCharArray();
int len = c.length;
int count = 0;
for (int i = 0; i < len; i++) {
int ch = c[i];
if (ch <= 0x7f) {
count++;
} else if (ch <= 0x7ff) {
count += 2;
} else {
count += 3;
}
}
byte[] b = new byte[count];
int off = 0;
for (int i = 0; i < len; i++) {
int ch = c[i];
if (ch <= 0x7f) {
b[off++] = (byte) ch;
} else if (ch <= 0x7ff) {
b[off++] = (byte) ((ch >> 6) | 0xc0);
b[off++] = (byte) ((ch & 0x3f) | 0x80);
} else {
b[off++] = (byte) ((ch >> 12) | 0xe0);
b[off++] = (byte) (((ch >> 6) & 0x3f) | 0x80);
b[off++] = (byte) ((ch & 0x3f) | 0x80);
}
}
return b;
}
/**
* Returns the hexidecimal character representation for the given integer.
*
* @param value
* The integer value.
* @return The hex representation.
*/
private static char toHex(int value) {
return HEX_DIGITS[(value & 0xF)];
}
/**
* Returns the escaped Unicode string representation of the given Java
* string.
*
* @param str
* The Java string (encoded in UTF-16).
* @param skipAscii
* If true, avoid escaping the ASCII characters.
* @return The escaped Unicode string representation.
*/
public static String toEscapedUnicode(String str, boolean skipAscii) {
int len = str.length();
StringBuffer result = new StringBuffer(len * 2);
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (skipAscii && ch < 0x007E) {
result.append(ch);
} else {
result.append("\\u"); //$NON-NLS-1$
result.append(toHex((ch >> 12) & 0xF));
result.append(toHex((ch >> 8) & 0xF));
result.append(toHex((ch >> 4) & 0xF));
result.append(toHex(ch & 0xF));
}
}
return result.toString();
}
/**
* Convert List to String Array
*
* @param list
* @return String[]
*/
public static String[] convertListToStrArray(List list) {
if (list != null) {
int cnt = list.size();
String[] strArray = new String[cnt];
for (int i = 0; i < cnt; i++) {
String str = (String) list.get(i);
strArray[i] = new String(str);
}
return strArray;
} else {
return null;
}
}
public static String convertFirstLetterCase(String s, String[] keyWords) {
StringBuffer sb = new StringBuffer(s.substring(0, 1).toUpperCase()
+ s.substring(1).toLowerCase());
int foundIndex;
for (int i = 0; i < keyWords.length; i++) {
if ((foundIndex = s.indexOf(keyWords[i].toLowerCase(), 1)) != -1) {
sb.replace(foundIndex, foundIndex + keyWords[i].length(),
keyWords[i]);
}
}
//System.out.println(sb.toString());
return sb.toString();
}
}