package com.byj.server.httpserver.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.byj.server.httpserver.Context;
public class StringUtils {
/**
* Converts a line of text into an array of lower case words. Words are
* delimited by the following characters: , .\r\n:/\+
* <p>
* In the future, this method should be changed to use a
* BreakIterator.wordInstance(). That class offers much more fexibility.
*
* @param text
* a String of text to convert into an array of words
* @return text broken up into an array of words.
*/
public static final String[] toLowerCaseWordArray(String text) {
if (text == null || text.length() == 0) {
return new String[0];
}
StringTokenizer tokens = new StringTokenizer(text, " ,\r\n.:/\\+");
String[] words = new String[tokens.countTokens()];
for (int i = 0; i < words.length; i++) {
words[i] = tokens.nextToken().toLowerCase();
}
return words;
}
/**
* Converts a line of text into an array of lower case words. Words are
* delimited by the following characters: , .\r\n:/\+
* <p>
* In the future, this method should be changed to use a
* BreakIterator.wordInstance(). That class offers much more fexibility.
*
* @param text
* a String of text to convert into an array of words
* @return text broken up into an array of words.
*/
public static final String[] toStringArray(String text) {
if (text == null || text.length() == 0) {
return new String[0];
}
StringTokenizer tokens = new StringTokenizer(text, ",\r\n/\\");
String[] words = new String[tokens.countTokens()];
for (int i = 0; i < words.length; i++) {
words[i] = tokens.nextToken();
}
return words;
}
/**
* * Converts a line of text into an array of lower case words. Words are
* delimited by the following characters: , .\r\n:/\+
* <p>
* In the future, this method should be changed to use a
* BreakIterator.wordInstance(). That class offers much more fexibility.
*
* @param text
* a String of text to convert into an array of words
* @param token
* String
* @return String[]broken up into an array of words.
*/
public static final String[] toStringArray(String text, String token) {
if (text == null || text.length() == 0) {
return new String[0];
}
StringTokenizer tokens = new StringTokenizer(text, token);
String[] words = new String[tokens.countTokens()];
for (int i = 0; i < words.length; i++) {
words[i] = tokens.nextToken();
}
return words;
}
/**
*
* @param source
* @return
*/
public static String[] splitOnWhitespace(String source) {
int pos = -1;
LinkedList<String> list = new LinkedList<String>();
int max = source.length();
for (int i = 0; i < max; i++) {
char c = source.charAt(i);
if (Character.isWhitespace(c)) {
if (i - pos > 1) {
list.add(source.substring(pos + 1, i));
}
pos = i;
}
}
return list.toArray(new String[list.size()]);
}
/**
* Replayer str
*
* @param str
* @param key
* @param replacement
* @return
*/
public static final String replaceAll(String str, String key,
String replacement) {
if (str != null && key != null && replacement != null
&& !str.equals("") && !key.equals("")) {
StringBuilder strbuf = new StringBuilder();
int begin = 0;
int slen = str.length();
int npos = 0;
int klen = key.length();
for (; begin < slen && (npos = str.indexOf(key, begin)) >= begin; begin = npos
+ klen) {
strbuf.append(str.substring(begin, npos)).append(replacement);
}
if (begin == 0) {
return str;
}
if (begin < slen) {
strbuf.append(str.substring(begin));
}
return strbuf.toString();
} else {
return str;
}
}
public static String UnicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
boolean hasU = false;
while (matcher.find()) {
hasU = true;
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
String s = str;
try{
if(!hasU){
int i = 0;
String rstr = "";
while(i+4<=str.length()){
ch = (char) Integer.parseInt(str.substring(i,i=i+4), 16);
rstr = rstr+ch;
}
str = rstr;
}
}catch(Exception ex){
str = s;
ex.printStackTrace();
}
return str;
}
/** 空字符串。 */
public static final String EMPTY_STRING = "";
/**
* 比较两个字符串(大小写敏感)。
* <pre>
* StringUtil.equals(null, null) = true
* StringUtil.equals(null, "abc") = false
* StringUtil.equals("abc", null) = false
* StringUtil.equals("abc", "abc") = true
* StringUtil.equals("abc", "ABC") = false
* </pre>
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
*
* @return 如果两个字符串相同,或者都是<code>null</code>,则返回<code>true</code>
*/
public static boolean equals(String str1, String str2) {
if (str1 == null) {
return str2 == null;
}
return str1.equals(str2);
}
/**
* 比较两个字符串(大小写不敏感)。
* <pre>
* StringUtil.equalsIgnoreCase(null, null) = true
* StringUtil.equalsIgnoreCase(null, "abc") = false
* StringUtil.equalsIgnoreCase("abc", null) = false
* StringUtil.equalsIgnoreCase("abc", "abc") = true
* StringUtil.equalsIgnoreCase("abc", "ABC") = true
* </pre>
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
*
* @return 如果两个字符串相同,或者都是<code>null</code>,则返回<code>true</code>
*/
public static boolean equalsIgnoreCase(String str1, String str2) {
if (str1 == null) {
return str2 == null;
}
return str1.equalsIgnoreCase(str2);
}
/**
* 检查字符串是否是空白:<code>null</code>、空字符串<code>""</code>或只有空白字符。
* <pre>
* StringUtil.isBlank(null) = true
* StringUtil.isBlank("") = true
* StringUtil.isBlank(" ") = true
* StringUtil.isBlank("bob") = false
* StringUtil.isBlank(" bob ") = false
* </pre>
*