应用String类中提供的startsWith()方法来实现,用于判断字符串是否以指定的前缀开始
public boolean startsWith(String prefix)
prefix:为指定的开始字符串,如果以prefix开头,方法返回值为true,否则返回false
在String类中还实现了一个同名的重载方法
判断字符串从指定索引开始的子字符串是否以指定前缀开始
public boolean startsWith(String prefix,int toffset)
prefix:为开始的字符串
toffset:为子字符串出现的索引位置
String str="abcdefg";
str.startsWith("c",2);
创建JavaBean类,判断字符串是否以指定的子字符串开头
public class StringUtil {
private String startStr ; //指定开头的字符串
private String str; //被判断的字符串
private boolean check; //判断结果
public String getStartStr() {
return startStr;
}
public void setStartStr(String startStr) {
this.startStr = startStr;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public boolean isCheck() {
//使用startsWith方法判断字符串是否以制定字符开头,如果是则返回ture,否则返回false
return str.startsWith(startStr);
}
}
创建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="check.jsp" method="post">
<table>
<tr>
<td align="right">请输入字符串:</td>
<td><input type="text" name="str" /></td>
</tr>
<tr>
<td align="right">请输入开头的字符串:</td>
<td><input type="text" name="startStr" /></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");
String startStr = request.getParameter("startStr");
%>
<!-- 使用useBean动作标签导入JavaBean对象 -->
<jsp:useBean id="strBean" class="com.cn.zj.bean.StringUtil"></jsp:useBean>
<!-- 对StringUtil类的str属性赋值 -->
<jsp:setProperty property="str" name="strBean" value="<%=str %>"/>
<!-- 对StringUtil类的startStr属性赋值 -->
<jsp:setProperty property="startStr" name="strBean" value ="<%=startStr %>"/>
<table>
<tr>
<td align="right">输入的字符串:</td>
<td>
<jsp:getProperty property="str" name="strBean"/>
</td>
</tr>
<tr >
<td align="right">开头的字符串:</td>
<td>
<jsp:getProperty property="startStr" name="strBean"/>
</td>
</tr>
<tr>
<td align="right">验证结果:</td>
<td>
<jsp:getProperty property="check" name="strBean"/>
</td>
</tr>
</table>
</body>
</html>