字符串类型的一些操作方法

String类

String用来表示一个字符串。具有以下特点:

         - java.lang.String使用了final修饰,不能 被继承;

         -字符串底层封装了字符数组及针对字符数组的操作算法;

         -内部使用一个char数组保存所有字符,每个字符为2字节,存的是该字符unicode编码。

         -字符串一旦创建,对象永远无法改变,但字符串引用可以重新赋值;若改变会创建新对象

         -Java字符串在内存中采用Unicode编码方式,任何一个字符对应两个字节的定长编码。

 字符串常量池:

java在堆内存中开辟了一段空间用于缓存所有使用字面量形式创建的字符串对象,并在后期再次使用该字面量创建字符串时重用对象,避免内存中堆积大量内容一样的字符串对象来减小内存开销。

对于重复出现的字符串直接量,JVM会首先在常量池中查找,如果存在即返回该对象地址。

案例

public class StringDemo {
    public static void main(String[] args) {
        String s1 = "123abc";//字面量
        String s2 = "123abc";//与s1字面量相同,重用对象
        //地址相同,说明s2重用了s1对象
        System.out.println(s1==s2);//true
        String s3 = "123abc";
        System.out.println(s1==s3);//true

        String s4 = new String("123abc");//new会产生新对象
        System.out.println("s4:"+s4);
        System.out.println(s1==s4);//false
        /*
            通常我们判断字符串都是比较内容,因此应当使用字符串的equals方法
         */
        System.out.println(s1.equals(s4));//true
        /*
            由于字符串是不变对象,改变内容会产生新对象
         */
        s1 = s1 + "!";//生成一个新的字符串对象123abc!.
        System.out.println("s1:"+s1);//123abc!
        System.out.println("s2:"+s2);//123abc
        System.out.println(s1==s2);//false s1,s2已经不再指向同一个对象了

        /*
            这里触发了一个编译器的特性:
            编译器在编译期间若遇到几个计算表达式,发现在编译期可以确定结果时就会进行计算
            并将结果编译到class文件中,这样以来JVM每次执行字节码文件就无需再计算了。
            下面的代码会被编译器改为:
            String s5 = "123abc";
            也因此s5会重用常量池中的对象,所以地址与s2相同
         */
        String s5 = "123" + "abc";
        System.out.println("s5:"+s5);
        System.out.println(s2==s5);//ture

        String s = "123";
        String s6 = s + "abc";//创建了一个新的对象
        System.out.println("s6:"+s6);
        System.out.println(s2==s6);//false

        String s7 = 1+2+3+"abc";//6abc
        System.out.println(s2==s7);//false


        String s8 = 1+'2'+3+"abc";
        System.out.println(s2==s8);//false

        String s9 = 1+"2"+3+"abc";
        System.out.println(s2==s9);//true

    }
}

 字符串常用方法

1.length()

 返回当前字符串的长度(字符个数)

        String str = "我爱java!";
        int len = str.length();
        System.out.println("len:"+len);


2. indexOf()

检索给定字符串在当前字符串中的位置,若当前字符串不含有给定内容则返回值为-1


        String str = "thinking in java";

        int index = str.indexOf("in");//2--是下标
        System.out.println(index);

        //重载的方法-可以从指定位置开始检索第一次出现给定字符串的位置
        index = str.indexOf("in",3);//5--下标
        System.out.println(index);

        //检索最后一次出现in的位置
        index = str.lastIndexOf("in");
        System.out.println(index);


3. substring()

截取当前字符串中指定范围内的字符串。两个参数分别为开始位置的下标和结束位置的下标。            语法    String substring(int start,int end)
  截取当前字符串中指定范围内的字符串。两个参数分别为开始位置的下标和结束位置的下标。
  注:在JAVA API中通常使用两个数字表示范围时是"含头不含尾"的。
 

        String line = "www.baidu.cn";
        //截取域名tedu
        String str = line.substring(4,8);
        System.out.println(str);
        
        //重载的方法是从指定位置开始截取到字符串末尾
        str = line.substring(4);
        System.out.println(str);


4. trim()

去除一个字符串两边的空白字符

        String line = "   hello         ";
        System.out.println(line);
        
        String trim = line.trim();
        System.out.println(trim);


5. charAt()

返回当前字符串中指定位置上的字符

        String str = "thinking in java";
        //获取第10个字符
        char c = str.charAt(9);//下标
        System.out.println(c)


6.startsWith()和endsWith()

判断当前字符串是否是以给定的字符串开始或结束的。

        String line = "http://www.baidu.com";

        boolean starts = line.startsWith("http");
        System.out.println("starts:"+starts);

        boolean ends = line.endsWith(".com");
        System.out.println("ends:"+ends);


7.toLowerCase()和toUpperCase()
将当前字符串中的英文部分转换为全大写或全小写

        String line = "我爱Java";

        String upper = line.toUpperCase();
        System.out.println(upper);

        String lower = line.toLowerCase();
        System.out.println(lower);
 

8.valueOf()

String提供了一组重载的静态方法:valueOf,作用是将其他类型转换为String

        int a = 123;
        String s1 = String.valueOf(a);
        System.out.println("s1:"+s1);

        double d = 123.123;
        String s2 = String.valueOf(d);
        System.out.println("s2:"+s2);

        String s3 = a+"";//任何内容和字符串链接结果都是字符串,与valueOf()相比,效率低下
        System.out.println("s3:"+s3);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值