在Java中,charAt()
方法用于从字符串中获取指定索引位置的字符
String str = "Hello, World!";
char[] charArray = str.toCharArray();
char ch = charArray[index];
substring()
方法和charAt()
方法组合:String str = "Hello, World!";
int index = 5;
char ch = str.substring(index, index + 1).charAt(0);
codePointAt()
方法(适用于Unicode字符):String str = "Hello, World!";
int index = 5;
int codePoint = str.codePointAt(index);
char ch = (char) codePoint;
请注意,这些替代方案可能不适用于所有情况。在处理包含Unicode字符的字符串时,建议使用codePointAt()
方法。