有两方法:
1、用String的Split(str,",");
2、
字符串分割:
public int getCount(String str,String sign){//查找某一字符串中str,特定子串s的出现次数
if(str==null) return 0;
StringTokenizer s=new StringTokenizer(str,sign);
return s.countTokens();
}
public String[] getArray(String str,String sign){//按特定子串s为标记,将子串截成数组。
int count=getCount(str,sign);
int j=0;
String[] arr=new String[count];
for(int i=0;i<count;i++){
if(str.indexOf(sign)!=-1){
j =str.indexOf(sign);
arr[i]=str.substring(0,j);
str =str.substring(j+1);
}else{
arr[i]=str;
}
}
return arr;
1、用String的Split(str,",");
2、
字符串分割:
public int getCount(String str,String sign){//查找某一字符串中str,特定子串s的出现次数
if(str==null) return 0;
StringTokenizer s=new StringTokenizer(str,sign);
return s.countTokens();
}
public String[] getArray(String str,String sign){//按特定子串s为标记,将子串截成数组。
int count=getCount(str,sign);
int j=0;
String[] arr=new String[count];
for(int i=0;i<count;i++){
if(str.indexOf(sign)!=-1){
j =str.indexOf(sign);
arr[i]=str.substring(0,j);
str =str.substring(j+1);
}else{
arr[i]=str;
}
}
return arr;
解决方案 »
- 怎么让jsp中的所有的超链接失效
- jsp分页问题
- 小弟初学JAVA,关于servlet有个小问题请教
- jsp登陆问题?(给50分)
- java.lang.NullPointerException问题
- 关于表单的提问
- JSP页面报ClientAbortException, 关闭流还是会报该错,此时不关流对系统是否有影响 ?
- 请问中型企业开发web应用(广域网internet)用什么服务器(包括jsp容器和其他)比较好,够用就好
- 请问这句话什么意思 :ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
- APACHE和TOMCAT配置问题
- 函数定义问题
- jsp怎样才能访问sql server 2000数据库----------急急!!
这个class可以达到你想要的效果
但是不是一个API
而是好几个method一起应用
int a=st.countTokens();
String ss[]=new String[a];
int i=0;
while(st.hasMoreTokens()){
ss[i]=st.nextToken();
i++;
}
Class String
java.lang.Object
|
+-java.lang.String
split
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
NullPointerException - if regex is null
Since:
1.4
See Also:
Pattern
我用的是jdkl.4,但是,我几乎不用它。
结果,今天,看到这篇帖子,才知道,原来,居然有现成的方法~!
而我居然笨笨的自己去写了一个方法来实现它。
哎~!浪废了n多的脑细胞啊~!
import java.lang.String;
就可以完成
String[] ss = "123,45,7,8,9,33,4".split(",");
可惜我只有20分来发放,不好意思