å¤æå符串æ¯ä¸æ¯æ°åï¼å¤§å®¶å¯è½ä¼ç¨ä¸äºjavaèªå¸¦çæ¹æ³ï¼ä¹æå¯è½ç¨å
¶ä»æªå¼çæå¼ï¼æ¯å¦å¤ææ¯ä¸æ¯æ´åæ°åï¼å°å符串强å¶è½¬æ¢ææ´åï¼ä¸æ¯æ°åçå°±ä¼æåºé误ï¼é£ä¹å°±ä¸æ¯æ´åçäºãä½æ¬æä»ç»çæ¯è¾å¥½ç两ç§æ¹æ³ï¼
1ãjavaç±»åºèªå¸¦çæ¹æ³ï¼
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}0202æ´æ°ï¼åç°ä»¥ä¸æ¹æ³åå¾ä¸å¤å°ä½ï¼ç°å¨å°±æ¹ä¸ºä¸é¢çç®å说æäºï¼è³äºå
·ä½çæ¹æ³å®ç°å符串å¤ææ¯å¦æ°åå°±ä¸åäºã
java.lang.Character.isDigit(char ch) boolean
isDigit åªè½ä½ç¨äºcharï¼æ以å¤æå符串æ¯å¦ä¸ºæ°åï¼è¦ä¸ä¸ªä¸ä¸ªæ¿åºcharè¿è¡å¤æã
2ãç¨æ£å表达å¼
é¦å
è¦import java.util.regex.Pattern å java.util.regex.Matcher
è¿ä¸¤ä¸ªå
ï¼æ¥ä¸æ¥æ¯ä»£ç
public boolean isNumeric(String str){Pattern pattern = Pattern.compile(â[0-9]*â);
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){return false;}return true;}02
3ãç¨æ£å表达å¼
温馨提示:答案为网友推荐,仅供参考