String类_Java(一)

news2025/7/10 9:55:17

作者:爱塔居的博客_CSDN博客-JavaSE领域博主

专栏:JavaSE

🌼作者简介:大三学生,希望跟大家一起进步!

文章目录

目录

文章目录

前言

一、构造字符串

二、Sring对象的比较

2.1 比较是否引用同一对象

2.2 比较String内容是否相同

 2.3 比较String大小返回差值

 三、 字符串查找

3.1 char charAt(int index)的使用

3.2 int indexOf(int ch)的使用

​编辑 3.3 int indexOf(int ch,int froIndex)的使用

​编辑 

3.4  int indexOf(String str)的使用

3.5 int indexOf(String str,int fromIndex)的使用 

​编辑

3.6 int lastIndexOf(int ch)的使用

四、转化

4.1 数值和字符串转化

​编辑 4.2 大小写转换

 4.3 字符串转数组

​编辑  4.4 格式化

 五、字符串替换

5.1 替换所有的指定内容

 5.2 替换首次出现的指定内容

六、字符串拆分

6.1 将字符串全部拆分

6.2 将字符串以指定的格式,拆分成limit组 

七、截取字符串 

7.1 从指定索引截取到结尾

 7.2 截取部分内容

八、trim()的用法

总结



前言

我们在C语言中,已经涉及到字符串了,但是在C语言中要表示字符串只能使用字符数组或者字符指针,可以使用标准库提供的字符串系列的函数完成大部分操作。但是这种将数据和操作数据方法分离开的方式不符合面向对象的思想。所以,java语言专门提供了String类。


一、构造字符串

public class Test {
    public static void main(String[] args) {
        //第一种字符串构造方法:使用常量串构造
        String s1="Hello World!";
        System.out.println(s1);
        //第二种:直接newString对象
        String s2=new String("Hello World!");
        System.out.println(s2);
        //第三种:使用字符串进行构造
        char[] array={'H','e','l','l','o',' ','W','o','r','l','d','!'};
        String s3=new String(array);
        System.out.println(s3);
    }
}

输出结果: 

🥪注意:

1.String是引用类型,内部并不存储字符串本身。 

 在12行取断点,Debug:

🍇我们会发现:

1.Java中,字符串没有存/0

2.字符串中其实存的是value数组的地址。

 2.在Java中用" "引起来的也是String类型对象

public class Test {
    public static void main(String[] args) {
        System.out.println("hello".length());//输出字符串hello的长度
    }}

 输出:

二、Sring对象的比较

2.1 比较是否引用同一对象

🌹对于内置类型来说,==比较的是变量中的值;对于引用类型==比较的是引用中的地址

public class Test {
    public static void main(String[] args) {
        String s1="Hello";
        String s2="Hello";
        String s3="Hello World!";
        System.out.println(s1==s2);//引用的同一个对象
        System.out.println(s1==s3);
        System.out.println(s2==s3);
    }
}

输出结果:

public class Test {
    public static void main(String[] args) {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("Hello World!");
        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s3==s2);
    }
}

输出结果:

2.2 比较String内容是否相同

boolean equals(Object anObject) 方法:
public class Test {
    public static void main(String[] args) {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("hello");
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
    }
}

输出结果:

 2.3 比较String大小返回差值

public class Test {
    public static void main(String[] args) {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("Hello World!");
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s3.compareTo(s2));
    }
}

输出结果:

 除了compareTo,还有int compareToIgnoreCase(String str) 。方式相同,但是忽略大小写比较。

public class Test {
    public static void main(String[] args) {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("hello");
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
    }
}

输出: 

public class Test {
    public static void main(String[] args) {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("hello");
        System.out.println(s1.compareToIgnoreCase(s2));
        System.out.println(s1.compareToIgnoreCase(s3));
    }
}

 输出结果:

 三、 字符串查找

方法

功能

char charAt(int index)

返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常

int indexOf(int ch)

返回ch第一次出现的位置,没有返回-1

int indexOf(int ch, int

fromIndex)

fromIndex位置开始找ch第一次出现的位置,没有返回-1

int indexOf(String str)

返回str第一次出现的位置,没有返回-1

int indexOf(String str, int

fromIndex)

fromIndex位置开始找str第一次出现的位置,没有返回-1

int lastIndexOf(int ch)

从后往前找,返回ch第一次出现的位置,没有返回-1

int lastIndexOf(int ch, int

fromIndex)

fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1

int lastIndexOf(String str)

从后往前找,返回str第一次出现的位置,没有返回-1

int lastIndexOf(String str, int

fromIndex)

fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1

3.1 char charAt(int index)的使用

作用:返回以 index位置的数字 为坐标下的字符。

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.charAt(1)); 
}}

输出:

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.charAt(-1));
}}

编译错误:

3.2 int indexOf(int ch)的使用

作用:返回 ch位置下的字符 在字符串中第一次出现的位置坐标

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.indexOf('H'));
        System.out.println(s.indexOf('l'));//s中第一次出现l的位置坐标
}}

输出:

 3.3 int indexOf(int ch,int froIndex)的使用

 作用:返回 以在froIndex位置的数字为坐标开始,查找在ch位置的字符第一次出现的坐标。没有找到的话,就返回-1

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.indexOf('H',0));
        System.out.println(s.indexOf('o',0));
        System.out.println(s.indexOf('o',1));
        System.out.println(s.indexOf('l',0));//找到从0开始,第一个出现的位置坐标
        System.out.println(s.indexOf('e',2));//没有找到,返回-1

}}

输出:

3.4  int indexOf(String str)的使用

作用:返回str位置上字符串所在位置坐标。如果没有找到,则返回-1

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.indexOf("el"));
        System.out.println(s.indexOf("elo"));
}}

输出:

3.5 int indexOf(String str,int fromIndex)的使用 

作用:输出在str位置的字符串,从 以在fromIndex位置数值为坐标 开始查找,返回位置。如果没有找到,输出-1

public class Test {
    public static void main(String[] args) {
        String s ="Hello";
        System.out.println(s.indexOf("ll",0));
        System.out.println(s.indexOf("ll",2));
        System.out.println(s.indexOf("ll",3));
        System.out.println(s.indexOf("llo",0));
}}

 结果:

3.6 int lastIndexOf(int ch)的使用

作用:从后往前找,返回ch第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String s ="aabbccaa";
        System.out.println(s.lastIndexOf("a"));
        System.out.println(s.lastIndexOf("e"));
}}

输出结果:

 后面的,大家就自己试着去写吧。

四、转化

4.1 数值和字符串转化

public class Test {
    public static void main(String[] args) {
        String s1 = String.valueOf(12);//数字转换为字符串
        String s2 = String.valueOf(0.34);
        String s3 = String.valueOf(true);//boolean转字符串
        System.out.println(s1+s2);
        System.out.println(s3);
        int data1 = Integer.parseInt("12");//字符串转数字
        double data2 = Double.parseDouble("0.34");
        System.out.println(data1+data2);
    }
}

输出:

 4.2 大小写转换

只转化字母:

public class Test {
    public static void main(String[] args) {
       String s1="hello world!";
       String s2="HELLO WORLD!";
        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase());
    }
}

 输出:

 4.3 字符串转数组

public class Test {
    public static void main(String[] args) {
        String s = "hello";
        char[] ch=s.toCharArray(); //字符串转数组
        for (int i=0;i<s.length();i++){
            System.out.print(ch[i]);
        }
        System.out.println();
        String s1=new String(ch);//数组转字符串
        System.out.println(s1);
    }
}

输出:

  4.4 格式化

public class Test {
    public static void main(String[] args) {
      String s=String.format("%d年%d月%d日",2022,11,23);
        System.out.println(s);
    }
}

输出结果:

 五、字符串替换

 使用一个指定的新的字符串替换掉已有的字符串数据:

5.1 替换所有的指定内容

String replaceAll(String regex, String replacement)
public class Test {
    public static void main(String[] args) {
     String s1="hello!";
     System.out.println(s1.replaceAll("!","?"));;
        System.out.println(s1);
    }
}

 5.2 替换首次出现的指定内容

String replaceFirst(String regex, String replacement):
public class Test {
    public static void main(String[] args) {
     String s1="helloworld";
     System.out.println(s1.replaceFirst("o","-"));;
        System.out.println(s1);
    }
}

输出:

public class Test {
    public static void main(String[] args) {
     String s1="helloworld";
     String s2=s1.replaceFirst("o","-");
     System.out.println(s2);
    }
}

输出结果:

 🍨我们会注意到:由于字符串是不可变对象,替换不修改当前的字符串,而是产生一个新的字符串

六、字符串拆分

6.1 将字符串全部拆分

String[] split(String regex)
public class Test {
    public static void main(String[] args) {
     String s1="hello my friends";
     String[] s2=s1.split(" ");
     for (String s:s2){
         System.out.println(s);
     }
    }
}

输出:

  

6.2 将字符串以指定的格式,拆分成limit组 

String[] split(String regex, int limit):
public class Test {
    public static void main(String[] args) {
     String s1="hello my friends";
     String[] s2=s1.split(" ",2);
     for (String s:s2){
         System.out.println(s);
     }
    }
}

输出:

 

注意!!! 

1.字符"|","*","+","."都要加上转义字符,前面加上"\\"

2.如果是"\",那么就写成"\\\\"

3.如果一个字符串中有多个分隔符,那么用"|"作为连字符

public class Test {
    public static void main(String[] args) {
     String s1="192.168.1.1";
     String[] s2=s1.split("\\.");
     for (String s:s2){
         System.out.println(s);
     }
    }
}

多次拆分:

public class Test {
    public static void main(String[] args) {
        String str = "name=zhangsan&age=18" ;
        String[] result = str.split("&") ;
        for (int i = 0; i < result.length; i++) {
            String[] temp = result[i].split("=") ;
            System.out.println(temp[0]+" = "+temp[1]);
        }
     }
    }

输出:

七、截取字符串 

7.1 从指定索引截取到结尾

String substring(int beginIndex):
public class Test {
    public static void main(String[] args) {
        String str = "hello world";
        System.out.println(str.substring(2));
     }
    }

 输出:

 7.2 截取部分内容

String substring(int beginIndex, int endIndex)
public class Test {
    public static void main(String[] args) {
        String str = "hello world";
        System.out.println(str.substring(2,7));
     }
    }

输出:

🍅注意:

1.索引从0开始

2.前闭后开,substring(2,7)表示包含2下标的字符,不包括7下标的字符

八、trim()的用法

功能:会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等),保留中间空格

public class Test {
    public static void main(String[] args) {
        String str = " 红楼 梦 ";
        System.out.println("《"+str.trim()+"》");
     }
    }

输出:

总结

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/28877.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

跨平台编译工具--CMake上手教程

文章目录一、引入二、基本关键字1.PROJECT2.SET3.MESSAGE4.ADD_EXECUTABLE5.ADD_SUBDIRECTORY(1)使用(2)CMakeLists执行顺序(3)输出文件的位置6.INSTALL(1)安装文件(2)安装非目标文件可执行文件(3)安装目录(4)安装指令7.ADD_LIBRARY8.SET_TARGET_PROPERTIES三、语法的基本规则四…

知识引擎藏经阁天花板——高性能Java架构核心原理手册

开场 本书是按照程序设计与架构的顺序编写的&#xff0c;共13章。 第1章介绍学习高性能Java应了解的核心知识&#xff0c;为前置内容。 第2章和第3章讲解在编写代码之前&#xff0c;如何高效地为My SQL填充亿级数据&#xff0c;并对My SQL进行基准测试&#xff0c;以便在之后…

Linux-awk和printf

printf printf ‘输出类型输出格式’ 输内容 输出类型&#xff1a; %ns 输出字符串&#xff0c;n是数字指代输出几个字符 %ni 输出整数&#xff0c;n是数字&#xff0c;指代输出几个数字 %m.nf 输出浮点数&#xff0c;m和n是数字&#xff0c;指代输出总位数和小数位数&#xf…

YOLO V1学习总结

图片大小&#xff1a;448 * 448 —> 7 * 7 *&#xff08;5 * B C&#xff09; 5&#xff1a;每个框的x,y,w,h,confidence; B2&#xff1a;在7*7的feature上&#xff0c;每个cell会生成2个预测框&#xff1b; C&#xff1a;类别数。 损失函数 坐标中心误差和位置宽高的误差…

卷积神经网络基本概念

卷积神经网络基本概念1. 感受野2. 卷积核3. 特征图【feature map】4. 通道【channel】5. 填充【padding】6. 步长【stride】7. 池化【pooling】8. dropout数字1处&#xff1a;一个圈表示一个神经元数字2处&#xff1a;一个圈表示一个神经元&#xff0c;圈的大小表示感受野的大小…

基于matlab的最小支配集CDS仿真

目录 1.算法描述 2.仿真效果预览 3.MATLAB部分代码预览 4.完整MATLAB程序 1.算法描述 支配集的定义如下&#xff1a;给定无向图G &#xff08;V , E&#xff09;,其中V是点集&#xff0c; E是边集&#xff0c; 称V的一个子集S称为支配集当且仅当对于V-S中任何一个点v, 都有…

一、FFmpeg 的初尝试《FFmpeg 音视频开发基础入门到实战》

学习目标 了解 FFmpeg学习 FFmpeg 工具的下载及环境配置了解 FFmpeg 工具的使用方式了解 FFmpeg play 的使用方法了解 FFmpeg paly 的音量设置、窗口设置、音量设置等设置方法 一、了解 FFmpeg FFmpeg 是一个音视频处理的工具&#xff0c;通过 FFmpeg 可以对视频进行旋转、缩…

新零售SaaS架构:多租户系统架构设计

什么是多租户&#xff1f; 多租户是SaaS领域的特有产物&#xff0c;在SaaS服务中&#xff0c;租户是指使用SaaS系统的客户&#xff0c;租户不同于用户&#xff0c;例如&#xff0c;B端SaaS产品&#xff0c;用户可能是某个组织下的员工&#xff0c;但整个企业组织是SaaS系统的租…

得数据者得天下!作为后端开发必备技能之一的MySQL,这份十多年经验总结的应用实战与性能调优我想你肯定是需要的!

MySQL对于很多Linux从业者而言&#xff0c;是一个非常棘手的问题&#xff0c;多数情况都是因为对数据库出现问题的情况和处理思路不清晰。在进行MySQL的优化之前必须要了解的就是MySQL的查询过程&#xff0c;很多的查询优化工作实际上就是遵循一些原则让MySQL的优化器能够按照预…

跑步戴什么耳机比较好、精挑五款最佳跑步耳机推荐

运动蓝牙耳机近几年受到市场的欢迎&#xff0c;种类越来越多&#xff0c;各类功能也日益五花八门&#xff0c;消费者很难准确的进行分辨&#xff0c;一不小心可能买到华而不实的产品。现在了解一下值得入手的运动蓝牙耳机&#xff0c;从多个角度对蓝牙耳机进行评估后&#xff0…

大数据项目之电商数仓、实时数仓同步数据、离线数仓同步数据、用户行为数据同步、日志消费Flume配置实操、日志消费Flume测试、日志消费Flume启停脚本

文章目录8. 实时数仓同步数据9. 离线数仓同步数据9.1 用户行为数据同步9.1.1 数据通道9.1.1.1 用户行为数据通道9.1.2 日志消费Flume配置概述9.1.2.1 日志消费Flume关键配置9.1.3 日志消费Flume配置实操9.1.3.1 创建Flume配置文件9.1.3.2 配置文件内容如下9.1.3.2.1 配置优化9.…

Arcpy新增随机高程点、空间插值及批量制图

&#xff08;1&#xff09;在“地质调查点基础数据表.xls”中图幅范围内增加200个随机位置的高程点。构建一个shape文件&#xff0c;采用自定义工具的模式&#xff0c;参数有两个&#xff1a;一个是让用户选择excel文件&#xff0c;一个让用户指定新生成的文件名。 &#xff08…

五子棋小游戏——Java

文章目录一、内容简介&#xff1a;二、基本流程三、具体步骤1.菜单栏2.创建棋盘并初始化为空格(1)定义行数、列数为常量(2)定义棋盘(3)给棋盘添加坐标并初始化棋盘为空格3.打印棋盘4.玩家落子5.判断输赢四、代码实现五、效果展示一、内容简介&#xff1a; 五子棋小游戏是我们日…

网络工程SSM毕设项目 计算机毕业设计【源码+论文】

文章目录前言 题目1 : 基于SSM的游戏攻略资讯补丁售卖商城 <br /> 题目2 : 基于SSM的疫情期间医院门诊网站 <br /> 题目3 : 基于SSM的在线课堂学习设计与实现<br /> 题目4 : 基于SSM的大学生兼职信息系统 <br /> 题目5 : 基于SSM的大学生社团管理系统 …

2022 云原生编程挑战赛圆满收官,见证冠军战队的诞生

11 月 3 日&#xff0c;天池大赛第三届云原生编程挑战赛在杭州云栖大会圆满收官。三大赛道18大战队手历经 3 个月激烈的角逐&#xff0c;终于交上了满意的答卷&#xff0c;同时也捧回了属于他们的荣耀奖杯。 云原生编程挑战赛发起人王荣刚在开场分享中提到&#xff0c;“在阿里…

【无标题】后来,我认为王阳明比尼采,叔本华都高明

悲欣交集 ——灵遁者 虽然我是个写作者&#xff0c;但我还是希望无苦难可以诉说。可事与愿违&#xff0c;我的笔下总有忧伤&#xff0c;也许我天生忧郁。 我觉得现在比以往任何时候&#xff0c;都更能体验和接触苦难。打开新闻&#xff0c;打开抖音&#xff0c;苦难就扑面而…

SpringBoot 整合 Shiro 权限框架

目录Shiro概述Shiro介绍基本功能Shiro架构SpringBoot整合Shiro环境搭建登录、授权、角色认证实现自定义实现 RealmShiro配置类controller代码权限异常处理多个 realm 的认证策略设置会话管理获得session方式Shiro概述 Shiro介绍 Apache Shiro 是一个功能强大且易于使用的 Jav…

力扣(LeetCode)42. 接雨水(C++)

栈 明确目标——计算接雨水的总量。 可以想到一层一层的接雨水。和算法结合&#xff0c;介绍思想 &#xff1a; 遍历柱子&#xff0c;栈 stkstkstk 维护降序高度的柱子&#xff0c;如果出现升序&#xff0c;说明形成凹槽&#xff0c;计算凹槽能接的雨水&#xff0c;加入答案。…

Java强软弱虚引用和ThreadLocal工作原理(一)

一、概述 本篇文章先引入java的四种引用在android开发中的使用&#xff0c;然后结合弱引用来理解ThreadLocal的工作原理。 二、JVM名词介绍 在提出四种引用之前&#xff0c;我们先提前说一下 Java运行时数据区域 虚拟机栈 堆 垃圾回收机制 这四个概念。 2.1 java运行时数据…

freeswitch通过limit限制cps

概述 freeswitch在业务开发中有极大的便利性&#xff0c;因为fs内部实现了很多小功能&#xff0c;这些小功能组合在一起&#xff0c;通过拨号计划就可以实现很多常见的业务功能。 在voip云平台的开发中&#xff0c;我们经常会碰到资源的限制&#xff0c;有外部线路资源方面的…