欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA中的charAt()和toCharArray()的用法

程序员文章站 2023-12-21 23:32:34
...

1、charAt()功能类似于数组,可以把字符串看作是char类型的数组,它是把字符串拆分获取其中的某个字符;返回指定位置的字符。
charAt(i),i为int类型,i从0开始。
例如:
String str01 = "hello123";
char c = str01.charAt(1);  //返回位置为1的字符
output:c=e
解析:类似于String [] str01 = {'h','e','l','l','o','1','2','3'};
 
 
2、toCharArray()的用法:将字符串对象中的字符转换为一个字符数组
例如:
public class Program
{
    public static void main(String[] args)
    {
        String str = "This is a String.";
        // Convert the above string to a char array.
        char[] arr = str.toCharArray();

        // Display the contents of the char array.
        System.out.println(arr);
    }
}

/*
Output:
This is a String.
*/
 
相关标签: charAt toCharArray

上一篇:

下一篇: