因此,在字符串"foo“中,如果我请求索引为0的字符,它将返回"f”。
发布于 2012-06-27 23:39:31
您要查找的方法是charAt
。下面是一个例子:
String text = "foo";
char charAtZero = text.charAt(0);
System.out.println(charAtZero); // Prints f
有关详细信息,请参阅Java documentation on String.charAt
。如果你想要另一个简单的教程,this one或this one。
如果不希望结果为char
数据类型,而是字符串,则可以使用Character.toString
方法:
String text = "foo";
String letter = Character.toString(text.charAt(0));
System.out.println(letter); // Prints f
如果您想了解更多关于Character
类和toString
方法的信息,我从the documentation on Character.toString获取了我的信息。
发布于 2012-06-27 23:39:25
你想要.charAt()
"mystring".charAt(2)
返回s
如果你执意要拥有一个字符串,有几种方法可以将字符转换为字符串:
String mychar = Character.toString("mystring".charAt(2));
或
String mychar = ""+"mystring".charAt(2);
甚至是
String mychar = String.valueOf("mystring".charAt(2));
例如。
发布于 2015-11-08 06:40:01
所有建议的答案都不适用于用于编码Unicode Basic Multiligual Plane之外的字符的代理项对。
下面的示例使用三种不同的技术来迭代字符串的“字符”(包括。使用Java8流API)。请注意,此示例包含Unicode补充多语言平面(SMP)的字符。您需要适当的字体才能正确显示此示例和结果。
// String containing characters of the Unicode
// Supplementary Multilingual Plane (SMP)
// In that particular case, hieroglyphs.
String str = "The quick brown jumps over the lazy ";
字符的迭代
第一个解决方案是在字符串的所有char
上执行一个简单循环:
/* 1 */
System.out.println(
"\n\nUsing char iterator (do not work for surrogate pairs !)");
for (int pos = 0; pos < str.length(); ++pos) {
char c = str.charAt(pos);
System.out.printf("%s ", Character.toString(c));
// ^^^^^^^^^^^^^^^^^^^^^
// Convert to String as per OP request
}
代码点的迭代
第二个解决方案也使用显式循环,但是使用codePointAt访问各个代码点,并根据charCount递增循环索引
/* 2 */
System.out.println(
"\n\nUsing Java 1.5 codePointAt(works as expected)");
for (int pos = 0; pos < str.length();) {
int cp = str.codePointAt(pos);
char chars[] = Character.toChars(cp);
// ^^^^^^^^^^^^^^^^^^^^^
// Convert to a `char[]`
// as code points outside the Unicode BMP
// will map to more than one Java `char`
System.out.printf("%s ", new String(chars));
// ^^^^^^^^^^^^^^^^^
// Convert to String as per OP request
pos += Character.charCount(cp);
// ^^^^^^^^^^^^^^^^^^^^^^^
// Increment pos by 1 of more depending
// the number of Java `char` required to
// encode that particular codepoint.
}
使用Stream API遍历代码点
第三种解决方案基本上与第二种解决方案相同,但使用的是Java 8 Stream API
/* 3 */
System.out.println(
"\n\nUsing Java 8 stream (works as expected)");
str.codePoints().forEach(
cp -> {
char chars[] = Character.toChars(cp);
// ^^^^^^^^^^^^^^^^^^^^^
// Convert to a `char[]`
// as code points outside the Unicode BMP
// will map to more than one Java `char`
System.out.printf("%s ", new String(chars));
// ^^^^^^^^^^^^^^^^^
// Convert to String as per OP request
});
结果
当您运行该测试程序时,您将获得:
Using char iterator (do not work for surrogate pairs !)
T h e q u i c k b r o w n ? ? j u m p s o v e r t h e l a z y ? ? ? ? ? ? ? ?
Using Java 1.5 codePointAt(works as expected)
T h e q u i c k b r o w n j u m p s o v e r t h e l a z y
Using Java 8 stream (works as expected)
T h e q u i c k b r o w n j u m p s o v e r t h e l a z y
正如您所看到的(如果您能够正确显示象形文字),第一个解决方案不能正确处理Unicode BMP之外的字符。另一方面,其他两种解决方案很好地处理了代理对。
https://stackoverflow.com/questions/11229986
复制相似问题