免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3715 | 回复: 5
打印 上一主题 下一主题

如何将一个字符串按“,”号分隔,存入数组中? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-04-04 10:18 |只看该作者 |倒序浏览
如何将一个字符串按“,”号分隔,存入数组中?

刚接触这东西,希望高手指点一下!

论坛徽章:
0
2 [报告]
发表于 2003-04-04 11:36 |只看该作者

如何将一个字符串按“,”号分隔,存入数组中?

这是我刚用java的时候自己写的Split函数,你看看可以用吗?
推荐你使用Apache的akata-oro包,它提供了很多类似Perl的
字符处理函数,也有Split函数。

public String[] Split(String strSource, String operator) {
        Hashtable  result = new Hashtable();
        Vector vTest = new Vector();

        int startIndex = 0;
        int i=0;
        String  thisStr = strSource;
        startIndex = strSource.indexOf(operator);
        if (startIndex == -1 ) {
            vTest.add(strSource);
        }else {
            while (startIndex != -1) {
                vTest.add(thisStr.substring(0,startIndex));
                thisStr = thisStr.substring(startIndex+1);
                startIndex = thisStr.indexOf(operator);
                i++;
            }
            vTest.add(thisStr.substring(startIndex + operator.length()));
        }

        String[] sRet = new String[vTest.size()];
        vTest.copyInto(sRet);
        return sRet;
    }

论坛徽章:
0
3 [报告]
发表于 2003-04-04 12:26 |只看该作者

如何将一个字符串按“,”号分隔,存入数组中?

刚才贴的比较乱,重发一个。

public  String[] Split(String strSource, String operator) {

        Vector vTest = new Vector();
        int startIndex = 0;
        int i=0;
        startIndex = strSource.indexOf(operator);
        if (startIndex == -1 ) {
            vTest.add(strSource);
        }else {
            String  thisStr = strSource;
            while (startIndex != -1) {
                vTest.add(thisStr.substring(0,startIndex));
                thisStr = thisStr.substring(startIndex+1);
                startIndex = thisStr.indexOf(operator);
                i++;
            }
            vTest.add(thisStr.substring(startIndex + operator.length()));
        }

        String[] sRet = new String[vTest.size()];
        vTest.copyInto(sRet);
        return sRet;
    }

论坛徽章:
0
4 [报告]
发表于 2003-04-04 14:16 |只看该作者

如何将一个字符串按“,”号分隔,存入数组中?

用JSDK1.4的新包java.util.regex

论坛徽章:
0
5 [报告]
发表于 2003-04-04 21:17 |只看该作者

如何将一个字符串按“,”号分隔,存入数组中?

java.util.StringTokenizer stk=new java.util.StringTokenizer(string_input,",";
int count=stk.countTokens() ;
String[] result=(count>;0)?new String[count]:null;
int i=0;
while(count>;0 && stk.hasMoreTokens());{
  result[i++]=stk.nextToken();
}

论坛徽章:
0
6 [报告]
发表于 2003-04-05 09:22 |只看该作者

如何将一个字符串按“,”号分隔,存入数组中?

多谢各位!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP