检查字符串是否包含数字

在这里插入图片描述在这里插入图片描述
创建JavaBean类

public class StringUtil3 {
	private String  str;		//要检查的字符串
	private boolean hasNum;		//是否包含数字
	private String othersStr;	//去掉数字后的字符串
	public String getOthersStr() {
		return othersStr;
	}
	public void setOthersStr(String othersStr) {
		this.othersStr = othersStr;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public boolean isHasNum() {
		char cArr[] = str.toCharArray();//将字符串转换为字符数组
		StringBuffer sbNum =new StringBuffer("");
		StringBuffer sbOthers =new StringBuffer("");
		for(int i=0;i<cArr.length;i++){
			//调用String类的hashCode()方法也能获得字符的ASCII码
			//int ascii = String.valueOf(cArr[i]).hashCode();
			int ascii = (int)cArr[i];//强制转换可以直接得到字符的ASCII码
			//数字的 ASCII码范围在48-57之间
			if(ascii>=48&&ascii<=57){
				sbNum.append(cArr[i]);//将每个数字添加到StringBuffer对象中
			}
			else{
				sbOthers.append(cArr[i]);
			}
		}
		this.setOthersStr(sbOthers.toString());
		if(!sbNum.toString().equals(""))
			hasNum=true;
		else
			hasNum=false;
		return hasNum;
	}
	public void setHasNum(boolean hasNum) {
		this.hasNum = hasNum;
	}
}

创建index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>判断字符串是否包含数字</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
		table{
			border: 1px solid;
			border-color: green;
			font-family:华文细黑;
			font-size: 13px;
			color:gray;
			
		}
		input{
			font-family:华文细黑;
			font-size: 13px;
			color:gray;
		}
	</style>
  </head>
  
  <body>
  
  	<form action="check3.jsp" method="post">
     <table>
     	<tr>
     		<td align="right">请输入字符串:</td>
     		<td><input type="text" name="str"  size="30"/></td>
     	</tr> 
     	<tr>
     		<td colspan="2" align="center"><input type="submit" value="检  查" /></td>
     	</tr>
     </table>	
     </form>
  </body>
</html>

创建check.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>检查</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
		table{
			border: 1px solid;
			border-color: green;
			color: green;
			font-size: 13px;
			font-family: 华文细黑;
		}
	</style>
  </head>
  <body>
   	<%
   		String str = request.getParameter("str");
  	 %>
  	 <!-- 使用useBean动作标签导入JavaBean对象 -->
  	<jsp:useBean id="strBean" class="com.cn.zj.bean.StringUtil3"></jsp:useBean>
  	 <!-- 对StringUtil类的str属性赋值 -->
  	<jsp:setProperty property="str" name="strBean" value="<%=str %>"/>
    <table>
    	<tr>
			<td align="right">输入的字符串:</td>
			<td>
				<!-- 从StringUtil对象中获得str的属性值 -->	
				<jsp:getProperty property="str" name="strBean"/>
			</td>
		</tr>
		<tr>
			<td align="right">是否包含数字:</td>
			<td>	
				<!-- 从StringUtil对象中获得hasNum的属性值 -->	
				<jsp:getProperty property="hasNum" name="strBean"/>
			</td>		
		</tr>
		<tr>
			<td align="right">去掉数字后的字符串:</td>
			<td>	
				<!-- 从StringUtil对象中获得othersStr的属性值 -->	
				<jsp:getProperty property="othersStr" name="strBean"/>
			</td>
		</tr>
 	</table>	
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>