Java 文字、字串處理
清除空字串
str = str.replaceFirst(" ", "");// 前
str = str.replaceAll("\\s+$", "");// 後
str = str.trim();// 全
// The string is include number
private static boolean strIncludNumber(String str){
boolean hasNum = false;
if(str.matches(".*\\d+.*"))
hasNum = true;
return hasNum;
}
// The string is include english
private static boolean strIncludEnglish(String str){
boolean hasEng = false;
for(int i=0; i<str.length(); i++)
{
String test = str.substring(i, i+1);
if(test.matches("[a-zA-Z]+"))
hasEng = true;
}
return hasEng;
}
// The string is include chinese
private static boolean strIncludCheinese(String str){
boolean hasChinese = false;
for(int i=0; i<str.length(); i++)
{
String test = str.substring(i, i+1);
if(test.matches("[\\u4E00-\\u9FA5]+"))
hasChinese = true;
}
return hasChinese;
}
沒有留言:
張貼留言