10

字符串转化为数组

 4 years ago
source link: https://www.oschina.net/question/3955988_2320732
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

字符串转化为数组

超级玛丽 发布于 前天 14:46
阅读 103

input标签中获取的字符串"7,8,9",如何转换为数组[7,8,9]

tcxu

前天 16:21

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> 

up-c5fda0c49b065739955d855beabecbcc9e1.png


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK