在字符串中除了a,b两个字符以外没有任何其他字符,返回TRUE,如何做呢
在字符串中除了a,b两个字符以外没有任何其他字符,返回TRUE,如何做呢
------解决方案--------------------将字符串转化为 字符数组,然后挨个比较必须是 a,或者 b
------解决方案--------------------	public static boolean testString(String str){ 
 		  if(str == null || str.length() == 0) 
 		    return true;   
 		  for(int i = 0; i  < str.length(); ++i) 
 		    if(str.charAt(i)  <  'a ' || str.charAt(i) >   'b ') 
 		      return false;   
 		  return true; 
 	} 
 这样OK吧?^_^
------解决方案--------------------正则表达式 
 public static boolean validate(String str){ 
     if(str.matches( "[ab]* "))return true; 
     return false; 
 }
------解决方案--------------------楼上方法正确,同意
------解决方案--------------------.....我的方法产生的结果跟eric1028完全一致,只是用了正则表达式而已,不知道你认为错在哪了?麻烦你测试后给出反例