java基础3--String类

本文详细介绍了Java中的String类,包括其不可变性、常用的字符串操作方法如length()、concat()、format()、charAt()、compareTo()、indexOf()等,并通过示例代码展示了这些方法的用法。此外,还讲解了equals()、hashCode()和intern()方法的区别,以及字符串的比较和编码转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、String类

String类型不可变,一旦创建了String对象,它的值就无法改变了。如果需要对字符串做很多修改,应该选择使用StringBuffer/StringBuilder类

public class Main {
    public static void main(String []args) {
        String s="Goole";
        System.out.println(s);
        s="Runoob";
        System.out.println(s);
    }
}

输出结果:Google    Runoob

看起来值是改变了,但并不是。原因是在定义基本类型的时候,s是一个引用,他被分配在栈中,但是Google 被分配在堆中。Runoob也被分配在堆中,和Google 的地址不一样,s="Runoob"是将这个引用指向了Runoob所在的地址,并没有改变Google所在内存中的值。

二、String方法

1、获取字符串长度  length()

 public static void main(String []args) {
        String s="public";
        int i=s.length();
        System.out.println(i);
    }

2、连接字符串   concat    +

    public static void main(String []args) {
        System.out.println("public".concat("aaa"));
        System.out.println("public"+"aaa");
    }

3、创建格式化字符串  format()

public class Main {
    public static void main(String []args) {
        float floatVar=1.0f;
        int intVar=2;
        String stringVar="new";
        String fs;
        fs = String.format("浮点型变量的值为 " +
                "%f, 整型变量的值为 " +
                " %d, 字符串变量的值为 " +
                " %s", floatVar, intVar, stringVar);
        System.out.println(fs);
    }
}

4、返回指定索引处的char值   charAt() 下标从0开始

    public static void main(String []args) {
        String s="whjuihcoih";
        char fs=s.charAt(6);
        System.out.println(fs);
    }

5、按字典顺序比较两个字符串   compareTo()

返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方结束。如果参数字符串等于此字符串,则返回值 0;如果此字符串小于字符串参数,则返回一个小于 0 的值;如果此字符串大于字符串参数,则返回一个大于 0 的值。

    public static void main(String []args) {
        String str1 = "Strings";
        String str2 = "Strings";
        String str3 = "Strings123";

        int result = str1.compareTo( str2 );
        System.out.println(result);

        result = str2.compareTo( str3 );
        System.out.println(result);

        result = str3.compareTo( str1 );
        System.out.println(result);
    }

6、按字典顺序比较两个字符串,不考虑大小写   compareToIgnoreCase()

7、测试字符串是否以指定后缀结尾       endsWith()    如果是空字符串或整个字符串,返回true

    public static void main(String []args) {
        String str1 = "welcome to aaa.com";
        boolean tile=str1.endsWith("");
        System.out.println(tile);
    }

8、判断字符串的值是否相等     equals()

    public static void main(String []args) {
        String str1 = "welcome to aaa.com";
        String str2=new String("welcome to aaa.com");
        String str3=new String("welcome to aaa.com");
        boolean tile=str1.equals(str2);
        boolean lile=str2.equals(str3);
        System.out.println(tile);
        System.out.println(lile);
    }

9、判断字符串的值是否相等,不考虑大小写     equalsIgnoreCase()

10、使用指定的字符集将字符串编码为byte序列,并将结果存储到一个新的byte数组中。IO流中可能会用到byte类型

public static void main(String args[]) {
        String Str1 = new String("runoob");
 
        try{
            byte[] Str2 = Str1.getBytes();
            System.out.println("返回值:" + Str2 );
            
            Str2 = Str1.getBytes( "UTF-8" );
            System.out.println("返回值:" + Str2 );
            
            Str2 = Str1.getBytes( "ISO-8859-1" );
            System.out.println("返回值:" + Str2 );
        } catch ( UnsupportedEncodingException e){
            System.out.println("不支持的字符集");
        }
    }

11、返回hash码       hashcode()是根据某些规则计算出来的值,可以自己定义规则

    public static void main(String args[]) {
        String Str1 = new String("runoob");
        System.out.println("hashcode"+Str1.hashCode());
        Character str2= new Character('A');
        boolean str3=str2.equals('B');
        System.out.println(str2.hashCode());
    }

12、返回指定字符在在字符串中第一次出现的索引   indexOf()

    public static void main(String args[]) {
        String Str1 = new String("runoob");
        System.out.println(Str1.indexOf('n'));//可以输入字符
        System.out.println(Str1.indexOf(97,3)); //输入字符对应unicode值,从3号位置开始找
        System.out.println(Str1.indexOf('a',3)); //输入字符,从3号开始找
        System.out.println(Str1.indexOf(98));  //输入unicode值
    }

13、返回字符串对象的规范化表示

它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。intern会产生字符串的副本,与原字符串的地址不同。

    public static void main(String args[]) {
        String Str1 = new String("runoob");
        String Str2 = new String("runoob");
        System.out.println(Str1.equals(Str2)); //true
        System.out.println(Str1.intern()==Str1); //false,==比较的是地址
        System.out.println(Str1.intern()==Str2.intern()); //true
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值