Java String getChars()方法与示例
字符串getChars()方法
getChars()是Java中的String方法,用于从字符串的给定索引中获取字符数。
它提取字符串的一部分并存储到字符数组。如果索引超出范围–它返回一个异常。
语法:
void source_string.getChars(
srcStartIndex,
srcEndIndex,
target_charArray,
targetStartIndex);这里,
source_string是我们必须从中获取字符的字符串。
srcStartIndex是source_string中的起始位置。
srcEndIndex是source_string中的结束位置。
target_charArray是输出/目标数组,我们在其中存储了复制的字符。
targetStartIndex是target_charArray中的起始位置。
示例
Input:
str = "Welcome at NHOOO"
Function call:
input.getChars(11, 22, output_str, 0);
Output:
output_str = "NHOOO"Java代码使用String.getChars()方法从字符串中获取字符
public class Main
{
public static void main(String[] args) {
String input = new String("Welcome at NHOOO");
char[] output = new char[11];
//在这里,我们将从String中获取NHOOO-
//NHOOO写于11日
input.getChars(11, 22, output, 0);
System.out.println("input = " + input);
System.out.println("output = " + String.valueOf(output));
}
}输出结果
input = Welcome at NHOOO output = NHOOO