String 类 常用方法

一、判断功能的方法

  • public boolean equlals (Object anObject)

:将此字符串与指定对象进行比较。

  • public boolean equlalsIgnoreCase (String anotherString)

:将此字符串与指定对象进行比较,忽略大小写。

方法演示,代码如下:

package String;  
  
public class StringTest3 {  
    public static void main(String[] args) {  
        String s1 = "hello";  
        String s2 = "hello";  
        String s3 = "HEllo";  
        String s4 = new String("hello");  
        System.out.println(s1 == s2);// true  
        System.out.println(s1.equals(s2));// true  
        // 只要内存地址一样  两个变量的内容肯定一样  反过来不成立  
        String s5 = "he";  
        s5 = s5+"llo";  // hello  
        System.out.println(s5.equals(s1));// true  
        System.out.println(s5 == s1);// false  
        System.out.println(s1.equalsIgnoreCase(s3));//忽略大小写的比较 true  
        System.out.println(s1.equals(s3));// false  
        System.out.println(s1.equals(s4));// true  
    }  
}

二、获取功能的方法

在这里插入图片描述

方法演示,代码如下:

package String;  
  
public class StringTest4 {  
    public static void main(String[] args) {  
        String s = "helloworld";  
        // 获取字符串长度也就是字符个数  int[] arr = new arr[10];  arr.legth        System.out.println(s.length());  
  
        // 将制定的字符串连接到该字符串的末尾  
        s = s.concat("666");// s+"666"  
        System.out.println(s);//helloworld666  
  
        // 获取指定索引处的字符  
        char c = s.charAt(5);// 获取第六个字符  
        System.out.println(c);// w  
  
        // 在字符串对象中第一次出现的索引 , 没有的话返回值为-1  
        int a = s.indexOf("ello");// 从第几个位置开始出现这个字符串  
        System.out.println(a);// 1  
  
        // String substring(int start) 从start处开始截取字符串到字符串结尾  
        String b = s.substring(1);// 从第二个字符 一直 截取到最后一个字符  
        System.out.println(b);// elloworld  
        // String substring(int start,int end) 从start到end截取字符串 含start 不含end  
        String d = s.substring(1,5);// 从第二个字符 一直 截取到底四个字符 (1<=d<5)  
        String e = s.substring(1,s.length()-1);// 从第二个字符 一直 截取到 倒数第二个个字符 (1<=e<12)  
        System.out.println(d);// ello  
        System.out.println(e);// elloworld66  
  
    }  
}

三、转换功能方法

在这里插入图片描述

方法演示,代码如下:

package String;  
  
public class StringTest6 {  
    public static void main(String[] args) {  
  
    String str = "abcde";  
    // 将 str字符串 转换为 字符数组  
    char[] c = str.toCharArray();  
        for (int i = 0; i < c.length; i++) {  
            System.out.print(c[i]);// a b c d e  
        }  
        // 将 str字符串 转换为 字节数组  
        byte[] bytes = str.getBytes();  
        for (int i = 0; i < bytes.length; i++) {  
            System.out.print(bytes[i]);// 97 98 99 100 101  
        }  
        // 替换 将字符串中的字符替换  将 c 替换为 w        
        String s = str.replace('c','w');  
        System.out.println(s);// abwde  
    }  
  
}

四、分割功能的方法
public String[] split(String regex)
:将此字符串按照给定的regex(规则)拆分为字符串数组。

方法演示,代码如下:

package String;  
  
public class StringTest9 {  
    public static void main(String[] args) {  
        String s = "aa-bb-cc-dd";  
        String[] strArray = s.split("-");//[aa,bb,cc,dd]  
        System.out.println(s.split("-"));// 打印出地址  
        for (int i = 0;i < strArray.length;i++){  
            System.out.println(strArray[i]);// aa bb cc dd  
        }  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂神我的神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值