简体   繁体   English

将逗号分隔的字符串拆分为多个新字符串

[英]Splitting Comma Delimited String into Multiple New Strings

I have a comma delimited string which I'd like to split (I know how this can be accomplished - there are plenty of resources on this) but then I'd like to take the new split values and use them as strings themselves. 我有一个逗号分隔的字符串,我想分割(我知道如何做到这一点-有很多资源),然后我想采用新的分割值并将其本身用作字符串。

For Example 例如

Oldstring = "user,game,demo,points,level"

NewString0 = user

NewString1 = game

NewString2 = demo 

NewString3 = points

NewString4 = level 

How can this be accomplished? 如何做到这一点?

String[] newStrings = oldstring.split(",")

Now use the array : 现在使用数组

NewString0 = newStrings[0];

NewString1 = newStrings[1];

NewString2 = newStrings[2];

NewString3 = newStrings[3];

NewString4 = newStrings[4];

You can make use of StringTokenizer . 您可以使用StringTokenizer

String newString[]=new String[10];
       StringTokenizer str=new StringTokenizer(oldString,",");
        for (int i = 0; str.hasMoreTokens(); i++) {
         newString[i]=str.nextToken();
        }

Hope it will be helpful. 希望对您有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM