当前位置: 首页 > news >正文

七千字详解javaString类

在这里插入图片描述

文章目录

  • 一、String类
    • 1. String初始化
    • 2. String具体存储形式
  • 二、String中的比较
  • 三、字符串查找
  • 四、字符串转化
    • 1. 大小写转化
    • 2. 数值字符串相互转化
    • 3. 字符串转数组
    • 4. 格式化字符串
  • 五、字符串截取与替换
    • 1. trim()
    • 2. subString()
    • 3.replace()
  • 六、字符串拆分

一、String类

在C语言中要表示字符串只能使用字符数组或者字符指针,在java中专门提供了String类.

1. String初始化

1.常量串构造

public static void main(String[] args) {
        String str1 = "dachang";
    }

2.new String对象

 public static void main(String[] args) {
        String str2 = new String("dachang");
    }

3.使用字符数组构造

public static void main(String[] args) {
        char[] ch = {'d','a','c','h','a','n','g'};
        String str3 = new String(ch);
    }

2. String具体存储形式

String是引用类型,内部不存储字符串本身,而是存储的一个地址.我们打开String的源码看一下
在这里插入图片描述
字符串由两部分组成char[ ]和hash两部分组成,String实际保存在char数组中.

public static void main(String[] args) {
        String s1 = new String("dachang");
        String s2 = new String("woyao");
        String s3 = s1;
    }

我们具体看一下在堆栈上是如何存储的.
在这里插入图片描述

二、String中的比较

  1. ==比较是否引用同一个对象
public static void main(String[] args) {
        String s1 = new String("dachang");
        String s2 = new String("dachang");
        System.out.println(s1 == s2);
    }

在这里插入图片描述
2.equals方法.按照具体字符串内容比较.
在这里插入图片描述
我们可以发现Object中的equals方法是按照==方式,比较的是引用地址.
在这里插入图片描述
String类中重写了Object中的equals方法,按照字符串中的内容比较.

 public static void main(String[] args) {
        String s1 = new String("dachang");
        String s2 = new String("dachang");
        System.out.println(s1.equals(s2));
    }

在这里插入图片描述
3. compareTo方法.
equals只能比较两个字符串是否相等,返回一个boolean类型.但如果你要知道谁大谁小这时候equals就不能满足了.

  1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
  2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
    在这里插入图片描述
    我们发现String类是实现了Comparable接口的.
public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abb");
        System.out.println(s1.compareTo(s2));
    }

在这里插入图片描述
忽略大小写的比较:
compareToIgnoreCase方法与comparaTo相同但忽略大小写.

public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ABC");
        System.out.println(s1.compareToIgnoreCase(s2));
    }

在这里插入图片描述

三、字符串查找

1. charAt(int index)

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.charAt(2));
    }

charAt()方法是返回index下标的字符
在这里插入图片描述
2.indexOf(int ch)

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.indexOf('l'));
    }

indexOf()方法是返回ch字符第一次出现的位置,没有返回-1
在这里插入图片描述
3.indexOf(String str)

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.indexOf("el"));
    }

indexOf()方法返回str字符串第一次出现的位置,没有返回-1
在这里插入图片描述
4.lastIndex(int ch)

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.lastIndexOf('l'));
    }

lastindexOf()方法是从后往前找返回ch字符第一次出现的位置,没有返回-1
在这里插入图片描述
5.lastIndex(String str)

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.lastIndexOf("lo"));
    }

lastindexOf()方法是从后往前找返回str字符串第一次出现的位置,没有返回-1
在这里插入图片描述

四、字符串转化

1. 大小写转化

toUpperCase()小写转大写,其他字符不变

public static void main(String[] args) {
        String str = new String("hello");
        System.out.println(str.toUpperCase());
    }

在这里插入图片描述
toLowerCase()小写转大写,其他字符不变

public static void main(String[] args) {
        String str = new String("HELLO");
        System.out.println(str.toLowerCase());
    }

在这里插入图片描述

2. 数值字符串相互转化

数值转字符串

class Person{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
    public static void main(String[] args) {
        String s1 = String.valueOf(100);
        String s2 = String.valueOf(10.5);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Person("zhangsan",21));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

实例类型转字符时需要重写toString()方法,不然输出的时路径@哈希地址
在这里插入图片描述
字符串转数值

public static void main(String[] args) {
        int a = Integer.parseInt("110");
        double b = Double.parseDouble("99.5");
        System.out.println(a);
        System.out.println(b);
    }

这里使用到了Integer包装类型.
在这里插入图片描述

3. 字符串转数组

public static void main(String[] args) {
        String str = new String("abcd");
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]+" ");
        }
    }

在这里插入图片描述

4. 格式化字符串

public static void main(String[] args) {
        String str = String.format("%d-%d-%d",2022,8,31);
        System.out.println(str);
    }

在这里插入图片描述

五、字符串截取与替换

1. trim()

去掉字符串左右所有的空格会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等

public static void main(String[] args) {
        String str = "  hello    ";
        System.out.println(str.trim());
    }

在这里插入图片描述

2. subString()

传入一个参数,是截取此位置到结尾.

public static void main(String[] args) {
        String str = "hello world!";
        System.out.println(str.substring(6));
    }

在这里插入图片描述
传入两个参数,是截取指定范围内容

public static void main(String[] args) {
        String str = "woyaojindachang";
        System.out.println(str.substring(2,8));
    }

在这里插入图片描述

3.replace()

使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

public static void main(String[] args) {
        String str = new String("2022.8.31");
        System.out.println(str.replace('.','-'));
    }

在这里插入图片描述
如果只想替换第一个内容使用replaceFirst()即可

六、字符串拆分

方法功能
String[] split(String regex)字符串全部拆分
String[] split(String regex, int limit)将字符串以指定的格式,拆分为limit组
public static void main(String[] args) {
        String str = "wo yao jin da chang";
        String[] arr = str.split(" ");
        for (String s: arr) {
            System.out.println(s);
        }
    }

在这里插入图片描述

public static void main(String[] args) {
        String str = "wo yao jin da chang";
        String[] arr = str.split(" ",2);
        for (String s: arr) {
            System.out.println(s);
        }
    }

在这里插入图片描述
特殊情况的拆分:
1.如果一个字符串中有多个分隔符,可以用"|"作为连字符.

public static void main(String[] args) {
        String str = "wo&jin=da=chang";
        String[] arr = str.split("=|&");
        for (String s:arr) {
            System.out.println(s);
        }
    }

在这里插入图片描述
2. 字符"|“,”*“,”+"都得加上转义字符,前面加上 “\”

public static void main(String[] args) {
        String str = "2002.8.31";
        String[] s = str.split("\\.");
        for (String ss:s) {
            System.out.println(ss);
        }
    }

在这里插入图片描述
3. 如果是 “” ,那么就得写成 “\\” .

public static void main(String[] args) {
        String str = "2002\\8\\31";
        String[] s = str.split("\\\\");
        for (String ss:s) {
            System.out.println(ss);
        }
    }

在这里插入图片描述

相关文章:

  • 希尔排序算法
  • synchronized同步以及双重检索
  • Codeforce8.29-9.4做题笔记
  • springboot+宴会预定平台 毕业设计-附源码231718
  • python super()详解,一篇文章告诉你python的super是什么,如何使用
  • Redis 的持久化
  • 2022年中国证券行业智能投顾专题分析
  • MYSQL高可用架构之MHA实战(真实可用)
  • 【Reinforcement Learning】蒙特卡洛算法
  • SAP ABAP ADT安装说明 as 20220901
  • 计算机组成原理知识总结(八)输入/输出系统
  • springboot基于java的康泰小区物业管理系统的设计与实现毕业设计源码101926
  • java查看对象真实地址和哈希值的工具类
  • SOLIDWORKS直播课:解锁3DE协同设计平台的“云端结构设计角色”
  • 简单的 手写 服务器
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Angular6错误 Service: No provider for Renderer2
  • CentOS7简单部署NFS
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • input实现文字超出省略号功能
  • Java 内存分配及垃圾回收机制初探
  • Laravel Mix运行时关于es2015报错解决方案
  • LeetCode18.四数之和 JavaScript
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • Odoo domain写法及运用
  • Spring Cloud Feign的两种使用姿势
  • SpriteKit 技巧之添加背景图片
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 前端学习笔记之观察者模式
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 云大使推广中的常见热门问题
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • 如何在招聘中考核.NET架构师
  • ​flutter 代码混淆
  • (23)Linux的软硬连接
  • (4)STL算法之比较
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (pojstep1.1.2)2654(直叙式模拟)
  • (TOJ2804)Even? Odd?
  • (超详细)语音信号处理之特征提取
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (三)c52学习之旅-点亮LED灯
  • (转) Face-Resources
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .Net Remoting常用部署结构
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .netcore如何运行环境安装到Linux服务器
  • .NET面试题(二)