input标签中获取的字符串如何转换为数组?
input标签中获取的字符串"7,8,9",如何转换为数组[7,8,9]
回答
1. 用 java:
public class T {
public static void main(String args[]) {
String data ="7,8,9";
String dat [] = data.split(",");
System.out.println("字符串数组");
for (int i=0; i<dat.length;i++)
System.out.print(dat[i] + " ");
System.out.println("\n整型数组");
int dataI [] = new int[dat.length];
for (int i=0; i<dat.length;i++){
dataI[i] =Integer.parseInt(dat[i]);
System.out.print(dataI[i] + " ");
}
}
}
DOS 输出:
D:\java\test\OS_China>java T
字符串数组
7 8 9
整型数组
7 8 9
D:\java\test\OS_China>
2. 用 js
<html>
<body>
</body>
<script language="javascript" type="text/javascript">
data = '7,8,9';
dat = data.split(',');
s = '';
for( i=0; i<dat.length;i++)
s += dat[i] + ' ';
document.write(s);
</script>
</html>
js用arrary.push
js用split分割就行了
console.log("7,8,9".split(","))
====>["7", "8", "9"]
var text = "1,2,3,4,5";
var array = text.split(",");