String的使用

#常用类 String的使用

import org.junit.Test;

//  String的使用
public class StringTest {
    /*
    String: 字符串,使用一对“”来表示
    1.String 声明为final的,不可被继承
    2.String 实现了Serializable接口,表示字符是至此序列化的
               实现了Comparable接口,表示String是可以比较大小的
     3.String 内部定义了final char[] value 用于存储字符串数据
     4.String: 代表了不可变的字符序列。 简称不可变性
                体现: 1.当对字符串重新赋值时,必须重写指定区域赋值,不能使用原有的value赋值。
                        2.
     5.通过字面量的方式(区别于new)给一个字符串赋值,此时字符串声明在字符串常量池中
     6.字符串常量池中不会存储相同内容的字符串。
     */
    @Test
    public void test1(){
        String s1 = "abc";  //字面量的定义方式
        String s2 = "abc";
      //  s1 = "hello";
        System.out.println(s1 == s2);
        System.out.println(s1); //hello
        System.out.println(s2);//abc
        System.out.println("88888888888888888888");

        String s3 = "abc";
        s3 += "def";
        System.out.println(s2);//abc
        System.out.println(s3);//abcdef

        System.out.println("88888888888888888888");
        String s4 = "abc";
        String replace = s4.replace('a', 'm');

        String s5 = "javaee";
        String s6 = s5 + "haixi";
        String s7 = s6.intern();  // intern()  必须在字符串常量池中找
        String s8 = "javaeehaixi";
        System.out.println(s7 == s8);  //true


    }
}

//常用方法

public class StringTest2 {
    public static void main(String[] args) {
        String s1 = "helloworld";

        System.out.println(s1.length());  //输出字符串的长度

        System.out.println(s1.charAt(0));  //输出指定位置上的索引元素

        System.out.println(s1.isEmpty());  //判断数组是不是空的  ,其实就是判断数组的长度

        System.out.println(s1.toUpperCase());   //   HELLOWORLD   将字符串变为大写
        System.out.println(s1);   //helloworld
        System.out.println(s1.toLowerCase());   //  将字符串变为小写

        System.out.println("******************************************");
        String s3 = "   he  ll  o  wor  ld    ";
        System.out.println(s3.trim());  //  去除前后空位

        System.out.println(s3 .equals(s1));  //  false  比较字符串的内容是否相同

        String s4 = "HELLOWORLD";
        System.out.println(s4.equalsIgnoreCase(s1));  //  true    与equals类似  忽略大小写

        String s5 = "abc";
        System.out.println(s4.concat(s5));   //  HELLOWORLDabc  在字符串后面连接一个字符串,拼接  ==  + ,直接用+ 也可以

        String s6 = "abe";
        System.out.println(s5.compareTo(s6));  // -2  比较字符串的大小  a 是97

        String s7 = "北京尚硅谷教育";
        System.out.println(s7.substring(2)); //截取字符串 从开始往后截取
        System.out.println(s7.substring(2, 5));  // 左闭右开

    }
}


public class StringTest3 {
    public static void main(String[] args) {
        String s1 = "helloWorld";
        System.out.println(s1.endsWith("ld"));  // true  判断是不是以指定字符串结尾的
        System.out.println(s1.endsWith("1d"));  //false

        System.out.println(s1.startsWith("he"));  //true 测试是不是以指定的字符串开始

        System.out.println(s1.startsWith("ll", 2)); //true 从2开始是不是以ll开头的

        System.out.println(s1.contains("Wo") + "************");  // true 检查是不是包含这个字符串
        System.out.println(s1.contains("oo") + "aaaaaaaaa");//  false

        System.out.println(s1.indexOf("lo")); //3  找出字符串的索引位置
        System.out.println(s1.indexOf("OO"));  // -1 表示索引没找到
        System.out.println(s1.indexOf("lo", 5)); //从位置5 开始找
        System.out.println(s1.lastIndexOf("or")); //从后往前找
    }
}

ublic class StringTest4 {
    public static void main(String[] args) {
        String s1 = "北京尚硅谷教育北京";
        System.out.println(s1.replace('北', '东'));// 东京尚硅谷教育东京 把北全部替换成为东
        System.out.println(s1.replace("北京", "上海")); //上海尚硅谷教育上海

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值