charsetutils.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 418 行 · 第 1/2 页
JAVA
418 行
* and returns the number of characters present in the specified string.</p>
*
* <p>An example would be:</p>
* <ul>
* <li>count("hello", {"c-f", "o"}) returns 2.</li>
* </ul>
*
* @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to count characters in, may be null
* @param set String[] set of characters to count, may be null
* @return character count, zero if null string input
*/
public static int count(String str, String[] set) {
if (str == null || str.length() == 0 || set == null || set.length == 0) {
return 0;
}
CharSet chars = evaluateSet(set);
int count = 0;
char[] chrs = str.toCharArray();
int sz = chrs.length;
for(int i=0; i<sz; i++) {
if(chars.contains(chrs[i])) {
count++;
}
}
return count;
}
// Keep
//-----------------------------------------------------------------------
/**
* <p>Takes an argument in set-syntax, see evaluateSet,
* and keeps any of characters present in the specified string.</p>
*
* <pre>
* CharSetUtils.keep(null, *) = null
* CharSetUtils.keep("", *) = ""
* CharSetUtils.keep(*, null) = ""
* CharSetUtils.keep(*, "") = ""
* CharSetUtils.keep("hello", "hl") = "hll"
* CharSetUtils.keep("hello", "le") = "ell"
* </pre>
*
* @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to keep characters from, may be null
* @param set String set of characters to keep, may be null
* @return modified String, <code>null</code> if null string input
* @since 2.0
*/
public static String keep(String str, String set) {
if (str == null) {
return null;
}
if (str.length() == 0 || set == null || set.length() == 0) {
return "";
}
String[] strs = new String[1];
strs[0] = set;
return keep(str, strs);
}
/**
* <p>Takes an argument in set-syntax, see evaluateSet,
* and keeps any of characters present in the specified string.</p>
*
* <p>An example would be:</p>
* <ul>
* <li>keep("hello", {"c-f", "o"})
* returns "hll"</li>
* </ul>
*
* @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to keep characters from, may be null
* @param set String[] set of characters to keep, may be null
* @return modified String, <code>null</code> if null string input
* @since 2.0
*/
public static String keep(String str, String[] set) {
if (str == null) {
return null;
}
if (str.length() == 0 || set == null || set.length == 0) {
return "";
}
return modify(str, set, true);
}
// Delete
//-----------------------------------------------------------------------
/**
* <p>Takes an argument in set-syntax, see evaluateSet,
* and deletes any of characters present in the specified string.</p>
*
* <pre>
* CharSetUtils.delete(null, *) = null
* CharSetUtils.delete("", *) = ""
* CharSetUtils.delete(*, null) = *
* CharSetUtils.delete(*, "") = *
* CharSetUtils.delete("hello", "hl") = "hll"
* CharSetUtils.delete("hello", "le") = "ell"
* </pre>
*
* @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to delete characters from, may be null
* @param set String set of characters to delete, may be null
* @return modified String, <code>null</code> if null string input
*/
public static String delete(String str, String set) {
if (str == null || str.length() == 0 || set == null || set.length() == 0) {
return str;
}
String[] strs = new String[1];
strs[0] = set;
return delete(str, strs);
}
/**
* <p>Takes an argument in set-syntax, see evaluateSet,
* and deletes any of characters present in the specified string.</p>
*
* <p>An example would be:</p>
* <ul>
* <li>delete("hello", {"c-f", "o"}) returns
* "hll"</li>
* </ul>
*
* @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to delete characters from, may be null
* @param set String[] set of characters to delete, may be null
* @return modified String, <code>null</code> if null string input
*/
public static String delete(String str, String[] set) {
if (str == null || str.length() == 0 || set == null || set.length == 0) {
return str;
}
return modify(str, set, false);
}
//-----------------------------------------------------------------------
// Implementation of delete and keep
private static String modify(String str, String[] set, boolean expect) {
CharSet chars = evaluateSet(set);
StringBuffer buffer = new StringBuffer(str.length());
char[] chrs = str.toCharArray();
int sz = chrs.length;
for(int i=0; i<sz; i++) {
if(chars.contains(chrs[i]) == expect) {
buffer.append(chrs[i]);
}
}
return buffer.toString();
}
// Translate
//-----------------------------------------------------------------------
/**
* <p>Translate characters in a String.
* This is a multi character search and replace routine.</p>
*
* <p>An example is:</p>
* <ul>
* <li>translate("hello", "ho", "jy")
* => jelly</li>
* </ul>
*
* <p>If the length of characters to search for is greater than the
* length of characters to replace, then the last character is
* used.</p>
*
* <pre>
* CharSetUtils.translate(null, *, *) = null
* CharSetUtils.translate("", *, *) = ""
* </pre>
*
* @param str String to replace characters in, may be null
* @param searchChars a set of characters to search for, must not be null
* @param replaceChars a set of characters to replace, must not be null or empty ("")
* @return translated String, <code>null</code> if null string input
* @throws NullPointerException if <code>with</code> or <code>repl</code>
* is <code>null</code>
* @throws ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")
* @deprecated Use {@link StringUtils#replaceChars(String, String, String)}.
* Method will be removed in Commons Lang 3.0.
*/
public static String translate(String str, String searchChars, String replaceChars) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer buffer = new StringBuffer(str.length());
char[] chrs = str.toCharArray();
char[] withChrs = replaceChars.toCharArray();
int sz = chrs.length;
int withMax = replaceChars.length() - 1;
for(int i=0; i<sz; i++) {
int idx = searchChars.indexOf(chrs[i]);
if(idx != -1) {
if(idx > withMax) {
idx = withMax;
}
buffer.append(withChrs[idx]);
} else {
buffer.append(chrs[i]);
}
}
return buffer.toString();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?