JavaScript中的indexOf
1、JavaScript中利用indexOf拼接字符串
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript中的indexOf</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">
<script type="text/javascript">
function strIndexOf()
{
var str = "水果,总数,总数|水果,苹果,总数|水果,苹果,红富士|树木,总数,总数|树木,松树,总数|树木,松树,红松";
var str1 = "";
var str2 = "";
var str3 = "";
var result = new Array();
result = str.split("|")
for(var i=0;i<result.length;i++)
{
if(result[i].indexOf(",总数,总数") > 0)
{
str1 += result[i] + "|";
}
else
{
if(result[i].indexOf(",总数") > 0)
{
str2 += result[i] + "|";
}
else
{
str3 += result[i] + "|";
}
}
}
str1 = str1.substring(0, str1.length - 1);
str2 = str2.substring(0, str2.length - 1);
str3 = str3.substring(0, str3.length - 1);
alert("str1:" + str1 + "
" + "str2:" + str2 + "
" + "str3:" + str3);
}
</script>
</head>
<body>
<input type="button" value="查询" οnclick="strIndexOf()"/>
</body>
</html>
2、运行结果