01-复习-字符串的获取

强调理解  类的概念 思考

String中的获取方法
public int length();  获取字符串中含有的字符个数,拿到字符串长度  str.length();
public String concat(String str) 字符串的拼接 也可以+  str0.concat(str1);   
public char charAt(int index);获取指定索引位置的单个字符 (索引从0开始)
public int indexOf(String str) 查找参数字符串当中首次出现的索引位置,如果没有返回-1值
package Demo01;

/*
字符串内容不会变感觉变了一定是创建新的了  ,字符串是常量
String中的获取方法
public int length();  获取字符串含有的字符个数
public String concat(String str) 拼接字符串  str0.concat(str1);
public char charAt(int index)l;获取指定索引位置的单个字符 (索引从0开始)
public int indexOf(String str) 查找参数字符串当中首次出现的索引位置,如果没有返回-1值
 */
public class 字符串的获取 {
    public static void main(String[] args) {
    int length = "sgdjkhgfkdfgj".length();
     System.out.println(length);

        //拼接字符串
        String str1 = "Hello";
        String str2 = "World";
        System.out.println(str1.concat(str2));  //  str1、2 都不动 拼接出得到了一个新的字符串
        //获取指定位置单个字符
        System.out.println(str1.charAt(0));  //H
        //查找参数字符串当中首次出现的索引位置  注意:第一次出现
        System.out.println("第一次索引值"+str1.indexOf('o'));  //注意:  char单个字符用单引号  4  

    }
}

猜你喜欢

转载自blog.csdn.net/shwjakak/article/details/120378337