日期:2014-05-17 浏览次数:20945 次
String s = "人数(5)"; int mynum; if("人数(".equals(s.substring(0, 3))){ String num[] = s.split("(")[1].split(")"); if(num.length>1&&!"0".equals(num[0].substring(0,1))){ try{ mynum = Integer.valueOf(num[0]); }catch(Exception e){ } } } System.out.println(mynum);
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <script type="text/javascript"> function check() { var a = document.getElementById('num').value; var v =/^人数\([1-9]\d*\)$/; var b = /\D/; if(v.test(a)){ alert('Yes'); a=a.substring(3,a.indexOf(")")); alert(a);} else{alert('No');} } </script> <body> <form id="form1" name="form1" method="post" action=""> <label> <input type="text" name="num" id="num" /> </label> <label> <input type="button" name="Submit" onclick="check()" value="提交" /> </label> </form> </body> </html>
------解决方案--------------------
String str1 ="人数(053)xxx(1234)"; String str2 = "人数(53)xxx(1234)"; String reg = "^人数\\([1-9]+\\d*\\).*$"; if(str1.matches(reg)){ System.out.println("字符串str1==>人数为:"+str1.substring(str1.indexOf("(")+1,str1.indexOf(")"))); }else{ System.out.println("字符串str1不符合规则!"); } if(str2.matches(reg)){ System.out.println("字符串str2==>人数为:"+str2.substring(str2.indexOf("(")+1,str2.indexOf(")"))); }else{ System.out.println("字符串str2不符合规则!"); }