简体   繁体   English

从字符串中获取一个字符

[英]get a character from a string

I have this string: 我有这个字符串:

String code="abc";

i want to extract "c" from this string by substring method but it doesn't work: 我想通过substring方法从这个字符串中提取“c”,但它不起作用:

String code="abc";
String getsmscode=code.substring(2,1);

This code return an error 此代码返回错误

java.lang.StringIndexOutOfBoundsException: length=3; regionStart=2; regionLength=-1

but i don't know why? 但我不知道为什么?

You need to read the documentation for substring - the second parameter isn't a length, it's the end index (exclusive). 您需要阅读substring的文档 - 第二个参数不是长度,它是结束索引(不包括)。

Parameters: 参数:
beginIndex - the beginning index, inclusive. beginIndex - 起始索引,包括。
endIndex - the ending index, exclusive. endIndex - 结束索引,独占。

When in doubt, read the docs... 如有疑问,请阅读文档......

Also note that if you're just trying to get the final character, you can just use the single-parameter overload, which returns a substring from a given start point to the end of the string: 另请注意,如果您只是尝试获取最终字符,则可以使用单参数重载,它会返回从给定起点到字符串末尾的子字符串:

String lastCharacter = text.substring(text.length() - 1);

Or you could get it as a single character: 或者你可以把它作为单个字符:

char lastCharacter = text.charAt(text.length() - 1);

第二个参数不是长度,它是结束索引

  String#substring(int beginIndex, int endIndex) 

The guys above have answered your question adequately but just a few tips on reading the API. 上面的人已经充分回答了你的问题,但只有一些关于阅读API的技巧。

Methods usually start with the word public and then include the return type... that is what type of data will be returned. 方法通常以单词public开头,然后包含返回类型...即返回的数据类型。 If nothing is expected to be returned is void. 如果预计不会返回任何内容,则无效。

Then comes the name of the method and two parenthesis. 然后是方法的名称和两个括号。

In between the parenthesis are the parameter type it accepts. 括号之间是它接受的参数类型。

with this particular method it accepts two integers. 使用这种特殊方法,它接受两个整数。 these help keep an index of how much of a string you'd like to keep 这些有助于保持您希望保留多少字符串的索引

index is an important concept to understand and it's hard to describe how it all works in words but looking at the examples in the api description listings help. 索引是一个需要理解的重要概念,很难用语言来描述它是如何工作的,但是看一下api描述列表帮助中的例子。

index-1 usually means counting starts at 0 so in the String "Hello World" H is indexed 0 and d is index 10. Notice there are only 9 letters but we still must include the space as a character so it's index in this case is 5. index-1通常意味着计数从0开始,因此在字符串“Hello World”中H被索引为0而d是索引10.注意,只有9个字母,但我们仍然必须将空格包含为字符,所以它的索引在这种情况下是5。

it's important to note the api mentions excludes and includes in the parameter descriptions. 重要的是要注意api提及排除和包含在参数说明中。 This just means the returned string will include the letter indexed by the first parameter but will not include the letter indexed by the second parameter. 这只意味着返回的字符串将包含由第一个参数索引的字母,但不包括由第二个参数索引的字母。

if all you need to do is extract the last letter in a string, no matter how long the string size, a simple solution is 如果所有你需要做的就是提取字符串中的最后一个字母,无论字符串大小有多长,一个简单的解决方案就是

stringName.substring(stringName.length()-1); 

The index start from 0. You can try 索引从0开始。你可以试试

String code="abc";
String getsmscode=code.substring(2);

I think substring takes a start index or a start index and end index as parameters: 我认为substring将起始索引或起始索引和结束索引作为参数:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

You've given it a start index greater than the end index, hence it is getting confused as the length returned is then negative. 你已经给它一个大于结束索引的起始索引,因此它会变得混乱,因为返回的长度是负的。 Swap the 1 and 2 around. 交换1和2左右。

Use can achieve this like also. 使用也可以实现这一点。

String getsmscode = code.substring(2,3);

Method Description 方法说明

public String substring (int start, int end)

Returns a string containing a subsequence of characters from this string. 返回包含此字符串中字符子序列的字符串。 The returned string shares this string's backing array. 返回的字符串共享此字符串的后备数组。

Parameters 参数

start - the offset of the first character. start - 第一个字符的偏移量。

end- the offset one past the last character. 结束 - 超过最后一个字符的偏移量。

Returns a new string containing the characters from start to end - 1 返回一个包含从头到尾的字符的新字符串 - 1

Throws 抛出

IndexOutOfBoundsException   if start < 0, start > end or end > length(). 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM