Splitting a space separated list

前端 未结 8 1092
暖寄归人
暖寄归人 2021-01-04 09:01

This is a common task I\'m facing: splitting a space separated list into a head element and an array containing the tail elements. For example, given this

8条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 09:30

    Use StringTokenizer and a while loop to step through each element. Inside the loop, you can get the first element and put the rest into an array.

    Edit: Oh, I guess StringTokenizer is a "legacy" class (though it still works).

    The recommended way is now to use String.split(). That will give you a String[] containing your elements. From there it should be trivial to get the first element and also create an array out of the remaining elements.

提交回复
热议问题