检查字符串是否包含英文字母
2018-08-18 本文已影响0人
神坛下的我
StringUtil7.java
public class StringUtil7 {
private String str;//要判断的字符串
private boolean hasEn;
private String cnStr;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public String getCnStr(){
char cArr[]=str.toCharArray();
StringBuffer sb=new StringBuffer("");
StringBuffer sb2=new StringBuffer("");
for(int i=0;i<cArr.length;i++){
int ascii=(int)cArr[i];//得到字符的ASCII码
if ((ascii>=65&&ascii<=90)||(ascii>=97&&ascii<=122)) {
sb.append(cArr[i]);//将英文字母添加到StringBuffer对象中
}else{
sb2.append(cArr[i]);
}
}
return sb2.toString();
}
public boolean isHasEn() {
char cArr[]=str.toCharArray();
StringBuffer sb=new StringBuffer("");
StringBuffer sb2=new StringBuffer("");
for(int i=0;i<cArr.length;i++){
int ascii=(int)cArr[i];//得到字符的ASCII码
if ((ascii>=65&&ascii<=90)||(ascii>=97&&ascii<=122)) {
sb.append(cArr[i]);//将英文字母添加到StringBuffer对象中
}else{
sb2.append(cArr[i]);
}
}
if(!sb.toString().equals("")) hasEn=true;
else hasEn=false;
return hasEn;
}
public void setHasEn(boolean hasEn) {
this.hasEn = hasEn;
}
}
index.jsp
<body>
<form action="result.jsp" method="post">
<table>
<tr>
<td align="right">请输入字符串:</td>
<td><input type="text" name="str" size="40"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="检查"/></td>
</tr>
</table>
</form>
</body>
result.jsp
<body>
<%
request.setCharacterEncoding("utf-8");
String str= request.getParameter("str");
%>
<jsp:useBean id="strBean" class="com.count.StringUtil7"></jsp:useBean>
<jsp:setProperty property="str" name="strBean" value="<%=str %>"/>
<table>
<tr>
<td>输入的字符串:</td>
<td align="left">
<jsp:getProperty property="str" name="strBean"/>
</td>
</tr>
<tr>
<td>是否包含英文字母:</td>
<td align="center">
<jsp:getProperty property="hasEn" name="strBean"/>
</td>
</tr>
<tr>
<td>去掉英文字母后的字符串:</td>
<td align="right">
<jsp:getProperty property="cnStr" name="strBean"/>
</td>
</tr>
</table>
</body>
4.PNG
5.PNG