通过索引获取字符串字符-Java

我知道如何计算字符串中某个字符或数字的索引,但是我可以使用任何预定义的方法在第n个位置给我该字符吗?因此,在字符串“ foo”中,如果我要求索引为0的字符,它将返回“ f”。

注意-在上述问题中,“字符”不是字符数据类型,而是字符串中的字母或数字。这里重要的是,调用该方法时,我不会收到char,而是一个字符串(长度为1)。而且我知道substring()方法,但是我想知道是否有更整洁的方法。


收到一只叮咚
浏览 1213回答 3
3回答

HUX布斯

您正在寻找的方法是charAt。这是一个例子:String text = "foo";char charAtZero = text.charAt(0);System.out.println(charAtZero); // Prints f有关更多信息,请参见上的Java文档String.charAt。如果想要另一个简单的教程,这一个或这一个。如果您不希望将结果作为char数据类型,而是作为字符串,则可以使用以下Character.toString方法:String text = "foo";String letter = Character.toString(text.charAt(0));System.out.println(letter); // Prints f如果您想要有关Character类和toString方法的更多信息,我从Character.toString的文档中提取了我的信息。

繁星点点滴滴

你要 .charAt()这是一个教程"mystring".charAt(2)退货 s如果您想拥有一个字符串,可以通过以下两种方法将字符转换为字符串:String mychar = Character.toString("mystring".charAt(2));要么String mychar = ""+"mystring".charAt(2);甚至String mychar = String.valueOf("mystring".charAt(2));例如。

Smart猫小萌

对于Unicode基本多义平面以外的字符编码的代理对,提出的答案均不适用。这是一个使用三种不同技术来迭代字符串的“字符”(包括使用Java 8流API)的示例。请注意,此示例包含Unicode补充多语言平面(SMP)的字符。您需要适当的字体才能正确显示此示例和结果。// String containing characters of the Unicode&nbsp;// Supplementary Multilingual Plane (SMP)// In that particular case, hieroglyphs.String str = "The quick brown ? jumps over the lazy ????";重复字符第一个解决方案是遍历所有char字符串的简单循环:/* 1 */System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; "\n\nUsing char iterator (do not work for surrogate pairs !)");for (int pos = 0; pos < str.length(); ++pos) {&nbsp; &nbsp; char c = str.charAt(pos);&nbsp; &nbsp; System.out.printf("%s ", Character.toString(c));&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Convert to String as per OP request}迭代代码点第二种解决方案也使用显式循环,但是使用codePointAt访问各个代码点,并根据charCount相应地增加循环索引:/* 2 */System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; "\n\nUsing Java 1.5 codePointAt(works as expected)");for (int pos = 0; pos < str.length();) {&nbsp; &nbsp; int cp = str.codePointAt(pos);&nbsp; &nbsp; char&nbsp; &nbsp; chars[] = Character.toChars(cp);&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Convert to a `char[]`&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;as code points outside the Unicode BMP&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;will map to more than one Java `char`&nbsp; &nbsp; System.out.printf("%s ", new String(chars));&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Convert to String as per OP request&nbsp; &nbsp; pos += Character.charCount(cp);&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; //&nbsp; &nbsp; Increment pos by 1 of more depending&nbsp; &nbsp; //&nbsp; &nbsp; the number of Java `char` required to&nbsp; &nbsp; //&nbsp; &nbsp; encode that particular codepoint.}使用Stream API遍历代码点第三种解决方案与第二种基本相同,但是使用的是Java 8 Stream API:/* 3 */System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; "\n\nUsing Java 8 stream (works as expected)");str.codePoints().forEach(&nbsp; &nbsp; cp -> {&nbsp; &nbsp; &nbsp; &nbsp; char&nbsp; &nbsp; chars[] = Character.toChars(cp);&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Convert to a `char[]`&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;as code points outside the Unicode BMP&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;will map to more than one Java `char`&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%s ", new String(chars));&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Convert to String as per OP request&nbsp; &nbsp; });结果运行该测试程序时,您将获得:Using char iterator (do not work for surrogate pairs !)T h e&nbsp; &nbsp;q u i c k&nbsp; &nbsp;b r o w n&nbsp; &nbsp;? ?&nbsp; &nbsp;j u m p s&nbsp; &nbsp;o v e r&nbsp; &nbsp;t h e&nbsp; &nbsp;l a z y&nbsp; &nbsp;? ? ? ? ? ? ? ?&nbsp;Using Java 1.5 codePointAt(works as expected)T h e&nbsp; &nbsp;q u i c k&nbsp; &nbsp;b r o w n&nbsp; &nbsp;?&nbsp; &nbsp;j u m p s&nbsp; &nbsp;o v e r&nbsp; &nbsp;t h e&nbsp; &nbsp;l a z y&nbsp; &nbsp;? ? ? ?&nbsp;Using Java 8 stream (works as expected)T h e&nbsp; &nbsp;q u i c k&nbsp; &nbsp;b r o w n&nbsp; &nbsp;?&nbsp; &nbsp;j u m p s&nbsp; &nbsp;o v e r&nbsp; &nbsp;t h e&nbsp; &nbsp;l a z y&nbsp; &nbsp;? ? ? ?&nbsp;如您所见(如果您能够正确显示象形文字),第一个解决方案将无法正确处理Unicode BMP之外的字符。另一方面,其他两个解决方案可以很好地处理代理对。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java