String 类

目录

1.常用方法

1.1字符串的构造

1.2字符串的比较

1.==

2.equals

3. int compareTo(String s) 

4. int compareToIgnoreCase(String str)

1.3字符串的查找

1.4转换

1.4.1数值与字符串之间的转换

1.4.2大小写转换

1.4.3字符串转数组

1.4.4格式化

1.5字符串的替换

1.6字符串的拆分

1.7字符串的截取

1.8其它字符串操作


1.常用方法

1.1字符串的构造

下面三种方式

public class Test {
    public static void main(String[] args) {

        //字符串的构造有下面三种方法
       // 第一种 使用常量串构造
        String s1="gnjdj";
        System.out.println(s1);

        //第二种直接newString对象
        String s2=new String("fkdsjfak");
        System.out.println(s2);

        //第三种使用字符数组进行构造
        char [] arrays={'f','f','g','i'};
        String s3=new String (arrays);
        System.out.println(s3);


    }
}

注意:

1.String是引用类型,内部并不存字符串本身(存的是地址)在String类的实现源码中,String类实例变量如下

2. 在Java中“”引起来的也是String类型对象。

1.2字符串的比较

一共有四种方式

1.==

注意:对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址。

注意最下面的那种情况

2.equals

字典序:字符大小的顺序

String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照 如下规则进行比较,比如: s1.equals(s2)

    public boolean equals(Object anObject) {
// 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true
        if (this == anObject) {
            return true;
        }
// 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false
        if (anObject instanceof String) {
// 将anObject向下转型为String类型对象
            String anotherString = (String)anObject;
            int n = value.length;
// 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
// 4. 按照字典序,从前往后逐个字符进行比较
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
}

我们使用equals比较是不需要重写方法(直接用比较内容)

3. int compareTo(String s) 

按照字典序进行比较

与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:

1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值

2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值

public class Test3 {
    public static void main(String[] args) {

        //字符串个数相同情况下
        String s1="a";
        String s2="e";
        int num=s2.compareTo(s1);
        System.out.println(num);
        //小写a与小写e之间差了4
        System.out.println("-------------------");

        String s3="A";
        String s4="a";
        int nom=s3.compareTo(s4);
        System.out.println(nom);
        //大写A与小写a之间差了32
        System.out.println();
        System.out.println("-----------------");

        //字符串个数不同情况下
        String s5="abc";
        String s6="abcdef";
        System.out.println(s5.compareTo(s6));
        //如果前面字符相同,返回的是两个字符串相差的个数
        System.out.println("---------------------------");


        //字符串个数不同,内容也不相同
            String s7="abc";
            String s8="abeef";
        System.out.println(s7.compareTo(s8));
        //返回的是第一个开始不同的俩个字符的差值


    }
}

4. int compareToIgnoreCase(String str)

方法:与compareTo方式相同,但是忽略大小写比较

1.3字符串的查找

public class Test5 {
    public static void main(String[] args) {
        //字符串的查找
        String s="aaabbbcccaaabbbccc";
        //1.char charAt(int index)
        //返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常
        System.out.println(s.charAt(0));//a
        //2.int indexOf(int ch)
        // 返回ch第一次出现的位置,没有返回-1
        System.out.println(s.indexOf('c'));//6
        //3.int indexOf(int ch, intfromIndex)
        //从fromIndex位置开始找ch第一次出现的位置,没有返回-1
        System.out.println(s.indexOf('a',3));//9
        //4.int indexOf(String str)
        // 返回str第一次出现的位置,没有返回-1
        System.out.println(s.indexOf("bc"));//5
        //5.int indexOf(String str, intfromIndex)
        //从fromIndex位置开始找str第一次出现的位置,没有返回-1
        System.out.println(s.indexOf("bc",6));//14
        //6.int lastIndexOf(int ch)
        // 从后往前找,返回ch第一次出现的位置,没有返回-1
        System.out.println(s.lastIndexOf('a'));//11
        //7.int lastIndexOf(int ch, intfromIndex)
        //从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
        System.out.println(s.lastIndexOf('a',3));//2
        //8.int lastIndexOf(String str)
        // 从后往前找,返回str第一次出现的位置,没有返回-1
        System.out.println(s.lastIndexOf("bc"));//14
        //9.int lastIndexOf(String str, intfromIndex)
        //从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1
        System.out.println(s.lastIndexOf("bc",6));//5
    }
}

1.4转换

1.4.1数值与字符串之间的转换

class Studay{
    public   String name;
    public   int age;

    public Studay(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Studay{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test6 {

    public static  void main(String[] args) {

        //数值转字符串
        String s1=String.valueOf(123);
        String s2=String.valueOf(12.34);
        String s3=String.valueOf(true);
        String s4=String.valueOf(new Studay("fjj",11));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println("--------------------");
        //字符串转数值
        // 注意:Integer、Double等是Java中的包装类型
        int dame1=Integer.parseInt("1234");
        double dame2=Double.parseDouble("12.34");
        System.out.println(dame1);
        System.out.println(dame2);



    }


}

1.4.2大小写转换

public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}

1.4.3字符串转数组

public class Test9 {
    public static void main(String[] args) {
        String s1="nguhaunjdf";
        //字符串转数组
        char[]ch=s1.toCharArray();
        for(int i=0;i<ch.length;i++){
            System.out.println(ch[i]);
        }

        //数组转字符串
        String s2=new String(ch);
        System.out.println(s2);
    }
}

1.4.4格式化

public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14);
System.out.println(s);
}

1.5字符串的替换

使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.

1.6字符串的拆分

可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串

代码示例: 实现字符串的拆分处理

String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}

代码示例: 字符串的部分拆分

String str = "hello world hello bit" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}

注意:有些特殊字符作为分隔符需要加上转义字符,才可以被识别

// 需要加上转义字符的特殊分隔符
        String s4="hello.world";
        String []strr=s4.split("\\.");
        for(int i=0;i<strr.length;i++){
            System.out.println(strr[i]);
        }
        System.out.println("---------------------------------");
        //在String字符串中”\“不能是单数存在存在例hello\world程序会报错
        String s7="hello\\\\world";
        String []stttt=s7.split("\\\\");
        for(int i=0;i< stttt.length;i++){
            System.out.println(stttt[i]);
        }
        System.out.println("------------------------------------------");
        //如果一个字符串中有多个分隔符,可以用"|"作为连字符.
        String s9="hello=wor-ld";
        String []st=s9.split("=|-");
        for(int i=0;i< st.length;i++){
            System.out.println(st[i]);
        }
    }

注意事项:

1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" .

2. 而如果是 "\" ,那么就得写成 "\\\\" .(在String字符串中”\“不能是单数存在存在例hello\world程序会报错)

3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符.

//多次拆分 代码示例

 //多次拆分
        String ss="name=zhangsan&age=11";
        String []a=ss.split("&");
        for(int i=0;i<a.length;i++){
            String []b=a[i].split("=");
            for(int j=0;j<b.length;j++){
                System.out.println(b[j]);
            }
        }

1.7字符串的截取

从一个完整的字符串之中截取出部分内容。(注意代码中一般都是默认为左闭右开)

public class Test99 {
    public static void main(String[] args) {
        //字符串的截取

        String s1="helloworld";
        //String substring(int beginIndex)
        // 从指定索引截取到结尾
        String s2=s1.substring(5);
        System.out.println(s2);
        System.out.println("-----------------------------");
        //String substring(int beginIndex, int endIndex)
        // 截取部分内容
        String s3=s1.substring(0,5);
        System.out.println(s3);
    }
}

注意事项:

1. 索引从0开始

2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标

1.8其它字符串操作

import java.util.Locale;

public class Test7 {
    public static void main(String[] args) {
        //其他字符串操作
        //1.String trim()
        // 去掉字符串中的左右空格,保留中间空格
        String s1="   hello   world       ";
        String s2="dvjkSNDJ***0909打飞机啊的开发";
        System.out.println(s1.trim());
        //2.String toUpperCase()
        // 字符串转大写
        System.out.println(s2.toUpperCase(Locale.ROOT));
        //3,String toLowerCase()
        // 字符串转小写
        System.out.println(s2.toLowerCase(Locale.ROOT));
    }
}

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值