String 类的运用

news/2025/3/14 22:52:39/

目录

1.字符串构造

 2.String对象的比较

2.1==比较是否引用同一个对象

2. 2boolean equals(Object anObject)

2.3int compareTo(String s) 方法: 按照字典序进行比较

2.4int compareToIgnoreCase(String str) 

3.字符串查找

 4.2大小写转换

4.3字符串转数组

 4.4 格式化

5.字符串替换

5.1把字符串中的某个字符替换成指定字符

5.2 把字符串中的某个字符串替换成指定字符串

5.3替换首个内容 

6.字符串拆分

6.1String[] split(String regex)

 6.2String[] split(String regex, int limit)

7 字符串截取

7.1String substring(int beginIndex)

7.2String substring(int beginIndex, int endIndex)

8.String trim()


1.字符串构造

String类提供的构造方式非常多,常用的就以下三种:

(1)使用常量串构造
String str1="wang";
(2)直接 new String 对象
String str3 = new String("123");
(3)使用字符数组进行构造
char arr[]={'a','b','c'};
String str4 = new String(arr);

                                字符串的内存储存图(图1-1)

看下面代码回答运行结果

public class Text {public static void main(String[] args) {char arr[]={'a','b','c'};String str1 = new String(arr,1,2);System.out.println(str1);String str2=new String();System.out.println(str2);String str3 ="";System.out.printf(str3);System.out.println(str3.length());String str4=null;System.out.println(str4);}
}

运行结果:

 

 

但如果强行这串代码

 运行结果:

 

对str3,str4经行解析:


 2.String对象的比较

2.1==比较是否引用同一个对象

String s1 = new String ( "hello" );
String s2 = new String ( "hello" );
String s3 = new String ( "world" );
String s4 = s1 ;
System . out . println ( s1 == s2 ); // false
System . out . println ( s2 == s3 ); // false
System . out . println ( s1 == s4 ); // true

小结:

对于基本类型变量,==比较两个变量中存储的值是否相同,对于引用类型变量,==比较两个引用变量引用的是否为同一个对象

2. 2boolean equals(Object anObject)

方法:按照字典序比较

String s1 = new String ( "hello" );
String s2 = new String ( "hello" );
String s3 = new String ( "wello" );
System . out . println ( s1 . equals ( s2 )); // true
System . out . println ( s1 . equals ( s3 )); // false

解析: 

equals 比较: String 对象中的逐个字符
 虽然 s1 s2 引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出 true s1与 s3 引用的不是同一个对象,而且两个对象中内容也不同,因此输出 false

2.3int compareTo(String s) 方法: 按照字典序进行比较

equals 不同的是, equals 返回的是 boolean 类型,而 compareTo 返回的是 int 类型。
具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3

2.4int compareToIgnoreCase(String str) 

与compareTo方式相同,但是忽略大小写比较
String s1 = new String ( "abc" );
String s2  = new String ( "ABc" );
System . out . println ( s1 . compareToIgnoreCase ( s2 )); // 相同输出 0

 

3.字符串查找

字符串查找也是字符串中非常常见的操作, String 类提供的常用查找的方法:
方法
功能
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
public static void main ( String [] args ) {
String s = "aaabbbcccaaabbbccc" ;
System . out . println ( s . charAt ( 3 )); // 'b'
System . out . println ( s . indexOf ( 'c' )); // 6
System . out . println ( s . indexOf ( 'c' , 10 )); // 15
System . out . println ( s . indexOf ( "bbb" )); // 3
System . out . println ( s . indexOf ( "bbb" , 10 )); // 12
System . out . println ( s . lastIndexOf ( 'c' )); // 17
System . out . println ( s . lastIndexOf ( 'c' , 10 )); // 8
System . out . println ( s . lastIndexOf ( "bbb" )); // 12
System . out . println ( s . lastIndexOf ( "bbb" , 10 )); // 3

4.转化

4.1数值转化字符串

 4.2大小写转换

String toUpperCase()
字符串转大写
String toLowerCase()
字符串转小写

 这两个函数只转换字母。

String s1 = "wang" ;
String s2 = "HELLO" ;
// 小写转大写
System . out . println ( s1 . toUpperCase ());
// 大写转小写
System . out . println ( s2 . toLowerCase ());

 运行结果: 

4.3字符串转数组

String s = "hello" ;
// 字符串转数组
char [] ch = s . toCharArray ();
// 数组转字符串
String s2 = new String ( ch );

 4.4 格式化

String s = String . format ( "%d-%d-%d" , 2002 , 5 ,2 4 );
System . out . println ( s );

 运行结果:  


 

5.字符串替换

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

5.1把字符串中的某个字符替换成指定字符

String str="aa1212bbac";
String str1=str.replace('a','0');
System.out.println(str1);

运行结果:  

5.2 把字符串中的某个字符串替换成指定字符串

String str="aa1212bbacbb";
String str2=str.replace("bb","ww");
System.out.println(str2);

运行结果:  

5.3替换首个内容 

String str="aa1212bbacbb";
String str3=str.replaceFirst("bb","ww");
System.out.println(str3);

运行结果:  

 


6.字符串拆分

6.1String[] split(String regex)

将字符串全部拆分
String str="123 0 456 789";
String arr[]=str.split(" ");
for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);
}

运行结果:  

 6.2String[] split(String regex, int limit)

将字符串以指定的格式,拆分为limit组
String str="123 0 456 789";
String arr[]=str.split(" ",2);
for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);
}

运行结果:  

拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义.  

注意事项 :
1. 字符 "|","*","+" 都得加上转义字符,前面加上 "\\" .
2. 而如果是 "\\" ,那么就得写成 "\\\\" .
3. 如果一个字符串中有多个分隔符,可以用 "|" 作为连字符 .
String str = "name\\wang*age=21";
String arr1[]=str.split("\\*|\\\\");
for (int i = 0; i < arr1.length; i++) {System.out.println(arr1[i]);
}

运行结果:  

 


7 字符串截取

从一个完整的字符串之中截取出部分内容。

7.1String substring(int beginIndex)

从指定索引截取到结尾
String str="123abcwang";
System.out.println(str.substring(4));

运行结果: 

7.2String substring(int beginIndex, int endIndex)

截取部分内容
String str="123abcwang";
System.out.println(str.substring(4,7));

运行结果: 

注意事项 :
1. 索引从 0 开始
2. 注意前闭后开区间的写法 , substring(0, 5) 表示包含 0 号下标的字符 , 不包含 5 号下标

8.String trim()

去掉字符串中的左右空格,保留中间空格
String str = " hello world " ;
System . out . println ( "[" + str + "]" );
System . out . println ( "[" + str . trim () + "]" );

运行结果: 


以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

 


http://www.ppmy.cn/news/1018638.html

相关文章

TCP滑动窗口和拥塞控制

目录 滑动窗口什么是滑动窗口为什么要使用滑动窗口滑动窗口的工作原理滑动窗口会出现的几种问题数据包丢失怎么解决&#xff1f;ACK丢失怎么解决&#xff1f; 拥塞控制拥塞控制是什么&#xff1f;拥塞控制的实现理解拓展&#xff1a;拥塞控制是如何判断网络拥塞情况的&#xff…

Linux在终端显示Git分支等信息

环境&#xff1a;RHEL7.5 在环境变量文件中添加以下内容&#xff1a; function git-branch-name { git symbolic-ref --short -q HEAD 2>/dev/null } function git-branch-prompt {local branchgit-branch-nameif [ $branch ]; then printf "(%s)" $branch; fi }…

【软件工程】3 ATM系统的设计

目录 3 ATM系统的设计 3.1体系结构设计 3.2 设计模式选择 3.3 补充、完善类图 3.4 数据库设计 3.4.1 类与表的映射关系 3.4.2 数据库设计规范 3.4.3 数据库表 3.5 界面设计 3.5.1 界面结构设计 3.5.2 界面设计 3.5.2.1 功能界面设计 3.5.2.2 交互界面 总博客&…

类与对象【下】

欢迎来到Cefler的博客&#x1f601; &#x1f54c;博客主页&#xff1a;那个传说中的man的主页 &#x1f3e0;个人专栏&#xff1a;题目解析 &#x1f30e;推荐文章&#xff1a;题目大解析2 目录 &#x1f449;&#x1f3fb;初始化列表初始化列表注意点 &#x1f449;&#x1f…

解决遥感技术在生态、能源、大气等领域的碳排放监测及模拟问题

以全球变暖为主要特征的气候变化已成为全球性环境问题&#xff0c;对全球可持续发展带来严峻挑战。2015年多国在《巴黎协定》上明确提出缔约方应尽快实现碳达峰和碳中和目标。2019年第49届 IPCC全会明确增加了基于卫星遥感的排放清单校验方法。随着碳中和目标以及全球碳盘点的现…

Springboot项目集成Durid数据源和P6Spy以及dbType not support问题

项目开发阶段&#xff0c;mybatis的SQL打印有占位符&#xff0c;调试起来还是有点麻烦&#xff0c;随想整合P6Spy打印可以直接执行的SQL&#xff0c;方便调试&#xff0c;用的Durid连接池。 Springboot项目集成Durid <dependency><groupId>com.alibaba</group…

springboot mongodb 配置多数据源

我想要的效果是&#xff0c;一个类统一管理多数据源&#xff0c;我传个参数进去&#xff0c;它就能返回我对应的mongotemplate 但是根据"mongbodb 多数据源"的关键词&#xff0c;找不到我想要的效果。 网上大多都是明确知道自己是几个数据源&#xff0c;然后每个数…

Json简述(C++)

目录 1.介绍 2.格式 3.底层 3.1数据对象表示 3.2序列化接口 3.3反序列化接口 4.使用 1.介绍 Json&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;其最早是为JavaScript编程语言设计的格式。不过发发展至今&#xff0c;Jso…