网上有很多正则表达式的规则之类的,就不发了,我也是在根据规则简单的写了点自己可能用到的一些,直接上代码实例,以下方法都是android自带的
/**
* 字符串是否为车牌号
* @param s
* @return
*/
public static boolean isPlate(String s)
{
if(s==null)
{
return false;
}
Pattern pattern = Pattern.compile("[\u4E00-\u9FA5][a-zA-Z][a-zA-Z0-9]{5}");
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
/**
* 字符串是否为纯数字
* @param s
* @return
*/
public static boolean isNumOnly(String s)
{
if(s==null)
{
return false;
}
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
/**
* 是否含有数字
* @param s
* @return
*/
public static boolean hasNum(String s)
{
if(s==null)
{
return false;
}
Pattern pattern = Pattern.compile(".*[0-9].*");
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
/**
* 除去字符串中所有的数字
* @param s
* @return
*/
public static String removeNums(String s)
{
if(s==null)
{
return "";
}
Pattern ptn = Pattern.compile("[0-9]+");
Matcher m = ptn.matcher(s);
return m.replaceAll("");
}
/**
* 只保留数字
* @param s
* @return
*/
public static String retainNumsOnly(String s)
{
if(s==null)
{
return "";
}
Pattern ptn = Pattern.compile("[^0-9]+");
Matcher m = ptn.matcher(s);
return m.replaceAll("");
}
/**
* 除去字符串中所有的汉字
* @param s
* @return
*/
public static String removeChn(String s)
{
if(s==null)
{
return "";
}
Pattern ptn = Pattern.compile("[\u4E00-\u9FA5]+");
Matcher m = ptn.matcher(s);
return m.replaceAll("");
}
/**
* 大写转小写(除去不相关字符)
* @return
*/
public static String Aa(String s)
{
if(s==null)
{
return "";
}
Pattern ptn = Pattern.compile("[^a-zA-Z]+");
Matcher m = ptn.matcher(s);
String ss=m.replaceAll("");
return ss.toLowerCase();
}
/**
* 小写转大写(除去不相关字符)
* @return
*/
public static String aA(String s)
{
if(s==null)
{
return "";
}
Pattern ptn = Pattern.compile("[^a-zA-Z]+");
Matcher m = ptn.matcher(s);
String ss=m.replaceAll("");
return ss.toUpperCase();
}
/**
* 把小写字母转为大写字母(不出去不相关字符)
* @param s
* @return
*/
public static String upperLetter(String s)
{
if(s==null)
{
return "";
}
return s.toUpperCase();
}
/**
* 把大写字母转为小写字母(不出去不相关字符)
* @param s
* @return
*/
public static String lowerLetter(String s)
{
if(s==null)
{
return "";
}
return s.toLowerCase();
}
}
上面都是自己一行行打的 有错误请指正