通过String类的split()方法来实现将字符串转换为数组。split()方法包含一个String类型·的参数regex,调用时会以regex为字符串的分隔符,将字符串分隔为字符数组。
创建StringUtil的JavaBean类,实现将字符串转换为数组
public class StringUtil4 {
private String str; //要分隔的字符串
private String strArr[]; //分隔后的字符串数组
private String listSeparator; //分隔符号
public StringUtil4(){} //默认构造方法
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
/**返回分隔符*/
public String getListSeparator() {
return listSeparator;
}
/**设置分隔符*/
public void setListSeparator(String listSeparator) {
this.listSeparator = listSeparator;
}
/**返回字符串数组*/
public String[] getStrArr() {
return str.split(listSeparator); //根据分隔符号分隔字符串为数组
}
}
创建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">
<style type="text/css">
table{
border: 1px solid;
border-color: green;
font-size: 12px;
}
input {
font-size: 12px;
color: gray;
}
textarea{
font-size: 12px;
color: gray;
}
</style>
<script type="text/javascript">
function getSelectCheckbox(){
var checkObj = document.getElementsByName("like"); //获得复选框对象 ,该对象是一个数组
var likeStr ="";
for(var i=0;i < checkObj.length ;i++){
if(checkObj[i].checked==true){ //如果某个复选框为选中状态
likeStr+=checkObj[i].value+","; //将选中的复选框值组合,并以","分隔
}
}
//将组合后复选框的值赋给隐藏域,表单 提交后,获得的是该隐藏域的值
document.getElementById("likes").value = likeStr ;
}
</script>
</head>
<body>
<form action="toarray.jsp" method="post">
<input type="hidden" name = "likes" id="likes" />
<table width="220">
<tr bgcolor="skyblue">
<td align="center">您喜欢的运动有:š</td></tr>
<tr>
<td>
<input type="checkbox" name="like" value="打篮球">打篮球
<input type="checkbox" name="like" value="踢足球">踢足球
<input type="checkbox" name="like" value="打乒乓球">打乒乓球
<input type="checkbox" name="like" value="跑步">跑步
<input type="checkbox" name="like" value="打羽毛球">打羽毛球
<input type="checkbox" name="like" value="游泳">游泳
</td>
</tr>
<tr bgcolor="skyblue">
<td align="center"><input type="submit" value="提 交" onclick="getSelectCheckbox()" /></td>
</tr>
</table>
</form>
</body>
</html>
创建toarray.jsp文件,处理表单请求,调用JavaBean方法将字符串转换为数组,然后将数组中的元素显示在表格中
<%@ 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: 12px;
}
</style>
</head>
<body>
<%
String likes = request.getParameter("likes");//从请求中获取以","分隔的字符串
%>
<!-- 使用useBean动作标签导入JavaBean对象 -->
<jsp:useBean id="strBean" class="com.cn.zj.bean.StringUtil4"></jsp:useBean>
<!-- 对StringUtil类的对象strBean的str属性赋值 -->
<jsp:setProperty property="str" name="strBean" value="<%=likes %>"/>
<!-- 对StringUtil类的对象strBean的listSeparator属性赋值 -->
<jsp:setProperty property="listSeparator" name="strBean" value=","/>
<%
String likeArr[] = strBean.getStrArr();//分隔后的字符串数组
%>
<table>
<tr >
<td width="100" valign="top">您选择的运动有:</td>
<%
for(int i = 0;i<likeArr.length;i++){
%>
<td align="left">
【<%=likeArr[i] %>】
</td>
<%} %>
</tr>
</table>
</body>
</html>