• String类


    hello,大家好,今天为大家带来String类的相关知识

    在C语言中,没有字符串类,在Java中有字符串类,举个例子,String  ret="hello";   这就是一个字符串类型的变量,

    下面我们来说一下String 类在创建的时候的三种形式

    1. public class Test {
    2. public static void main(String[] args) {
    3. String string="hello";
    4. String ret=new String("wyb520");
    5. char[] ch={'a','b','c'};
    6. String str=new String(ch);
    7. }
    8. }

    一种是直接写

    还有一种是new一个对象

    最后一种是通过创建一个数组,然后new一个数组对象

     

     大家看我的这个截图,我们要注意到其实String这个对象在创建一个变量时有value和hash两个内存,我们转到String的源码看一下

     从这个源码当中就可以看出来,有hash和value,value是一个引用

    一定有同学对String直接new了一个新对象有疑问,其实真正的原因就是String类继承了object类,object类默认是所有类的父类,所以可以直接new实例化对象

    现在用图来更好的理解一下String类的对象的创建

     

    string与ret两个变量的创建如上图,比较特殊的是数组那一块,先创建数组然后new  String时拷贝了一份数组。

    Java当中没有所谓的\0结尾。

    String 是引用类型,内部并不存储字符串,其实字符串保存在数组中

    1. String s1 = new String("hello");
    2. String s2 = new String("world");
    3. String s3 = s1;
    4. System.out.println(s1.length());
    5. System.out.println(s1.isEmpty());

    在计算字符串的长度时一定要.length(),这个括号不能忘

    下面继续画图让大家更好的理解这几个对象的创建

     🐶String字符串的比较

    字符串排序。Java中总共提供了4中方式

    1.用==来判断. ==比较是否引用同一个对象

    对于基本类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址。

    1. public static void main(String[] args) {
    2. int a = 40;
    3. int b = 20;
    4. int c = 40;
    5. System.out.println(a == b); // false
    6. System.out.println(a == c); // true
    7. }
    1. public static void main(String[] args) {
    2. String s1=new String("wyb");
    3. String s2=new String("wyb");
    4. System.out.println(s1==s2);//false
    5. }

    对于这个引用类型

     两个变量的地址不一样。所以答案是false

    🐷2.用equals方法比较

    1. public static void main(String[] args) {
    2. String s1 = new String("hello");
    3. String s2 = new String("hello");
    4. String s3 = new String("world");
    5. System.out.println(s1 == s2); // false
    6. System.out.println(s1.equals(s2));//true
    7. System.out.println(s2 == s3); // false
    8. System.out.println(s2.equals(s3)); // false
    9. }

    3.使用compare   to

    1. System.out.println(s1.compareTo(s2));
    2. //compaer to 的源码
    3. public int compareTo(String anotherString) {
    4. int len1 = value.length;
    5. int len2 = anotherString.value.length;
    6. int lim = Math.min(len1, len2);
    7. char v1[] = value;
    8. char v2[] = anotherString.value;
    9. int k = 0;
    10. while (k < lim) {
    11. char c1 = v1[k];
    12. char c2 = v2[k];
    13. if (c1 != c2) {
    14. return c1 - c2;
    15. }
    16. k++;
    17. }
    18. return len1 - len2;
    19. }

    compare  to    返回的是int类型的值,看一下源码大家就能 明白了,长度相等返回0,不等返回两长度差值

    🐷4.int compareToIgnoreCase
    

    这个函数与compare   to   其实是一样的,就是忽略了大小写

    1. public static void main(String[] args) {
    2. String s1 = new String("WYB");
    3. String s2 = new String("wyb");
    4. System.out.println(s1.compareToIgnoreCase(s2));//0
    5. }

     

    String类提供的字符串查找方法

    1. public static void main(String[] args) {
    2. String s = "aaabbbcccaaabbbccc";
    3. System.out.println(s.charAt(3)); // 'b'
    4. System.out.println(s.indexOf('c')); // 6
    5. System.out.println(s.indexOf('c', 10)); // 15
    6. System.out.println(s.indexOf("bbb")); // 3
    7. System.out.println(s.indexOf("bbb", 10)); // 12
    8. System.out.println(s.lastIndexOf('c')); // 17
    9. System.out.println(s.lastIndexOf('c', 10)); // 8
    10. System.out.println(s.lastIndexOf("bbb")); // 12
    11. System.out.println(s.lastIndexOf("bbb", 10));// 3
    12. }

    💚char charAt(int index):返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常
    💚int indexOf(int ch) 返回ch第一次出现的位置,没有返回-1

    💚int indexOf(int ch, intfromIndex):从fromIndex位置开始找ch第一次出现的位置,没有返回-1

    💚int indexOf(String str) 返回str第一次出现的位置,没有返回-1
    💚int indexOf(String str, intfromIndex):从fromIndex位置开始找str第一次出现的位置,没有返回-1
    💚int lastIndexOf(int ch) 从后往前找,返回ch第一次出现的位置,没有返回-1
    💚int lastIndexOf(int ch, intfromIndex):从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
    💚int lastIndexOf(String str) 从后往前找,返回str第一次出现的位置,没有返回-1
    💚 int lastIndexOf(String str, intfromIndex):从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1

    🐷数值和字符串转换

    用valueof

    1. public static void main(String[] args) {
    2. //把其他的数据类型 变成字符串
    3. String s = String.valueOf(123);
    4. String s2 = String.valueOf(12.5);
    5. System.out.println(s);
    6. System.out.println(s2);
    7. }

    结果

     

    1. public static void main(String[] args) {
    2. //字符串转数字
    3. int data1 = Integer.parseInt("199785");
    4. double data2 = Double.parseDouble("1997.85");
    5. System.out.println(data1);
    6. System.out.println(data2);
    7. }
    1. public static void main(String[] args) {
    2. int a = Integer.valueOf("64");//6*8^1 + 4*8^0 = 52//默认十进制
    3. System.out.println(a);
    4. int a2 = Integer.parseInt("100");//默认十进制
    5. System.out.println(a2);
    6. }
    1. public static void main(String[] args) {
    2. String s1 = "abondon";
    3. String ret = s1.toUpperCase();
    4. System.out.println(ret);
    5. System.out.println("s1-> " + s1);
    6. String s2 = "ABANDON";
    7. ret = s2.toLowerCase();
    8. System.out.println(ret);
    9. }

     这里要注意,使用这个大小写的时候要记得本身并没有改变

    1. public static void main(String[] args) {
    2. String s = "happy";
    3. // 字符串转数组
    4. char[] ch = s.toCharArray();
    5. for (int i = 0; i < ch.length; i++) {
    6. System.out.print(ch[i]);
    7. }
    8. System.out.println();
    9. // 数组转字符串
    10. String s2 = new String(ch);
    11. System.out.println(s2);
    12. }

    格式化

    1. public static void main(String[] args) {
    2. String s = String.format("%d-%d-%d", 2003 ,2,14);
    3. System.out.println(s);//2003-2-14
    4. }
    1. public static void main(String[] args) {
    2. String s1 = "ababcabcdabcde";
    3. String ret = s1.replaceFirst("ab","qquuuuu");//代表用qquuuuu替换第一个ab
    4. System.out.println(ret);
    5. System.out.println("s1-> "+s1);//就算替换,但是它本身是没有改变的
    6. }
    1. 字符串拆分
    2. public static void main(String[] args) {
    3. String s1 = "hello wyb happy everyday";
    4. String[] ret = s1.split(" ",4);//以空格的形式分开,分成4组
    5. for (String s : ret) {
    6. System.out.println(s);//打印
    7. }
    8. }

     

    1. public static void main(String[] args) {
    2. String str = "192.168.1.1";
    3. String[] ret = str.split("\\.",4);
    4. for (String s : ret) {
    5. System.out.println(s);
    6. }
    7. }

    以\.的形式分为4组打印,\需要两条\\来编译,那么两条\\就需要4条\\\\来编译

    1. public static void main16(String[] args) {
    2. String str = "197\\168\\1\\1";
    3. String[] ret = str.split("\\\\");
    4. for (String s : ret) {
    5. System.out.println(s);
    6. }
    7. }

     

    1. public static void main(String[] args) {
    2. String str = "abcdef";
    3. String ret = str.substring(1,4);//[1,4)//字符串截取//结果为bcd默认范围为左闭右开
    4. System.out.println(ret);
    5. System.out.println("=============");
    6. String s = " we are happy ";
    7. System.out.println(s.trim());//当前字符串的左右的空格去掉
    8. //trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
    9. System.out.println(s);
    10. }

    字符串替代

    1. public static void main(String[] args) {
    2. String str="we%are%happy";
    3. String ret=str.replaceAll("%"," ");
    4. System.out.println(ret);
    5. }

    用空格替代所有的%

     今天的分享就到这里,我们下期再见,886

  • 相关阅读:
    以太坊合并 你需知道的10个问题
    Class Activation Mapping(CAM)介绍
    手把手教你做个智能加湿器(一)
    Midjourney-01 初试上手 注册使用并生成你的第一张AI图片 详细流程 提示词 过程截图 生成结果 付费文生图的天花板!
    实验3
    微信小程序(五十二)开屏页面效果
    ftp服务器搭建
    关于React中的数据源绑定
    Android xml布局设置默认隐藏&&通讯
    支配世界的几个重要算法
  • 原文地址:https://blog.csdn.net/weixin_61436104/article/details/125502503