📄 stringutils.java.svn-base
字号:
/*
* Copyright 2002-2006 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in
* writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing
* permissions and limitations under the License.
*/
package com.easyjf.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
/**
* Miscellaneous string utility methods. Mainly for internal
* use within the framework; consider Jakarta's Commons Lang
* for a more comprehensive suite of string utilities.
*
* <p>
* This class delivers some simple functionality that should
* really be provided by the core Java String and
* StringBuffer classes, such as the ability to replace all
* occurrences of a given substring in a target string. It
* also provides easy-to-use methods to convert between
* delimited strings, such as CSV strings, and collections
* and arrays.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Keith Donald
* @author Rob Harrop
* @since 16 April 2001
* @see org.apache.commons.lang.StringUtils
*/
public abstract class StringUtils {
private static final String FOLDER_SEPARATOR = "/";
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
private static final String TOP_PATH = "..";
private static final String CURRENT_PATH = ".";
private static final char EXTENSION_SEPARATOR = '.';
// ---------------------------------------------------------------------
// General convenience methods for working with Strings
// ---------------------------------------------------------------------
/**
* Check if a String has length.
* <p>
*
* <pre>
* StringUtils.hasLength(null) = false
* StringUtils.hasLength("") = false
* StringUtils.hasLength(" ") = true
* StringUtils.hasLength("Hello") = true
* </pre>
*
* @param str
* the String to check, may be
* <code>null</code>
* @return <code>true</code> if the String is not null
* and has length
*/
public static boolean hasLength(String str) {
return (str != null && str.length() > 0);
}
/**
* Check if a String has text. More specifically,
* returns <code>true</code> if the string not
* <code>null<code>, it's <code>length is > 0</code>, and
* it has at least one non-whitespace character.
* <p><pre>
* StringUtils.hasText(null) = false
* StringUtils.hasText("") = false
* StringUtils.hasText(" ") = false
* StringUtils.hasText("12345") = true
* StringUtils.hasText(" 12345 ") = true
* </pre>
* @param str the String to check, may be <code>null</code>
* @return <code>true</code> if the String is not null, length > 0,
* and not whitespace only
* @see java.lang.Character#isWhitespace
*/
public static boolean hasText(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return false;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
/**
* Trim leading and trailing whitespace from the given
* String.
*
* @param str
* the String to check
* @return the trimmed String
* @see java.lang.Character#isWhitespace
*/
public static String trimWhitespace(String str) {
if (!hasLength(str)) {
return str;
}
StringBuffer buf = new StringBuffer(str);
while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
buf.deleteCharAt(0);
}
while (buf.length() > 0
&& Character.isWhitespace(buf.charAt(buf.length() - 1))) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
/**
* Trim leading whitespace from the given String.
*
* @param str
* the String to check
* @return the trimmed String
* @see java.lang.Character#isWhitespace
*/
public static String trimLeadingWhitespace(String str) {
if (!hasLength(str)) {
return str;
}
StringBuffer buf = new StringBuffer(str);
while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
buf.deleteCharAt(0);
}
return buf.toString();
}
/**
* Trim trailing whitespace from the given String.
*
* @param str
* the String to check
* @return the trimmed String
* @see java.lang.Character#isWhitespace
*/
public static String trimTrailingWhitespace(String str) {
if (!hasLength(str)) {
return str;
}
StringBuffer buf = new StringBuffer(str);
while (buf.length() > 0
&& Character.isWhitespace(buf.charAt(buf.length() - 1))) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
/**
* Test if the given String starts with the specified
* prefix, ignoring upper/lower case.
*
* @param str
* the String to check
* @param prefix
* the prefix to look for
* @see java.lang.String#startsWith
*/
public static boolean startsWithIgnoreCase(String str, String prefix) {
if (str == null || prefix == null) {
return false;
}
if (str.startsWith(prefix)) {
return true;
}
if (str.length() < prefix.length()) {
return false;
}
String lcStr = str.substring(0, prefix.length()).toLowerCase();
String lcPrefix = prefix.toLowerCase();
return lcStr.equals(lcPrefix);
}
/**
* Test if the given String ends with the specified
* suffix, ignoring upper/lower case.
*
* @param str
* the String to check
* @param suffix
* the suffix to look for
* @see java.lang.String#endsWith
*/
public static boolean endsWithIgnoreCase(String str, String suffix) {
if (str == null || suffix == null) {
return false;
}
if (str.endsWith(suffix)) {
return true;
}
if (str.length() < suffix.length()) {
return false;
}
String lcStr = str.substring(str.length() - suffix.length())
.toLowerCase();
String lcSuffix = suffix.toLowerCase();
return lcStr.equals(lcSuffix);
}
/**
* Count the occurrences of the substring in string s.
*
* @param str
* string to search in. Return 0 if this is
* null.
* @param sub
* string to search for. Return 0 if this is
* null.
*/
public static int countOccurrencesOf(String str, String sub) {
if (str == null || sub == null || str.length() == 0
|| sub.length() == 0) {
return 0;
}
int count = 0, pos = 0, idx = 0;
while ((idx = str.indexOf(sub, pos)) != -1) {
++count;
pos = idx + sub.length();
}
return count;
}
/**
* Replace all occurences of a substring within a string
* with another string.
*
* @param inString
* String to examine
* @param oldPattern
* String to replace
* @param newPattern
* String to insert
* @return a String with the replacements
*/
public static String replace(String inString, String oldPattern,
String newPattern) {
if (inString == null) {
return null;
}
if (oldPattern == null || newPattern == null) {
return inString;
}
StringBuffer sbuf = new StringBuffer();
// output StringBuffer we'll build up
int pos = 0; // our position in the old string
int index = inString.indexOf(oldPattern);
// the index of an occurrence we've found, or -1
int patLen = oldPattern.length();
while (index >= 0) {
sbuf.append(inString.substring(pos, index));
sbuf.append(newPattern);
pos = index + patLen;
index = inString.indexOf(oldPattern, pos);
}
sbuf.append(inString.substring(pos));
// remember to append any characters to the right of
// a match
return sbuf.toString();
}
/**
* Delete all occurrences of the given substring.
*
* @param pattern
* the pattern to delete all occurrences of
*/
public static String delete(String inString, String pattern) {
return replace(inString, pattern, "");
}
/**
* Delete any character in a given string.
*
* @param charsToDelete
* a set of characters to delete. E.g. "az\n"
* will delete 'a's, 'z's and new lines.
*/
public static String deleteAny(String inString, String charsToDelete) {
if (inString == null || charsToDelete == null) {
return inString;
}
StringBuffer out = new StringBuffer();
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
if (charsToDelete.indexOf(c) == -1) {
out.append(c);
}
}
return out.toString();
}
// ---------------------------------------------------------------------
// Convenience methods for working with formatted
// Strings
// ---------------------------------------------------------------------
/**
* Quote the given String with single quotes.
*
* @param str
* the input String (e.g. "myString")
* @return the quoted String (e.g. "'myString'"), or
* <code>null<code> if the input was <code>null</code>
*/
public static String quote(String str) {
return (str != null ? "'" + str + "'" : null);
}
/**
* Turn the given Object into a String with single
* quotes if it is a String; keeping the Object as-is
* else.
*
* @param obj
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -