- 浏览: 469783 次
- 性别:
- 来自: 杭州
-
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
其实对字符串的处理,一般一个项目的core会建一个,最经典的算是commons-lang对他的封装。 其实我们可能不需要用到所有的方法,比如我们只是使用StringUtils.isBlank,就引入一个jar,那也太奢侈了,会增加项目的依赖,项目依赖越少越好,直到保持核心层最少依赖。 其实源码写的好,可以采用copy,reference等方式会更好。
package org.ycl.commons.text; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.text.StrBuilder; /** * this StringUtils contains all String process, may be copy from other jar, but * have linked it. * * Functions: * * 1. isBlank(String str) * <li>- checks if a String contains text</li> * 2. isNumeric(String str)/IsAlpha/isWhitespace * 3. getFromInputStream(InputStream is) * 4. Trim/Strip * <li>- removes leading and trailing whitespace</li> * 5. Equals/EqualsIgnoreCase * <li>- compares two strings null-safe</li> * 6. startsWith[ignoreCase] * <li>- check if a String starts with a prefix null-safe</li> * 7. endsWith[ignoreCase] * <li>- check if a String ends with a suffix null-safe</li> * 8. IndexOf/LastIndexOf/Contains * <li>- null-safe index-of checks</li> * 9. substring * <li>- null-safe substring extractions</li> * 10. Split/join * <li>- splits a String into an array of substrings and vice versa</li> * 11. deleteWhitespace * <li>- removes Whitespace of a String</li> * 12. capitalize/uncapitalize * <li>- changes the case of a String</li> * 13. defaultString * <li>- protects against a null input String</li> * 14. abbreviate * <li>- simple words for long words</li> * 15. getHtml/getHtmlAll/getHtmlContent/getHtmlContentAll/getHtmlContentAllGroup * <li>- use jdk regex to get content of html/xml</li> * <li>- this is simple get html content, {@link HttpClient}</li> * * NOTE:this is from my tool box * * {@link org.apache.commons.lang.StringUtils} * * Ref: * commons-lang-2.6.jar * commons-io-2.4.jar * * @author e557400 * */ public class StringUtils { public static boolean DEFAULT_DEBUG = true;// if DEBUG is true, will be print error message /** * {@link org.apache.commons.lang.StringUtils#isBlank(char)} * <p> * Checks if a String is whitespace, empty ("") or null. * </p> * * <pre> * StringUtils.isBlank(null) = true * StringUtils.isBlank("") = true * StringUtils.isBlank(" ") = true * StringUtils.isBlank("bob") = false * StringUtils.isBlank(" bob ") = false * </pre> * * @param str * the String to check, may be null * @return <code>true</code> if the String is null, empty or whitespace * @since 2.0 */ public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } /** * {@link org.apache.commons.lang.StringUtils#isNumeric(char)} * <p> * Checks if the String contains only unicode digits. A decimal point is not * a unicode digit and returns false. * </p> * * <p> * <code>null</code> will return <code>false</code>. An empty String * (length()=0) will return <code>true</code>. * </p> * * <pre> * StringUtils.isNumeric(null) = false * StringUtils.isNumeric("") = false * StringUtils.isNumeric(" ") = false * StringUtils.isNumeric("123") = true * StringUtils.isNumeric("12 3") = false * StringUtils.isNumeric("ab2c") = false * StringUtils.isNumeric("12-3") = false * StringUtils.isNumeric("12.3") = false * </pre> * * @param str * the String to check, may be null * @return <code>true</code> if only contains digits, and is non-null */ public static boolean isNumeric(String str) { if (isBlank(str)) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if (Character.isDigit(str.charAt(i)) == false) { return false; } } return true; } /** * use String pattern to check whether Str contains other charactor * * @param str * @return */ public static boolean isNumeric1(String str) { if (isBlank(str)) { return false; } Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (isNum.matches()) { return true; } else { return false; } } /** * use Char's ascii to check whether is Number * * @param str * @return */ public static boolean isNumeric2(String str) { if (isBlank(str)) { return false; } char[] chars = str.toCharArray(); for (char ch : chars) { if (ch < 48 || ch > 57) return false; } return true; } /** * Reader InputStream to String[Don't close InputStream] * * @param is * @return * @throws IOException */ public static String getFromInputStream(InputStream is) throws IOException { StringBuilder sb = new StringBuilder(); String line; BufferedReader br = new BufferedReader(new InputStreamReader(is)); boolean firstLine = true; while ((line = br.readLine()) != null) { if (firstLine) { sb.append(line); firstLine = false; } else { sb.append(System.getProperty("line.separator")); sb.append(line); } } return sb.toString(); } /** * Reader InputString to String[IOUtils],use default "file.encoding" * encoding.[Don't close InputStream] * * TIP: new InputStreamReader(input, Charsets.toCharset(encoding)); * * other Encoding: "UTF-8" "ISO8859-1" "GBK" "GB2312" * * @param is * @return * @throws IOException */ public static String getFromInputStream1(InputStream is) throws IOException { return IOUtils.toString(is); } /** * just remove ' ' * <p>Removes control characters (char <= 32) from both * ends of this String, handling <code>null</code> by returning * <code>null</code>.</p> * * <p>The String is trimmed using {@link String#trim()}. * Trim removes start and end characters <= 32. * To strip whitespace use {@link #strip(String)}.</p> * * <p>To trim your choice of characters, use the * {@link #strip(String, String)} methods.</p> * * <pre> * StringUtils.trim(null) = null * StringUtils.trim("") = "" * StringUtils.trim(" ") = "" * StringUtils.trim("abc") = "abc" * StringUtils.trim(" abc ") = "abc" * </pre> * * @param str the String to be trimmed, may be null * @return the trimmed string, <code>null</code> if null String input */ public static String trim(String str) { return str == null ? null : str.trim(); } /** * <p>Compares two Strings, returning <code>true</code> if they are equal.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case sensitive.</p> * * <pre> * 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 * </pre> * * @see java.lang.String#equals(Object) * @param str1 the first String, may be null * @param str2 the second String, may be null * @return <code>true</code> if the Strings are equal, case sensitive, or * both <code>null</code> */ public static boolean equals(String str1, String str2) { return str1 == null ? str2 == null : str1.equals(str2); } /** * <p>Compares two Strings, returning <code>true</code> if they are equal ignoring * the case.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered equal. Comparison is case insensitive.</p> * * <pre> * StringUtils.equalsIgnoreCase(null, null) = true * StringUtils.equalsIgnoreCase(null, "abc") = false * StringUtils.equalsIgnoreCase("abc", null) = false * StringUtils.equalsIgnoreCase("abc", "abc") = true * StringUtils.equalsIgnoreCase("abc", "ABC") = true * </pre> * * @see java.lang.String#equalsIgnoreCase(String) * @param str1 the first String, may be null * @param str2 the second String, may be null * @return <code>true</code> if the Strings are equal, case insensitive, or * both <code>null</code> */ public static boolean equalsIgnoreCase(String str1, String str2) { return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2); } /** * remove Whitespace. * * <p>Strips whitespace from the start and end of a String.</p> * * <p>This is similar to {@link #trim(String)} but removes whitespace. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p> * * <p>A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.strip(null) = null * StringUtils.strip("") = "" * StringUtils.strip(" ") = "" * StringUtils.strip("abc") = "abc" * StringUtils.strip(" abc") = "abc" * StringUtils.strip("abc ") = "abc" * StringUtils.strip(" abc ") = "abc" * StringUtils.strip(" ab c ") = "ab c" * </pre> * * @param str the String to remove whitespace from, may be null * @return the stripped String, <code>null</code> if null String input */ public static String strip(String str){ return org.apache.commons.lang.StringUtils.strip(str); } /** * <p>Check if a String starts with a specified prefix.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case sensitive.</p> * * <pre> * StringUtils.startsWith(null, null) = true * StringUtils.startsWith(null, "abc") = false * StringUtils.startsWith("abcdef", null) = false * StringUtils.startsWith("abcdef", "abc") = true * StringUtils.startsWith("ABCDEF", "abc") = false * </pre> * * @see java.lang.String#startsWith(String) * @param str the String to check, may be null * @param prefix the prefix to find, may be null * @return <code>true</code> if the String starts with the prefix, case sensitive, or * both <code>null</code> * @since 2.4 */ public static boolean startsWith(String str, String prefix) { if (str == null || prefix == null) { return (str == null && prefix == null); } if (prefix.length() > str.length()) { return false; } return str.startsWith(prefix); } /** * use JDK regex to check startsWith * * @param str * @param prefix * @return */ public static boolean startsWith1(String str, String prefix) { if (str == null || prefix == null) { return (str == null && prefix == null); } if (prefix.length() > str.length()) { return false; } String pattern = "^"+prefix+".*$"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); return m.matches(); } /** * <p>Check if a String ends with a specified suffix.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case sensitive.</p> * * <pre> * StringUtils.endsWith(null, null) = true * StringUtils.endsWith(null, "def") = false * StringUtils.endsWith("abcdef", null) = false * StringUtils.endsWith("abcdef", "def") = true * StringUtils.endsWith("ABCDEF", "def") = false * StringUtils.endsWith("ABCDEF", "cde") = false * </pre> * * @see java.lang.String#endsWith(String) * @param str the String to check, may be null * @param suffix the suffix to find, may be null * @return <code>true</code> if the String ends with the suffix, case sensitive, or * both <code>null</code> * @since 2.4 */ public static boolean endsWith(String str, String suffix) { if (str == null || suffix == null) { return (str == null && suffix == null); } if (suffix.length() > str.length()) { return false; } return str.endsWith(suffix); } /** * use JDK regex to check endsWith * * @param str * @param suffix * @return */ public static boolean endsWith1(String str, String suffix) { if (str == null || suffix == null) { return (str == null && suffix == null); } if (suffix.length() > str.length()) { return false; } String pattern = "^.*"+suffix+"$"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); return m.matches(); } /** * <p>Finds the first index within a String, handling <code>null</code>. * This method uses {@link String#indexOf(String)}.</p> * * <p>A <code>null</code> String will return <code>-1</code>.</p> * * <pre> * StringUtils.indexOf(null, *) = -1 * StringUtils.indexOf(*, null) = -1 * StringUtils.indexOf("", "") = 0 * StringUtils.indexOf("", *) = -1 (except when * = "") * StringUtils.indexOf("aabaabaa", "a") = 0 * StringUtils.indexOf("aabaabaa", "b") = 2 * StringUtils.indexOf("aabaabaa", "ab") = 1 * StringUtils.indexOf("aabaabaa", "") = 0 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @return the first index of the search String, * -1 if no match or <code>null</code> string input * @since 2.0 */ public static int indexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.indexOf(searchStr); } /** * use JDK regex to check indexOf [just the searchStr is pattern.] * * @param str * @param searchStr * @return */ public static int indexOf1(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } String pattern = searchStr; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); if(m.find()){ return m.start(); } return -1; } /** * use JDK regex group to check indexOf [just the searchStr is pattern.] * * @param str * @param searchStr * @return */ public static int indexOf2(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } String pattern = "("+searchStr+").*"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); while(m.find()){ if(DEFAULT_DEBUG){ System.out.println(m.group()+":"+m.start()+":"+m.end()+":("+m.groupCount()+")"); System.out.println(m.group(1)+":"+m.start(1)+":"+m.end(1)+":("+m.groupCount()+")"); } return m.start(); } return -1; } /** * <p>Finds the last index within a String, handling <code>null</code>. * This method uses {@link String#lastIndexOf(String)}.</p> * * <p>A <code>null</code> String will return <code>-1</code>.</p> * * <pre> * StringUtils.lastIndexOf(null, *) = -1 * StringUtils.lastIndexOf(*, null) = -1 * StringUtils.lastIndexOf("", "") = 0 * StringUtils.lastIndexOf("aabaabaa", "a") = 7 * StringUtils.lastIndexOf("aabaabaa", "b") = 5 * StringUtils.lastIndexOf("aabaabaa", "ab") = 4 * StringUtils.lastIndexOf("aabaabaa", "") = 8 * </pre> * * @param str the String to check, may be null * @param searchStr the String to find, may be null * @return the last index of the search String, * -1 if no match or <code>null</code> string input * @since 2.0 */ public static int lastIndexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.lastIndexOf(searchStr); } /** * use JDK regex to check lastindexOf [just the searchStr is pattern.] * * @param str * @param searchStr * @return */ public static int lastIndexOf1(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } String pattern = ".*("+searchStr+")"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); while(m.find()){ if(DEFAULT_DEBUG){ System.out.println(m.group()+":"+m.start()+":"+m.end()+":("+m.groupCount()+")"); System.out.println(m.group(1)+":"+m.start(1)+":"+m.end(1)+":("+m.groupCount()+")"); } return m.start(1); } return -1; } /** * <p>Checks if String contains a search character, handling <code>null</code>. * This method uses {@link String#indexOf(int)}.</p> * * <p>A <code>null</code> or empty ("") String will return <code>false</code>.</p> * * <pre> * StringUtils.contains(null, *) = false * StringUtils.contains("", *) = false * StringUtils.contains("abc", 'a') = true * StringUtils.contains("abc", 'z') = false * </pre> * * @param str the String to check, may be null * @param searchChar the character to find * @return true if the String contains the search character, * false if not or <code>null</code> string input * @since 2.0 */ public static boolean contains(String str, char searchChar) { if (isBlank(str)) { return false; } return str.indexOf(searchChar) >= 0; } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * * <p>A negative start position can be used to start <code>n</code> * characters from the end of the String.</p> * * <p>A <code>null</code> String will return <code>null</code>. * An empty ("") String will return "".</p> * * <pre> * StringUtils.substring(null, *) = null * StringUtils.substring("", *) = "" * StringUtils.substring("abc", 0) = "abc" * StringUtils.substring("abc", 2) = "c" * StringUtils.substring("abc", 4) = "" * StringUtils.substring("abc", -2) = "bc" * StringUtils.substring("abc", -4) = "abc" * </pre> * * @param str the String to get the substring from, may be null * @param start the position to start from, negative means * count back from the end of the String by this many characters * @return substring from start position, <code>null</code> if null String input */ public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return ""; } return str.substring(start); } /** * <p>Splits the provided text into an array, using whitespace as the * separator. * Whitespace is defined by {@link Character#isWhitespace(char)}.</p> * * <p>The separator is not included in the returned String array. * Adjacent separators are treated as one separator. * For more control over the split use the StrTokenizer class.</p> * * <p>A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.split(null) = null * StringUtils.split("") = [] * StringUtils.split("abc def") = ["abc", "def"] * StringUtils.split("abc def") = ["abc", "def"] * StringUtils.split(" abc ") = ["abc"] * </pre> * * @param str the String to parse, may be null * @return an array of parsed Strings, <code>null</code> if null String input */ public static String[] split(String str) { return org.apache.commons.lang.StringUtils.split(str); } /** * <p>Splits the provided text into an array, separator specified. * This is an alternative to using StringTokenizer.</p> * * <p>The separator is not included in the returned String array. * Adjacent separators are treated as one separator. * For more control over the split use the StrTokenizer class.</p> * * <p>A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.split(null, *) = null * StringUtils.split("", *) = [] * StringUtils.split("a.b.c", '.') = ["a", "b", "c"] * StringUtils.split("a..b.c", '.') = ["a", "b", "c"] * StringUtils.split("a:b:c", '.') = ["a:b:c"] * StringUtils.split("a b c", ' ') = ["a", "b", "c"] * </pre> * * @param str the String to parse, may be null * @param separatorChar the character used as the delimiter * @return an array of parsed Strings, <code>null</code> if null String input * @since 2.0 */ public static String[] split(String str, char separatorChar) { return org.apache.commons.lang.StringUtils.split(str,separatorChar); } /** * <p>Splits the provided text into an array, separators specified. * This is an alternative to using StringTokenizer.</p> * * <p>The separator is not included in the returned String array. * Adjacent separators are treated as one separator. * For more control over the split use the StrTokenizer class.</p> * * <p>A <code>null</code> input String returns <code>null</code>. * A <code>null</code> separatorChars splits on whitespace.</p> * * <pre> * StringUtils.split(null, *) = null * StringUtils.split("", *) = [] * StringUtils.split("abc def", null) = ["abc", "def"] * StringUtils.split("abc def", " ") = ["abc", "def"] * StringUtils.split("abc def", " ") = ["abc", "def"] * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"] * </pre> * * @param str the String to parse, may be null * @param separatorChars the characters used as the delimiters, * <code>null</code> splits on whitespace * @return an array of parsed Strings, <code>null</code> if null String input */ public static String[] split(String str, String separatorChars) { return org.apache.commons.lang.StringUtils.split(str,separatorChars); } /** * <p>Splits the provided text into an array, separator specified, * preserving all tokens, including empty tokens created by adjacent * separators. This is an alternative to using StringTokenizer.</p> * * <p>The separator is not included in the returned String array. * Adjacent separators are treated as separators for empty tokens. * For more control over the split use the StrTokenizer class.</p> * * <p>A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.splitPreserveAllTokens(null, *) = null * StringUtils.splitPreserveAllTokens("", *) = [] * StringUtils.splitPreserveAllTokens("a.b.c", '.') = ["a", "b", "c"] * StringUtils.splitPreserveAllTokens("a..b.c", '.') = ["a", "", "b", "c"] * StringUtils.splitPreserveAllTokens("a:b:c", '.') = ["a:b:c"] * StringUtils.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"] * StringUtils.splitPreserveAllTokens("a b c", ' ') = ["a", "b", "c"] * StringUtils.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", ""] * StringUtils.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", "", ""] * StringUtils.splitPreserveAllTokens(" a b c", ' ') = ["", a", "b", "c"] * StringUtils.splitPreserveAllTokens(" a b c", ' ') = ["", "", a", "b", "c"] * StringUtils.splitPreserveAllTokens(" a b c ", ' ') = ["", a", "b", "c", ""] * </pre> * * @param str the String to parse, may be <code>null</code> * @param separatorChar the character used as the delimiter, * <code>null</code> splits on whitespace * @return an array of parsed Strings, <code>null</code> if null String input * @since 2.1 */ public static String[] splitTokens(String str, String separatorChars) { return org.apache.commons.lang.StringUtils.splitPreserveAllTokens(str,separatorChars); } /** * Splits the provided text into an array, separator specified, preserving * all tokens, including empty tokens created by adjacent separators. * * CSVUtil.split(null, *, true) = null * CSVUtil.split("", *, , true) = [] * CSVUtil.split("a.b.c", '.', true) = ["a", "b", "c"] * CSVUtil.split("a...c", '.', true) = ["a", "", "", "c"] * CSVUtil.split("a...c", '.', false) = ["a", "c"] * * @param str * the string to parse * @param separatorChar * the seperator char * @param preserveAllTokens * if true, adjacent separators are treated as empty token * separators * @return the splitted string */ public static String[] splitPreserveAllTokens(String str, String separatorChars, boolean preserveAllTokens) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return new String[0]; } List<String> list = new ArrayList<String>(); int i = 0, start = 0; boolean match = false; boolean lastMatch = false; while (i < len) { if (separatorChars.indexOf(str.charAt(i))>=0) { if (match || preserveAllTokens) { list.add(str.substring(start, i)); list.add(String.valueOf(str.charAt(i)));//add token match = false; lastMatch = true; } start = ++i; continue; } lastMatch = false; match = true; i++; } if (match || preserveAllTokens && lastMatch) { list.add(str.substring(start, i)); } return list.toArray(new String[list.size()]); } /** * append Array's all element together, system.out is a good usage. * * <p>Joins the elements of the provided array into a single String * containing the provided list of elements.</p> * * <p>No separator is added to the joined String. * Null objects or empty strings within the array are represented by * empty strings.</p> * * <pre> * StringUtils.join(null) = null * StringUtils.join([]) = "" * StringUtils.join([null]) = "" * StringUtils.join(["a", "b", "c"]) = "abc" * StringUtils.join([null, "", "a"]) = "a" * </pre> * * @param array the array of values to join together, may be null * @return the joined String, <code>null</code> if null array input * @since 2.0 */ public static String join(Object[] array) { return org.apache.commons.lang.StringUtils.join(array); } /** * <p>Joins the elements of the provided array into a single String * containing the provided list of elements.</p> * * <p>No delimiter is added before or after the list. * A <code>null</code> separator is the same as an empty String (""). * Null objects or empty strings within the array are represented by * empty strings.</p> * * <pre> * StringUtils.join(null, *) = null * StringUtils.join([], *) = "" * StringUtils.join([null], *) = "" * StringUtils.join(["a", "b", "c"], "--") = "a--b--c" * StringUtils.join(["a", "b", "c"], null) = "abc" * StringUtils.join(["a", "b", "c"], "") = "abc" * StringUtils.join([null, "", "a"], ',') = ",,a" * </pre> * * @param array the array of values to join together, may be null * @param separator the separator character to use, null treated as "" * @return the joined String, <code>null</code> if null array input */ public static String join(Object[] array, String separator) { return org.apache.commons.lang.StringUtils.join(array,separator); } /** * <p>Deletes all whitespaces from a String as defined by * {@link Character#isWhitespace(char)}.</p> * * <pre> * StringUtils.deleteWhitespace(null) = null * StringUtils.deleteWhitespace("") = "" * StringUtils.deleteWhitespace("abc") = "abc" * StringUtils.deleteWhitespace(" ab c ") = "abc" * </pre> * * @param str the String to delete whitespace from, may be null * @return the String without whitespaces, <code>null</code> if null String input */ public static String deleteWhitespace(String str) { if (isBlank(str)) { return str; } int sz = str.length(); char[] chs = new char[sz]; int count = 0; for (int i = 0; i < sz; i++) { if (!Character.isWhitespace(str.charAt(i))) { chs[count++] = str.charAt(i); } } if (count == sz) { return str; } return new String(chs, 0, count); } /** * change first letter to Uppercase * * <p>Capitalizes a String changing the first letter to title case as * per {@link Character#toTitleCase(char)}. No other letters are changed.</p> * * <p>For a word based algorithm, see {@link WordUtils#capitalize(String)}. * A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.capitalize(null) = null * StringUtils.capitalize("") = "" * StringUtils.capitalize("cat") = "Cat" * StringUtils.capitalize("cAt") = "CAt" * </pre> * * @param str the String to capitalize, may be null * @return the capitalized String, <code>null</code> if null String input * @see WordUtils#capitalize(String) * @see #uncapitalize(String) * @since 2.0 */ public static String capitalize(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } return new StrBuilder(strLen) .append(Character.toTitleCase(str.charAt(0))) .append(str.substring(1)) .toString(); } /** * change first letter to Lowercase. * * <p>Uncapitalizes a String changing the first letter to title case as * per {@link Character#toLowerCase(char)}. No other letters are changed.</p> * * <p>For a word based algorithm, see {@link WordUtils#uncapitalize(String)}. * A <code>null</code> input String returns <code>null</code>.</p> * * <pre> * StringUtils.uncapitalize(null) = null * StringUtils.uncapitalize("") = "" * StringUtils.uncapitalize("Cat") = "cat" * StringUtils.uncapitalize("CAT") = "cAT" * </pre> * * @param str the String to uncapitalize, may be null * @return the uncapitalized String, <code>null</code> if null String input * @see WordUtils#uncapitalize(String) * @see #capitalize(String) * @since 2.0 */ public static String uncapitalize(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } return new StrBuilder(strLen) .append(Character.toLowerCase(str.charAt(0))) .append(str.substring(1)) .toString(); } /** * <p>Checks if the String contains only unicode letters.</p> * * <p><code>null</code> will return <code>false</code>. * An empty String (length()=0) will return <code>true</code>.</p> * * <pre> * StringUtils.isAlpha(null) = false * StringUtils.isAlpha("") = false * StringUtils.isAlpha(" ") = false * StringUtils.isAlpha("abc") = true * StringUtils.isAlpha("ab2c") = false * StringUtils.isAlpha("ab-c") = false * </pre> * * @param str the String to check, may be null * @return <code>true</code> if only contains letters, and is non-null */ public static boolean isAlpha(String str) { if (isBlank(str)) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if (Character.isLetter(str.charAt(i)) == false) { return false; } } return true; } /** * <p>Checks if the String contains only whitespace.</p> * * <p><code>null</code> will return <code>false</code>. * An empty String (length()=0) will return <code>true</code>.</p> * * <pre> * StringUtils.isWhitespace(null) = false * StringUtils.isWhitespace("") = true * StringUtils.isWhitespace(" ") = true * StringUtils.isWhitespace("abc") = false * StringUtils.isWhitespace("ab2c") = false * StringUtils.isWhitespace("ab-c") = false * </pre> * * @param str the String to check, may be null * @return <code>true</code> if only contains whitespace, and is non-null * @since 2.0 */ public static boolean isWhitespace(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; } /** * thougth it is easy , but it is usage in application normalrize. * * <p>Returns either the passed in String, * or if the String is <code>null</code>, an empty String ("").</p> * * <pre> * StringUtils.defaultString(null) = "" * StringUtils.defaultString("") = "" * StringUtils.defaultString("bat") = "bat" * </pre> * * @see ObjectUtils#toString(Object) * @see String#valueOf(Object) * @param str the String to check, may be null * @return the passed in String, or the empty String if it * was <code>null</code> */ public static String defaultString(String str) { return str == null ? "" : str; } /** * <p>Abbreviates a String using ellipses. This will turn * "Now is the time for all good men" into "Now is the time for..."</p> * * <p>Specifically: * <ul> * <li>If <code>str</code> is less than <code>maxWidth</code> characters * long, return it.</li> * <li>Else abbreviate it to <code>(substring(str, 0, max-3) + "...")</code>.</li> * <li>If <code>maxWidth</code> is less than <code>4</code>, throw an * <code>IllegalArgumentException</code>.</li> * <li>In no case will it return a String of length greater than * <code>maxWidth</code>.</li> * </ul> * </p> * * <pre> * StringUtils.abbreviate(null, *) = null * StringUtils.abbreviate("", 4) = "" * StringUtils.abbreviate("abcdefg", 6) = "abc..." * StringUtils.abbreviate("abcdefg", 7) = "abcdefg" * StringUtils.abbreviate("abcdefg", 8) = "abcdefg" * StringUtils.abbreviate("abcdefg", 4) = "a..." * StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException * </pre> * * @param str the String to check, may be null * @param maxWidth maximum length of result String, must be at least 4 * @return abbreviated String, <code>null</code> if null String input * @throws IllegalArgumentException if the width is too small * @since 2.0 */ public static String abbreviate(String str, int maxWidth) { return org.apache.commons.lang.StringUtils.abbreviate(str, maxWidth); } /** * get xml data with JDK regex. * * html format exp: * * 1. "<title>.*?</title>"; * * @param input * @param regex * @return */ public static String getHtml(String input,String regex){ List<String> htmls = new ArrayList<String>(); Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); Matcher ma = pa.matcher(input); while(ma.find()){ htmls.add(ma.group()); } return htmls.get(0); } /** * get xml data with JDK regex. * * html format exp: * * 1. "<title>.*?</title>"; * * @param input * @param regex * @return */ public static List<String> getHtmlAll(String input,String regex){ List<String> htmls = new ArrayList<String>(); Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); Matcher ma = pa.matcher(input); while(ma.find()){ htmls.add(ma.group()); } return htmls; } /** * get xml data with JDK regex. * * html format exp: * * 1. "<title>.*?</title>"; * * @param input * @param regex * @return */ public static String getHtmlContent(String input,String regex){ List<String> htmls = new ArrayList<String>(); Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); Matcher ma = pa.matcher(input); while(ma.find()){ htmls.add(ma.group(1)); } return htmls.get(0); } /** * get xml data with JDK regex. * * html format exp: * * 1. "<title>.*?</title>"; * * @param input * @param regex * @return */ public static List<String> getHtmlContentAll(String input,String regex){ List<String> htmls = new ArrayList<String>(); Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); Matcher ma = pa.matcher(input); while(ma.find()){ htmls.add(ma.group(1)); } return htmls; } /** * get xml data with JDK regex. * * html format exp: * * 1. "<title>.*?</title>"; * * @param input * @param regex * @return */ public static Map<Integer,List<String>> getHtmlContentAllGroup(String input,String regex){ Map<Integer,List<String>> htmls = new HashMap<Integer,List<String>>(); Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); Matcher ma = pa.matcher(input); int regroup = 0; while(ma.find()){ int groupindex = 1; while(groupindex<=ma.groupCount()){ List<String> groupvs = htmls.get(regroup); if(groupvs == null){ groupvs = new ArrayList<String>(); htmls.put(regroup, groupvs); } groupvs.add(ma.group(groupindex)); groupindex++; } regroup ++; } return htmls; } public static void main(String args[]){ System.out.println(indexOf2("abcdfwea","d"));//(d).* [dfwea/d] two group System.out.println(lastIndexOf1("abcdfwea","d"));//.*(d) [abcd/d] two group StringBuffer sb = new StringBuffer(); sb.append("<image>"); sb.append("<title>Yahoo! Weather</title>"); sb.append("<width>142</width>"); sb.append("<height>18</height>"); sb.append("<link>http://weather.yahoo.com</link>"); sb.append("<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>"); sb.append("</image>"); sb.append("<item>"); sb.append("<title>Conditions for Hangzhou, CH at 12:59 pm CST</title>"); sb.append("<geo:lat>30.23</geo:lat>"); System.out.println(getHtml(sb.toString(),"<title>(.*?)</title>"));//.*(d) [abcd/d] two group System.out.println(getHtmlContent(sb.toString(),"<title>(.*?)</title>"));//.*(d) [abcd/d] two group System.out.println(getHtml(sb.toString(),"(?<=<title>)(.*?)(?=</title>)"));//.*(d) [abcd/d] two group System.out.println(getHtml(sb.toString(),"(?<=<url>)(.*?)(?=</url>)"));//.*(d) [abcd/d] two group System.out.println(getHtml(sb.toString(),"<url>(.*?)</url>"));//.*(d) [abcd/d] two group System.out.println(getHtmlContent(sb.toString(),"<url>(.*?)</url>"));//.*(d) [abcd/d] two group System.out.println(getHtml(sb.toString(),"(<[^>]*>)"));//get html tag. System.out.println(getHtmlContent(sb.toString(),"<image>.*<url>(.*?)</url>.*</image>"));//get html tag. // System.out.println(split(null)); // System.out.println(split("")); // System.out.println(split("abc def")[0]); // System.out.println(split("abc def")[0]); // System.out.println(split(" abc ")[0]); // System.out.println(split(" abc ,bd,ss , a ,b",',')[0]); // System.out.println(split(" abc ,bd,ss , a ,b",',')[1]); // System.out.println(split(" abc ,bd,ss , a ,b",',')[2]); // System.out.println(split(" abc ,bd,ss , a ,b",',')[3]); // System.out.println(split(" abc ,bd,ss , a ,b",',')[4]); // // System.out.println(join(split(" abc ,bd,ss , a ,b",",\t\r\n\b"))); // System.out.println(Arrays.toString(split(" abc ,bd,ss , a ,b",", \t\r\n\b")));//\t\r\n\b // // System.out.println(abbreviate("fdsafafdsafdassdfa",4)); // System.out.println(abbreviate("fdsaffdsafaasdfa",6)); // System.out.println(abbreviate("fdsafdsaffasdfa",7)); // System.out.println(abbreviate("fdsafafdsafsdfa",9)); // System.out.println(abbreviate("fdsafafdsafsdfa",10)); // System.out.println(abbreviate("fdsafafdsaffdsfsdfsdfdssdfa",12)); // System.out.println(indexOf2("aacccdddcc", "c")); // System.out.println(indexOf1("aacccdddcc", "c")); // System.out.println(indexOf("aacccdddcc", "c")); } }
发表评论
-
commons-httpClient Helper
2016-09-27 19:27 836使用HttpClient来发送请求获取数据最经典,以下呢我们使 ... -
commons-httpClient Helper
2016-09-27 19:34 806使用HttpClient来发送请求获取数据最经典,以下呢我们使 ... -
commons-logging
2015-04-13 10:22 958我想,这个包是Apache开源里面用的最多的包,被各种开源使用 ... -
commons-io FileUtils Helper
2015-04-13 10:10 1919commons-io主要对输入流,输出流的打开和关闭,主要是对 ... -
commons-lang
2015-04-13 10:04 703这个包处了String和Number的封装,还有对Date, ... -
commons-lang NumberUtilHelper
2015-04-13 09:50 1306除了对字符串的复杂处理,对于数字,我们也是头大啊,类型,位移等 ... -
commons-dbutils Helper VS JDBCTemplate
2015-04-10 17:03 3831这两个JDBC轻量分装框架的确都是刚刚的。 但是相对来说co ... -
commons-dbutils Helper
2015-04-09 17:00 3190封装下dbutils, 使用这个help去操作数据库会非常的方 ... -
commons-dbutils
2015-04-09 11:26 1501现在ORM框架很多,什么Hibernate,ibatis等等。 ...
相关推荐
赠送jar包:commons-lang-2.6.jar; 赠送原API文档:commons-lang-2.6-javadoc.jar; 赠送源代码:commons-lang-2.6-sources.jar; 包含翻译后的API文档:commons-lang-2.6-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:commons-lang3-3.12.0.jar; 赠送原API文档:commons-lang3-3.12.0-javadoc.jar; 赠送源代码:commons-lang3-3.12.0-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.12.0.pom; 包含翻译后的API...
赠送jar包:commons-lang3-3.9.jar; 赠送原API文档:commons-lang3-3.9-javadoc.jar; 赠送源代码:commons-lang3-3.9-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.9.pom; 包含翻译后的API文档:...
赠送jar包:commons-lang3-3.10.jar; 赠送原API文档:commons-lang3-3.10-javadoc.jar; 赠送源代码:commons-lang3-3.10-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.10.pom; 包含翻译后的API文档:...
赠送jar包:commons-lang3-3.12.0.jar; 赠送原API文档:commons-lang3-3.12.0-javadoc.jar; 赠送源代码:commons-lang3-3.12.0-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.12.0.pom; 包含翻译后的API...
赠送jar包:commons-lang3-3.4.jar; 赠送原API文档:commons-lang3-3.4-javadoc.jar; 赠送源代码:commons-lang3-3.4-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.4.pom; 包含翻译后的API文档:...
赠送jar包:commons-lang3-3.7.jar; 赠送原API文档:commons-lang3-3.7-javadoc.jar; 赠送源代码:commons-lang3-3.7-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.7.pom; 包含翻译后的API文档:...
赠送jar包:commons-lang3-3.3.2.jar; 赠送原API文档:commons-lang3-3.3.2-javadoc.jar; 赠送源代码:commons-lang3-3.3.2-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.3.2.pom; 包含翻译后的API文档...
赠送jar包:commons-lang3-3.10.jar; 赠送原API文档:commons-lang3-3.10-javadoc.jar; 赠送源代码:commons-lang3-3.10-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.10.pom; 包含翻译后的API文档:...
赠送jar包:commons-lang-2.4.jar; 赠送原API文档:commons-lang-2.4-javadoc.jar; 赠送源代码:commons-lang-2.4-sources.jar; 赠送Maven依赖信息文件:commons-lang-2.4.pom; 包含翻译后的API文档:commons-...
赠送jar包:commons-lang3-3.5.jar; 赠送原API文档:commons-lang3-3.5-javadoc.jar; 赠送源代码:commons-lang3-3.5-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.5.pom; 包含翻译后的API文档:...
本资源包含了两个不同版本的Lang包:`commons-lang3-3.7.jar` 和 `commons-lang-2.6.jar`。这两个版本分别代表了Lang项目在不同时间点的发展状态,它们各自具有不同的特性和功能,适用于不同需求的项目。 `commons-...
commons-lang.jar是日常编程中必不可少的一个jar包,在日常java编程中,经常会使用到commons-lang.jar包,还没有准备commons-lang.jar包的用户可以前来下载使用,本站不仅为大家提供了commons-lang3-3.1.jar下载地址
开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-...
这个"commons-lang-2.4.rar"压缩包包含了两个子文件,分别是"commons-lang-2.4-bin.zip"和"commons-lang-2.4-src.zip"。 "commons-lang-2.4-bin.zip"文件是编译后的二进制版本,包含了commons-lang库的JAR文件。这...
commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包加源码
赠送jar包:commons-lang3-3.8.jar; 赠送原API文档:commons-lang3-3.8-javadoc.jar; 赠送源代码:commons-lang3-3.8-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.8.pom; 包含翻译后的API文档:...
commons-lang-1.0-b1.jar commons-lang-1.0.1.jar commons-lang-2.0.jar commons-lang-2.1-javadoc.jar commons-lang-2.1-sources.jar commons-lang-2.1.jar commons-lang-2.3.jar commons-lang-2.4-sources.jar ...
jar包大小:239KB commons-lang-2.3.jar jar包大小:237KB commons-lang-2.2.jar jar包大小:202KB commons-lang-2.1.jar jar包大小:165KB commons-lang-2.0.jar jar包大小:278KB commons-jxpath-1.2.jar jar包大小:129...
赠送jar包:commons-lang-2.6.jar; 赠送原API文档:commons-lang-2.6-javadoc.jar; 赠送源代码:commons-lang-2.6-sources.jar; 包含翻译后的API文档:commons-lang-2.6-javadoc-API文档-中文(简体)-英语-对照...