Java StringBuilder charAt()方法与示例

StringBuilder类charAt()方法 (StringBuilder Class charAt() method)

  • charAt() method is available in java.lang package.

    charAt()方法在java.lang包中可用。

  • charAt() method is used to return the character value of the given index and array indexing starts from 0 (i.e. first character value will be located at index 0 and second character value will be located at index 1 and so..on).

    charAt()方法用于返回给定索引的字符值,数组索引从0开始(即第一个字符值位于索引0处,第二个字符值位于索引1处,依此类推)。

  • charAt() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    charAt()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • charAt() method may throw an exception at the time of returning the character of non-indexed.

    返回未索引字符时, charAt()方法可能会引发异常。

    IndexOutOfBoundsException - This exception may throw when the given argument is greater than the length or denotes negative value.

    IndexOutOfBoundsException-当给定参数大于长度或表示负值时,可能引发此异常。

Syntax:

句法:

    public char  charAt(int indices);

Parameter(s):

参数:

  • int indices – represents the index of the returned char value.

    int index –表示返回的char值的索引。

Return value:

返回值:

The return type of this method is char, it returns the char value of the given index.

此方法的返回类型为char ,它返回给定索引的char值。

Example:

例:

// Java program to demonstrate the example 
// of char  charAt(int indices) method of StringBuilder 

public class CharAt {
    public static void main(String[] args) {

        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java");
        System.out.println("st_b = " + st_b);

        // By using charAt(1) method to display the character of
        // given index 1 i.e. 'a'
        System.out.println("st_b.charAt(1) = " + st_b.charAt(1));

        // Creating another StringBuilder object
        st_b = new StringBuilder("Programming");
        System.out.println("st_b = " + st_b);

        // By using charAt(11) method throw an exception i.e.
        // no such index exists
        // System.out.println("st_b.charAt(11) = "+st_b.charAt(11));
    }
}

Output

输出量

st_b = Java
st_b.charAt(1) = a
st_b = Programming


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值