What is the easiest/best/most correct way to iterate through the characters of a string in Java?

前端 未结 15 1501
挽巷
挽巷 2020-11-22 11:14

StringTokenizer? Convert the String to a char[] and iterate over that? Something else?

15条回答
  •  再見小時候
    2020-11-22 11:41

    I wouldn't use StringTokenizer as it is one of classes in the JDK that's legacy.

    The javadoc says:

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

提交回复
热议问题