JAVA字符串

字符串

 

1. 字符串

1.1 字符串概述和特点

java.lang.String类代表字符串。
API当中说:Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
其实就是说:程序当中所有的双引号字符串,都是String类的对象。(就算没有new,也照样是。)

字符串的特点:

  1.  字符串的内容永不可变。【重点】
  2. 正是因为字符串不可改变,所以字符串是可以共享使用的。
  3. 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组。

1.2字符串的构造方法和直接创建

创建字符串的常见3+1种方式。
三种构造方法:
public String():创建一个空白字符串,不含有任何内容。
public String(char[] array):根据字符数组的内容,来创建对应的字符串。
public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
一种直接创建:
String str = "Hello"; // 右边直接用双引号

注意:直接写上双引号,就是字符串对象。

1.2.1 案例代码一

 1 /*
 2 java.lang.String类代表字符串。
 3 API当中说:Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
 4 其实就是说:程序当中所有的双引号字符串,都是String类的对象。(就算没有new,也照样是。)
 5 
 6 字符串的特点:
 7 1. 字符串的内容永不可变。【重点】
 8 2. 正是因为字符串不可改变,所以字符串是可以共享使用的。
 9 3. 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组。
10 
11 创建字符串的常见3+1种方式。
12 三种构造方法:
13 public String():创建一个空白字符串,不含有任何内容。
14 public String(char[] array):根据字符数组的内容,来创建对应的字符串。
15 public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
16 一种直接创建:
17 String str = "Hello"; // 右边直接用双引号
18 
19 注意:直接写上双引号,就是字符串对象。
20  */
21 public class Demo01String {
22 
23 public static void main(String[] args) {
24 // 使用空参构造
25 String str1 = new String(); // 小括号留空,说明字符串什么内容都没有。
26 System.out.println("第1个字符串:" + str1);
27 
28 // 根据字符数组创建字符串
29 char[] charArray = { 'A', 'B', 'C' };
30         String str2 = new String(charArray);
31         System.out.println("第2个字符串:" + str2);
32 
33 // 根据字节数组创建字符串
34 byte[] byteArray = { 97, 98, 99 };
35         String str3 = new String(byteArray);
36         System.out.println("第3个字符串:" + str3);
37 
38 // 直接创建
39 String str4 = "Hello";
40         System.out.println("第4个字符串:" + str4);
41     }
42 }

 

 1.3字符串常量池

字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池中。

对于基本类型来说,==是进行数值的比较。
对于引用类型来说,==是进行【地址值】的比较。

1.3.1案例代码二

 1 /*
 2 字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池中。
 3 
 4 对于基本类型来说,==是进行数值的比较。
 5 对于引用类型来说,==是进行【地址值】的比较。
 6  */
 7 public class Demo02StringPool {
 8 
 9 public static void main(String[] args) {
10         String str1 = "abc";
11         String str2 = "abc";
12 
13 char[] charArray = {'a', 'b', 'c'};
14         String str3 = new String(charArray);
15 
16         System.out.println(str1 == str2); // true
17 System.out.println(str1 == str3); // false
18 System.out.println(str2 == str3); // false
19 }
20 }

 

 1.4常见方法

1.4.1字符串的比较相关方法

=是进行对象的地址值比较,如果确实需要字符串的内容比较,可以使用两个方法:public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true;否则返回false。

注意事项:1. 任何对象都能用Object进行接收。

2. equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
3. 如果比较双方一个常量一个变量,推荐把常量字符串写在前面。
推荐:"abc".equals(str)    不推荐:str.equals("abc")
public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较。

1.4.1.1案例代码三
 1 /*
 2 ==是进行对象的地址值比较,如果确实需要字符串的内容比较,可以使用两个方法:
 3 
 4 public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true;否则返回false。
 5 注意事项:
 6 1. 任何对象都能用Object进行接收。
 7 2. equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
 8 3. 如果比较双方一个常量一个变量,推荐把常量字符串写在前面。
 9 推荐:"abc".equals(str)    不推荐:str.equals("abc")
10 
11 public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较。
12  */
13 public class Demo01StringEquals {
14 
15 public static void main(String[] args) {
16         String str1 = "Hello";
17         String str2 = "Hello";
18 char[] charArray = {'H', 'e', 'l', 'l', 'o'};
19         String str3 = new String(charArray);
20 
21         System.out.println(str1.equals(str2)); // true
22 System.out.println(str2.equals(str3)); // true
23 System.out.println(str3.equals("Hello")); // true
24 System.out.println("Hello".equals(str1)); // true
25 
26 String str4 = "hello";
27         System.out.println(str1.equals(str4)); // false
28 System.out.println("=================");
29 
30         String str5 = null;
31         System.out.println("abc".equals(str5)); // 推荐:false
32 //        System.out.println(str5.equals("abc")); // 不推荐:报错,空指针异常NullPointerException
33 System.out.println("=================");
34 
35         String strA = "Java";
36         String strB = "java";
37         System.out.println(strA.equals(strB)); // false,严格区分大小写
38 System.out.println(strA.equalsIgnoreCase(strB)); // true,忽略大小写
39 
40 // 注意,只有英文字母区分大小写,其他都不区分大小写
41 System.out.println("abc一123".equalsIgnoreCase("abc壹123")); // false
42 }
43 }

1.4.2字符串的获取相关方法

String当中与获取相关的常用方法有:

public int length()
:获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str)
:将当前字符串和参数字符串拼接成为返回值新的字符串。
public char charAt(int index)
:获取指定索引位置的单个字符。(索引从0开始。)
public int indexOf(String str)
:查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。

1.4.2.1案例代码四
 1 /*
 2 String当中与获取相关的常用方法有:
 3 
 4 public int length():获取字符串当中含有的字符个数,拿到字符串长度。
 5 public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。
 6 public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始。)
 7 public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。
 8  */
 9 public class Demo02StringGet {
10 
11 public static void main(String[] args) {
12 // 获取字符串的长度
13 int length = "asdasfeutrvauevbueyvb".length();
14         System.out.println("字符串的长度是:" + length);
15 
16 // 拼接字符串
17 String str1 = "Hello";
18         String str2 = "World";
19         String str3 = str1.concat(str2);
20         System.out.println(str1); // Hello,原封不动
21 System.out.println(str2); // World,原封不动
22 System.out.println(str3); // HelloWorld,新的字符串
23 System.out.println("==============");
24 
25 // 获取指定索引位置的单个字符
26 char ch = "Hello".charAt(1);
27         System.out.println("在1号索引位置的字符是:" + ch);
28         System.out.println("==============");
29 
30 // 查找参数字符串在本来字符串当中出现的第一次索引位置
31 // 如果根本没有,返回-1值
32 String original = "HelloWorldHelloWorld";
33 int index = original.indexOf("llo");
34         System.out.println("第一次索引值是:" + index); // 2
35 
36 System.out.println("HelloWorld".indexOf("abc")); // -1
37 
38 System.out.println("==============");
39         String str = "hello";
40         str.concat("world");
41         System.out.println(str);
42     }
43 }

1.4.3   字符串的截取方法

字符串的截取方法:

public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。
public String substring(int begin, int end):截取从begin开始,一直到end结束,中间的字符串。
备注:[begin,end),包含左边,不包含右边。

1.4.3.1案例代码五

 1 /*
 2 字符串的截取方法:
 3 
 4 public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。
 5 public String substring(int begin, int end):截取从begin开始,一直到end结束,中间的字符串。
 6 备注:[begin,end),包含左边,不包含右边。
 7  */
 8 public class Demo03Substring {
 9 
10 public static void main(String[] args) {
11         String str1 = "HelloWorld";
12         String str2 = str1.substring(5);
13         System.out.println(str1); // HelloWorld,原封不动
14 System.out.println(str2); // World,新字符串
15 System.out.println("================");
16 
17         String str3 = str1.substring(4, 7);
18         System.out.println(str3); // oWo
19 System.out.println("================");
20 
21 // 下面这种写法,字符串的内容仍然是没有改变的
22 // 下面有两个字符串:"Hello","Java"
23         // strA当中保存的是地址值。
24 // 本来地址值是Hello的0x666,
25 // 后来地址值变成了Java的0x999
26 String strA = "Hello";
27         System.out.println(strA); // Hello
28 strA = "Java";
29         System.out.println(strA); // Java
30 }
31 }

1.4.4

1.1.1    字符串的转换相关方法

String当中与转换相关的常用方法有:

public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值。
public byte[] getBytes():获得当前字符串底层的字节数组。
public String replace(CharSequence oldString, CharSequence newString)
将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串。
备注:CharSequence意思就是说可以接受字符串类型。

1.4.4.1案例代码六

 1 /*
 2 String当中与转换相关的常用方法有:
 3 
 4 public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值。
 5 public byte[] getBytes():获得当前字符串底层的字节数组。
 6 public String replace(CharSequence oldString, CharSequence newString):
 7 将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串。
 8 备注:CharSequence意思就是说可以接受字符串类型。
 9  */
10 public class Demo04StringConvert {
11 
12 public static void main(String[] args) {
13 // 转换成为字符数组
14 char[] chars = "Hello".toCharArray();
15         System.out.println(chars[0]); // H
16 System.out.println(chars.length); // 5
17 System.out.println("==============");
18 
19 // 转换成为字节数组
20 byte[] bytes = "abc".getBytes();
21 for (int i = 0; i < bytes.length; i++) {
22             System.out.println(bytes[i]);
23         }
24         System.out.println("==============");
25 
26 // 字符串的内容替换
27 String str1 = "How do you do?";
28         String str2 = str1.replace("o", "*");
29         System.out.println(str1); // How do you do?
30 System.out.println(str2); // H*w d* y*u d*?
31 System.out.println("==============");
32 
33         String lang1 = "会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!";
34         String lang2 = lang1.replace("你大爷的", "****");
35         System.out.println(lang2); // 会不会玩儿呀!****!****!****!!!
36 }
37 }

1.4.5   字符串的分割方法

分割字符串的方法:
public String[] split(String regex):按照参数的规则,将字符串切分成为若干部分。

注意事项:
split方法的参数其实是一个“正则表达式”,今后学习。
今天要注意:如果按照英文句点“.”进行切分,必须写"\\."(两个反斜杠)

1.4.5.1案例代码七

 1 /*
 2 分割字符串的方法:
 3 public String[] split(String regex):按照参数的规则,将字符串切分成为若干部分。
 4 
 5 注意事项:
 6 split方法的参数其实是一个“正则表达式”,今后学习。
 7 今天要注意:如果按照英文句点“.”进行切分,必须写"\\."(两个反斜杠)
 8  */
 9 public class Demo05StringSplit {
10 
11 public static void main(String[] args) {
12         String str1 = "aaa,bbb,ccc";
13         String[] array1 = str1.split(",");
14 for (int i = 0; i < array1.length; i++) {
15             System.out.println(array1[i]);
16         }
17         System.out.println("===============");
18 
19         String str2 = "aaa bbb ccc";
20         String[] array2 = str2.split(" ");
21 for (int i = 0; i < array2.length; i++) {
22             System.out.println(array2[i]);
23         }
24         System.out.println("===============");
25 
26         String str3 = "XXX.YYY.ZZZ";
27         String[] array3 = str3.split("\\.");
28         System.out.println(array3.length); // 0
29 for (int i = 0; i < array3.length; i++) {
30             System.out.println(array3[i]);
31         }
32     }
33 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/battlecry/p/9390023.html

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

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

相关文章

21分钟 MySQL 入门教程

转自 21分钟 MySQL 入门教程 一、MySQL的相关概念介绍二、Windows下MySQL的配置配置步骤MySQL服务的启动、停止与卸载三、MySQL脚本的基本组成四、MySQL中的数据类型五、使用MySQL数据库登录到MySQL创建一个数据库选择所要操作的数据库创建数据库表六、操作MySQL数据库向表中插…

node-sass报错解决方法

node-sass报错解决方法 node-sass报错解决方法 在Vue.js中&#xff0c;每一个vue文件都是一个组件&#xff0c;在.vue文件中可以将模板&#xff0c;脚本&#xff0c;样式写在一起&#xff0c;便于组织整个组件。在使用template&#xff0c;script时&#xff0c;编写css样式时&a…

微软人工智能愿景:根植于研发 寄望于“对话”

过去25年来&#xff0c;微软公司持续投入人工智能的发展愿景。现在&#xff0c;借助全新发布的聊天机器人Zo、Cortana Decices SDK和智能套件、以及扩展智能工具&#xff0c;这一愿景即将成为现实。12月13日&#xff0c;在旧金山的一次小聚会上&#xff0c;微软全球执行副总裁、…

H264 TS/ES

http://blog.csdn.net/heanyu/article/details/6229724

Java中Semaphore(信号量) 数据库连接池

计数信号量用来控制同时访问某个特定资源的操作数或同时执行某个指定操作的数量 A counting semaphore.Conceptually, a semaphore maintains a set of permits. Each acquire blocks if necessary until a permit is available, and then takes it. Each release adds a permi…

Visual Studio for Mac Preview离线下载安装

Visual Studio for Mac离线下载安装。 环境&#xff1a;OS X EI Caption 10.11.2 .NET Core SDK 1.1 需预先安装 .NET Core 1.1 SDK macOS版下载地址:https://go.microsoft.com/fwlink/?LinkID835011 安装SDK需先安装openssl。 brew update brew install openssl mkdir -p /us…

LOAM_velodyne学习(一)

在研读了论文及开源代码后&#xff0c;对LOAM的一些理解做一个整理。 文章&#xff1a;Low-drift and real-time lidar odometry and mapping 开源代码&#xff1a;https://github.com/daobilige-su/loam_velodyne 系统概述 LOAM的整体思想就是将复杂的SLAM问题分为&#x…

实战Vue简易项目(2)定制开发环境

本章内容包含上一章思考的解决&#xff0c;还有一些其它的定制... CSS预处理 关于对.vue文件模块处理规则的配置依次可在build/webpack.base.conf.js->build/vue-loader.conf.js->build/utils.js文件中跟踪&#xff1b; 而loaders的关键在于build/vue-loader.conf.js文件…

LINUX framebuffer

http://wangshh03.blog.163.com/blog/static/49103415201001231317484/ 一、FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口。 Linux是工作在保护模式下&#xff0c;所以用户态进程是无法象DOS那样使用显卡BIOS里提供的中断调用来实现直接写屏&…

[POI2007]POW-The Flood

题目描述 给定一张地势图&#xff0c;所有的点都被水淹没&#xff0c;现在有一些关键点&#xff0c;要求放最少的水泵使所有关键点的水都被抽干 输入输出格式 输入格式&#xff1a; In the first line of the standard input there are two integers and , separated by a sin…

LOAM_velodyne学习(二)

LaserOdometry 这一模块&#xff08;节点&#xff09;主要功能是&#xff1a;进行点云数据配准&#xff0c;完成运动估计 利用ScanRegistration中提取到的特征点&#xff0c;建立相邻时间点云数据之间的关联&#xff0c;由此推断lidar的运动。我们依旧从主函数开始&#xff1…

户外穿越

晚上很早就睡了&#xff0c;并且&#xff0c;太过激动&#xff0c;所以早上四点五十分就被惊醒&#xff0c;然后到早上闹钟响。 早上匆匆忙吃过早餐&#xff0c;就赶去坐车&#xff0c;到登山之前&#xff0c;坐了大巴车&#xff0c;又坐了景区的车&#xff0c;景区的路是山路十…

【oracle】关于创建表时用default指定默认值的坑

刚开始学create table的时候没注意&#xff0c;学到后面发现可以指定默认值。于是写了如下语句&#xff1a; 当我查询的时候发现&#xff0c;查出来的结果是这样的。。 很纳闷有没有&#xff0c;我明明指定默认值了呀&#xff0c;为什么创建出来的表还是空的呢&#xff1f;又跑…

Makefile中用宏定义进行条件编译(gcc -D)/在Makefile中进行宏定义-D

在源代码里面如果这样是定义的&#xff1a; #ifdef MACRONAME //可选代码 #endif 那在makefile里面 gcc -D MACRONAMEMACRODEF 或者 gcc -D MACRONAME 这样就定义了预处理宏&#xff0c;编译的时候可选代码就会被编译进去了。 对于GCC编译器&#xff0c;有如下选项&…

python安装与配置

首先下载python地址&#xff1a; https://www.python.org/downloads/release/python-361/下载页面中有多个版本&#xff1a; web-based installer 是需要通过联网完成安装的 executable installer 是可执行文件(*.exe)方式安装 embeddable zip file 嵌入式版本&#xff0c;可…

[OpenGL ES 03]3D变换:模型,视图,投影与Viewport

[OpenGL ES 03]3D变换&#xff1a;模型&#xff0c;视图&#xff0c;投影与Viewport 罗朝辉 (http://blog.csdn.net/kesalin) 本文遵循“署名-非商业用途-保持一致”创作公用协议 系列文章&#xff1a;[OpenGL ES 01]OpenGL ES之初体验[OpenGL ES 02]OpenGL ES渲染管线与着色器…

LOAM_velodyne学习(三)

终于到第三个模块了&#xff0c;我们先来回顾下之前的工作&#xff1a;点云数据进来后&#xff0c;经过前两个节点的处理可以完成一个完整但粗糙的里程计&#xff0c;可以概略地估计出Lidar的相对运动。如果不受任何测量噪声的影响&#xff0c;这个运动估计的结果足够精确&…

监控视频线种类 视频信号传输介绍及各种视频接口的传输距离

一.视频信号接口 监控视频线种类介绍&#xff1a; 按照材料区分有SYV及SYWV两种&#xff0c;绝缘层的物理材料结构不同&#xff0c;SYV是实心聚乙烯电缆&#xff0c;SYWV是高物理发泡电缆&#xff0c;物理发泡电缆传输性能优于聚乙烯。 S--同轴电缆 Y--聚乙烯 V--聚氯乙烯 W…

免费节假日API 更新新功能了 新增农历信息返回

感谢大家对免费节假日API的支持.最近看了别家的api于是增加了一些新功能即获取日期的农历信息. 这个新功能还处于测试阶段如有问题欢迎反馈 检查一个日期是详细信息 https://tool.bitefu.net/jiari/?d20180101&info1 返回值 {"status": 1,"type": 1,…

新手算法学习之路----二叉树(二叉树最大路径和)

摘抄自&#xff1a;https://segmentfault.com/a/1190000003554858#articleHeader2 题目&#xff1a; Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1/ \2 3Return 6. 思…