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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StringBuildercharAt()方法是用来获取指定索引位置上的字符的。与直接使用字符串charAt()方法不同的是,需要先将字符串转换为StringBuilder对象,然后再调用charAt()方法来获取字符。 以下是一个使用charAt()方法示例代码: ```java // 创建一个StringBuilder对象 StringBuilder st_b = new StringBuilder("Java"); System.out.println("st_b = " + st_b); // 使用charAt(1)方法来获取索引为1的字符,即'a' System.out.println("st_b.charAt(1) = " + st_b.charAt(1)); // 创建另一个StringBuilder对象 st_b = new StringBuilder("Programming"); System.out.println("st_b = " + st_b); // 使用charAt(11)方法会抛出异常,因为索引为11的字符不存在 // System.out.println("st_b.charAt(11) = " + st_b.charAt(11)); ``` 在上面的示例中,首先创建了一个StringBuilder对象"Java",然后使用charAt(1)方法来获取索引为1的字符,即'a'。接着创建了另一个StringBuilder对象"Programming",但是如果使用charAt(11)方法来获取索引为11的字符,会抛出异常,因为索引为11的字符不存在。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java StringBuilder charAt()方法示例](https://blog.csdn.net/cumtv80668/article/details/107797308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [当charAt遇到StringBuilder(留档待解决)](https://blog.csdn.net/xlpda/article/details/128121825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值