getchars
字符串getChars()方法 (String getChars() Method)
getChars() is a String method in Java and it is used to get the number of characters from the given index from the string.
getChars()是Java中的String方法,用于从字符串的给定索引中获取字符数。
It extracts the part of the string and stores to the character array. If an index is of out of range – it returns an exception.
它提取字符串的一部分并存储到字符数组。 如果索引超出范围–它返回一个异常。
Syntax:
句法:
void source_string.getChars(
srcStartIndex,
srcEndIndex,
target_charArray,
targetStartIndex);
Here,
这里,
source_string is a string from where we have to get the characters.
source_string是我们必须从中获取字符的字符串。
srcStartIndex is the starting position in the source_string.
srcStartIndex是source_string中的起始位置。
srcEndIndex is the ending position in the source_string.
srcEndIndex是source_string中的结束位置。
target_charArray is the output/targeted array in which we have store the copied characters.
target_charArray是输出/目标数组,我们在其中存储了复制的字符。
targetStartIndex is the start position in the target_charArray.
targetStartIndex是target_charArray中的起始位置。
Example:
例:
Input:
str = "Welcome at IncludeHelp"
Function call:
input.getChars(11, 22, output_str, 0);
Output:
output_str = "IncludeHelp"
Java code to get the characters from a string using String.getChars() method
Java代码使用String.getChars()方法从字符串中获取字符
public class Main
{
public static void main(String[] args) {
String input = new String("Welcome at IncludeHelp");
char[] output = new char[11];
//here, we are going to get IncludeHelp from the String
//IncludeHelp is written from 11th
input.getChars(11, 22, output, 0);
System.out.println("input = " + input);
System.out.println("output = " + String.valueOf(output));
}
}
Output
输出量
input = Welcome at IncludeHelp
output = IncludeHelp
翻译自: https://www.includehelp.com/java/string-getChars-method-with-example.aspx
getchars