string charat_Java String charAt()方法与示例

string charat

字符串charAt()方法 (String charAt() Method)

charAt() method is a String class method in Java, it is used to get the character from specified index from a given string.

charAt()方法是Java中的String类方法,用于从给定字符串的指定索引中获取字符。

Syntax:

句法:

    char String.charAt(index);

It accepts an index and returns the characters from the specified position of the String.

它接受索引并从String的指定位置返回字符。

Note: If the index is out of range, the method returns "String index out of range" exception.

注意:如果索引超出范围,则该方法将返回“字符串索引超出范围”异常。

Example:

例:

    Input:
    str = "includeHelp"
    Function call:
    str.charAt(0)

    Output:
    "i"

Code:

码:

public class Main
{
    public static void main(String[] args) {
        String str = "includeHelp";
        System.out.println("character at 0 index: " + str.charAt(0));
        System.out.println("character at 1 index: " + str.charAt(1));
        System.out.println("character at 2 index: " + str.charAt(2));
        System.out.println("character at 3 index: " + str.charAt(3));
        System.out.println("character at 4 index: " + str.charAt(4));
        System.out.println("character at 5 index: " + str.charAt(5));
        System.out.println("character at 6 index: " + str.charAt(6));
        System.out.println("character at 7 index: " + str.charAt(7));
        System.out.println("character at 8 index: " + str.charAt(8));
        System.out.println("character at 9 index: " + str.charAt(9));
        System.out.println("character at 10 index: " + str.charAt(10));
    }
}

Output

输出量

character at 0 index: i
character at 1 index: n
character at 2 index: c
character at 3 index: l
character at 4 index: u
character at 5 index: d
character at 6 index: e
character at 7 index: H
character at 8 index: e
character at 9 index: l
character at 10 index: p


翻译自: https://www.includehelp.com/java/string-charAt-method-with-example.aspx

string charat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值