简体   繁体   中英

How can I pick 11 numbers in a string to an array?

I have a string like this:

MyString="5,9,12,15,16,22,23,28,33,45,65";

I should love to get it into an array like this:

Myarray[0]="5";

Myarray[1]="9";

Myarray[2]="12";

Myarray[3]="15";

Myarray[4]="16";

Myarray[5]="22";

Myarray[6]="23";

Myarray[7]="28";

Myarray[8]="33";

Myarray[9]="45";

Myarray[10]="65";

Is there someway to do this in Java ?

String[] MyArray = MyString.split(",");

From your comments, it seems you love loops (just like me!), if so, you can use Java StringTokenizer :

StringTokenizer st = new StringTokenizer(myString, ",");
String[] myArray = new String[st.countTokens()];    
int i = 0;

while (st.hasMoreElements()) {
    myArray[i++] = st.nextElement();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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