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
        }
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值