Java常用类01:String

💨💨💨——String字符串的简介和存储原理
  • String字符串的简介

    关于java JDK中内置的一个类:java.lang.String

    1. String表示字符串类型,属于引用数据类型,不属于基本数据类型
    2. 在java中随便使用双引号括起来的都是String对象,例:“abc”、“hello world!”
    3. java中规定,在JDK当中双引号括起来的字符串,是不可变的(都是储存在方法区字符串常量池当中的)
      ——为什么SUN公司把字符串存储在一个==字符串常量池==当中呢?
      ——因为字符串在实际开发中使用太频繁,为了执行效率,所以把字符串放在方法区的字符串常量池当中
  • String字符串的储存原理
    public class StringTest01{
    	public static void main(String[] args){
    		// 这两行代码表示的底层创建了3个字符串对象,都在字符串常量池当中
    		String s1="abcdef";
    		String s2="abcdef"+"xy";
    		
    		//分析:这是使用new方式创建的字符串对象,这个代码中的"xy"是从哪里来的
    		//解析:凡是双引号括起来的都在字符串常量池中有一份,new对象时一定在堆内存中开辟空间
    		String s3=new String("xy");
    	}
    }
    
    在这里插入图片描述

💨💨💨——String类的构造方法
  • 常用的构造方法
    1. String s = new String(" ");
    2. String s = " "; <最常用>
    3. String s = new String(char数组);
    4. String s = new String(char数组, 起始下标, 长度);
    5. String s = new String(byte数组);
    6. String s = new String(byte数组, 起始下标, 长度);

💨💨💨——String类的方法
  • 常用的方法
    1. charAt(int index):获得该字符串相应索引位置的值
      ———— 返回值为char
      —— 返回指定索引处的值
      System.out.println("erdong".charAt(2));//d
      

    2. compareTo(String anotherString):按字典顺序比较两个字符串中相对应索引位置每个字符的Unicode值,不为0则返回输出比较值后停止后续比较
      ———— 返回值为int
      —— 返回值为0,表示当前字符串与参数字符串相等
      —— 返回值为正数,表示当前字符串按字典顺序排列在参数字符串之后
      —— 返回值为负数,表示当前字符串按字典顺序排列在参数字符串之前
      System.out.println("123cdswwq".compareTo("123cdswwq"));//0
      System.out.println("523ddswwq".compareTo("123cdswwq"));//4
      System.out.println("023fdswwq".compareTo("123cdswwq"));//-1
      
      注:
      • equals方法老版本调用了compareTo方法;新版本没有直接调用
      • equals方法只能看出相不相等;compareTo方法可以看出是否相等,并且同时还能看出谁大谁小

    3. equals(Object anObject):判断当前字符串是否与参数字符串相等
      ———— 返回值为boolean
      System.out.println("abcd".equals("abcd"));//true
      
      注:
      • equals方法老版本调用了compareTo方法;新版本没有直接调用
      • equals方法只能看出相不相等;compareTo方法可以看出是否相等,并且同时还能看出谁大谁小

    4. equalsIgnoreCase(String anotherString):忽略大小写判断两个字符串是否相等。
      ———— 返回值为boolean
      System.out.println("a123BcaD".equalsIgnoreCase("A123bCad"));//true
      

    5. contains(CharSequence s):判断当前字符串中是否包含参数字符串。
      ———— 返回值为boolean
      System.out.println("HelloWorld.java".contains("World"));//true
      System.out.println("http://www.baidu.com".contains("https"));//false
      

    6. startsWith(String prefix):判断当前字符串是否以参数字符串开始。
      ———— 返回值为boolean
      System.out.println("https://www.baidu.com".startsWith("https"));//true
      System.out.println("http://www.baidu.com".startsWith("https"));//false
      

    7. endsWith(String suffix):判断当前字符串是否以参数字符串结尾。
      ———— 返回值为boolean
      System.out.println("HelloWorld.java".endsWith(".java"));//true
      System.out.println("HelloWorld.java".endsWith(".text"));//false
      

    8. getBytes():将字符串对象转换成字节数组
      ———— 返回值为byte[]
      byte[] bytes = "a1b2c3d4".getBytes();
      System.out.println(Arrays.toString(bytes));//[97, 49, 98, 50, 99, 51, 100, 52]
      

    9. toCharArray():将字符串转换成char数组
      ———— 返回值为char[]
      char[] chars = "a1b2c3d4".toCharArray();
      System.out.println(Arrays.toString(chars));//[a, 1, b, 2, c, 3, d, 4]
      

    10. indexOf(String str):判断参数字符串在当前字符串中第一次出现处的索引(下标)。
      ———— 返回值为int
      System.out.println("pythonC++CC#javaPHP".indexOf("java"));//12
      

    11. lastIndexOf(String str):判断参数字符串在当前字符串中最后一次出现的索引(下标)
      ———— 返回值为int
      System.out.println("pythonC++CC#javaPHP".lastIndexOf("C"));//10
      

    12. isEmpty():判断当前字符串是否为“空字符串”。
      ———— 返回值为boolean
      注:底层源代码调用的应该是字符串的length()方法。
      System.out.println("11wwe".isEmpty());//false
      System.out.println(" ".isEmpty());//false
      System.out.println("".isEmpty());//true
      

    13. length():获得字符串长度(字符个数)
      ———— 返回值为int
      注:判断数组长度是length属性;判断字符串长度是length()方法
      System.out.println("length()".length());//8
      

    14. replace(CharSequence target, CharSequence replacement):将当前字符串的某段字符串替换为参数字符串。
      ———— 返回值为String
      注:String的父接口就是 CharSequence
      System.out.println("http://www.baidu.com".replace("http://", "https://"));//https://www.baidu.com
      

    15. split(String regex):拆分当前字符串
      ———— 返回值为String[]
      String[] s = "name=zhangsan&password=1234&age=23".split("&");
      System.out.println(Arrays.toString(s));//[name=zhangsan, password=1234, age=23]
      for (int i = 0; i < s.length; i++) {
       String[] s1 = s[i].split("=");
       for (int j = 0; j < s1.length; j++) {
           System.out.print(s1[j]+"\t");//name	zhangsan	password	1234	age	23
       }
       System.out.println();
      }
      

    16. substring(int beginIndex):截取起始索引(包含)之后的字符串
      ———— 返回值为String
      System.out.println("https://www.baidu.com".substring(8));//www.baidu.com
      

    17. substring(int beginIndex, int endIndex):截取起始索引(包含)到结束索引(不包含)之间的字符串
      ———— 返回值为String
      System.out.println("https://www.baidu.com".substring(8,11));//www
      

    18. toLowerCase():将当前字符串全部转换为小写。
      ———— 返回值为String
      System.out.println("a1B2C3d4E".toLowerCase());//a1b2c3d4e
      

    19. toUpperCase():将当前字符串全部转换为大写。
      ———— 返回值为String
      System.out.println("a1B2C3d4E".toUpperCase());//A1B2C3D4E
      

    20. trim():去除当前字符串前后空白
      ———— 返回值为String
      System.out.println("     hello    world    ".trim());//hello    world
      

    21. valueOf(Object obj):将“非字符串”转换成“字符串”
      ———— String中唯一一个静态方法,不需要new对象
      System.out.println(String.valueOf(true));//true
      System.out.println(String.valueOf(100));//100
      System.out.println(String.valueOf(new Object()));//java.lang.Object@1b6d3586
      System.out.println(String.valueOf(new String("hello")));//hello
      
      注:
      • 当参数是一个对象的时候,会自动调用该对象的toString()方法
      • 为什么输出一个引用的时候,会调用toString()方法!!!!
        ————本质上System.out.println()这个方法在输出任何数据的时候都是先转换成字符串,再输出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值