首页javastringJava Data Type - 如何从字符串获取每个字符

Java Data Type - 如何从字符串获取每个字符

我们想知道如何从字符串获取每个字符。

You can use the charAt() method to get a character at a particular index from a String object. The index starts at zero.

The following code prints the index value and the character at each index in a string of "JAVA2S.COM":

public class Main { public static void main(String[] args) { String str = "JAVA2S.COM"; // Get the length of string int len = str.length(); // Loop through all characters and print their indexes for (int i = 0; i < len; i++) { System.out.println(str.charAt(i) + " has index " + i); } } }