Java里常用的字符串方法的使用

方法 功能
equals 比较两个字符串是否相同
length 求字符长度
concat 字符拼接
charAt 获取指定字符索引位置的字符
indexOf 查找字符第一次出现位置
substring 截取字符串中的一段字符
toCharArray 将字符串转化为字符数组
replace 将字符串中的某段字符串替换为另一串字符串
split 对一串字符串进行切割

1.从char数组到String类型的转换

方法实现: String 变量=new String(char数组名称)

public class Main {
    public static void main(String[] args) {
        String str1 = new String();
        char[] charArray = {'a', 'b', 'c'};
        String str2 = new String(charArray);
        System.out.println(str2);//abc

    }
}

2.equals方法的使用

方法实现: 字符串1.equals(字符串2)
如果字符串1和字符串2相同,则返回true,否则返回false
注意: equals区分英文大小写,若想让其不区分大小写应用equalsIgnoreCase

public class Main {
    public static void main(String[] args) {
        String str1="hello";
        String str2="Hello";
        char []charArrey={'h','e','l','l','o'};
        String str3=new String(charArrey);
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true
        System.out.println(str1.equals(str3));//true
        }
}

3.length方法的使用

方法实现: 字符串.length

public class Main {
    public static void main(String[] args) {
        String str1 ="hello world";
        int length=str1.length();
        System.out.println(length);//11
    }
}

4.concat方法的使用

方法实现: 字符串1.concat(字符串2)

public class Main {
    public static void main(String[] args) {
        String str1 ="hello ";
        String str2="world";
       String str3=str1.concat(str2);
        System.out.println(str3);//hello world
    }
}

5.charAt方法的使用

方法实现 字符串1.charAt(索引值位置)

public class Main {
    public static void main(String[] args) {
        String str1 ="hello ";
        System.out.println(str1.charAt(4));//o
    }
}

6.indexOf方法的使用

方法实现: 字符串.indexOf(需查找的字符串)
注意: 若能找到则返回位置,查不到则返回-1

public class Main {
    public static void main(String[] args) {
        String str1 ="hello world";
        System.out.println(str1.indexOf("world"));//6
        System.out.println(str1.indexOf("abc"));//-1
    }
}

7.substring方法的使用

方法实现: 1.字符串.substring(索引值位置)
2.字符串.substring(开始截取位置,结束位置)

public class Main {
    public static void main(String[] args) {
        String str1="hello world";
        String str2=str1.substring(6);//world
        String str3=str1.substring(2,8);//llo wo
        System.out.println(str2);
        System.out.println(str3);
    }
}

8.toCharArray方法的使用

方法实现: 字符串.toCharArray()

public class Main {
    public static void main(String[] args) {
        String str1 = "hello";
        char[] chars = str1.toCharArray();
        System.out.println(chars[1]);//e
    }
}

9.replace方法的使用

方法实现: 1.字符串.replace(原来的字符,替换的字符)
2.字符串.replace(原来的字符串,替换的字符串)

public class Main {
    public static void main(String[] args) {
        String str1 = "I Love You";
        String str2=str1.replace("u","u too");//I Love You too
        System.out.println(str2);
    }
}

10.split方法的使用

方法实现: 字符串.split(切割字符)
注意: 若要以’.'为切割字符,则添加到split中时需加两个反斜杠

public class Main {
    public static void main(String[] args) {
        String str1 = "aaa,bbb,ccc,d";
        String[] array1 = str1.split(",");
        for (int i = 0; i < array1.length; i++) {
        System.out.print(array1[i]);//aaabbbcccd
        }
    }
 }
发布了1 篇原创文章 · 获赞 1 · 访问量 38

猜你喜欢

转载自blog.csdn.net/qq_45659384/article/details/104078750