Java学习笔记18——深入学习字符串(1),2024Python进阶学习资料

res += world;
res += “!”;


 上面的操作,等同于调用String类的concat方法:



String world = “World”;
String res = “Hello”;

res = res.concat(world);
res = res.concat(“!”);


 在Java中,“+”和“+=”运算符,除了可以应用于字符串对象,还可以应用其它的数据类型,例如:



String world = “World”;
int i =3;
float f = 4.5f;
char ch = ‘a’;
boolean b = true;

System.out.println(world + i + f + ch + b);


输出结果:World34.5atrue


**5、操作字符串的常用方法:**


(1)获取字符串的长度:调用String类的length()方法,注意返回的是字符的数量,不是该字符串占用内存的大小。



String str = “Java 从入门到精通”;
System.out.println(str.length());


(2)查找字符或字符串


查找字符串中是否存在某个字符或者某个子字符串,使用String类重载的4个indexOf方法,如下:



public int indexOf(int ch) // 参数ch是要查找的字符
public int indexOf(int ch,int fromIndex) // 参数fromIndex表示从哪个索引位置开始查找
public int indexOf(String str) // 参数str是要查找的子字符串
public int indexOf(String str,int fromIndex)


 indexOf方法返回一个整数,默认从索引0位置开始查找,返回字符或者子字符串第一次出现的索引,如果没有找到,返回值是-1。



public class StringCompareTo {
public static void main(String[] args) {
String str = “Hello World”;
System.out.println(str.indexOf(‘o’)); // 输出4
System.out.println(str.indexOf(‘o’,5)); // 输出7
System.out.println(str.indexOf(“World”)); // 输出6
System.out.println(str.indexOf(“Welcome”)); // 输出-1
}
}


 String类也提供了反向查找字符或者子字符串的一组重载的lastIndexOf方法,如下所示:



public int lastIndexOf(int ch) // 参数ch是要查找的字符
public int lastIndexOf(int ch,int fromIndex) // 参数fromIndex表示从哪个索引位置开始查找
public int lastIndexOf(String str) // 参数str是要查找的子字符串
public int lastIndexOf(String str,int fromIndex)


将上面代码修改如下,结果将是:



public class StringCompareTo {
public static void main(String[] args) {
String str = “Hello World”;
System.out.println(str.lastIndexOf(‘o’)); // 输出7
System.out.println(str.lastIndexOf(‘o’,5)); // 输出4
System.out.println(str.lastIndexOf(“World”)); // 输出6
System.out.println(str.lastIndexOf(“Welcome”)); // 输出-1
}
}


请注意lastIndexOf()方法,是从字符串的最后一个字符开始,查找字符或者子字符串在字符串第一次出现的字符,返回这个字符在字符串中的索引位置(从正面开始,索引值从0开始)。


(3)判断字符串的开始和结尾:startsWith() / endsWith()方法



// 测试字符串是否以指定的前缀开始
public boolean startsWith(String prefix)

// 测试字符串从指定的索引开始的子字符串是否以指定前缀开始
// 参数toffset表示在字符串中开始查找的位置
public boolean startsWith(String prefix,int toffset)

// 测试字符串是否以指定的后缀结尾
public boolean endsWith(String suffix)

String str = “代码test.jsp”;

System.out.println(str.stratsWith(“代码”)); // true
System.out.println(str.endsWith(“.jsp”)); // false


(4)获取指定索引位置的字符:charAt(int index)



String str = “hello”; // 定义并初始化字符串变量str
int len = str.length(); // 获取字符串变量str的长度
char[] chaArr = new char[len]; // 定义一个字符型数组对象chArr,并初始化长度为str的字符个数

for (int i=0; i<len; i++) {
chArr[i] = str.charAt(i); // 给字符型数组变量chArr添加元素
}

System.out.println(charArr); // 输出:hello


另外,字符串转化为字符数组这个功能也经常会用到,Java中的String类提供了toCharArray()方法来实现。定义如下:


* public char[] toCharArray()


(5)截取子字符串



// 从指定索引位置截取子字符串,直到字符串的末尾
// public String substring(int beginIndex)

String str = “Hello World”;

System.out.println(str.substring(6)); // 输出:World

// 从指定索引位置截取子字符串,直到索引endIndex-1处的字符,不包含endIndex索引字符
// public String substring(int beginIndex,int endIndex)
System.out.println(str.substring(6,10)); // 输出:Worl


substring方法可以和indexOf方法结合使用,先查找是否存在特定的字符或子字符串,在得到索引后开始截取。



String str = “Will Smith”;
int index = str.indexOf(“Smith”); // indexOf方法查找到子字符串“Smith”存在,返回字符“S”的位置
if (index != -1) {
System.out.println(str.substring(index,index+“Smith”.length())); // 输出Smith
}


(6)分割字符串


public String[] split(String regex)


参数regex是用于进行匹配的正则表达式字符串,split方法根据参数regex返回一个保存了拆分后的各个字符串的数组。



String str = “zhansan,lisi,wangwu,zhaoliu”;
String[] names = str.split(“,”);

for (String name : names) {
System.out.println(name);
}


 输出如下:


zhansan  
 lisi  
 wangwu  
 zhaoliu


(7)替换字符或字符串


应用中替换字符串的部分内容是特别常见的。例如在某个字符串中含有敏感信息,或者不合规的字符串序列,在向用户显示时可以替换为\*号。String类有两个重载的replace方法,一个用于替换字符,一个用于替换字符串。


* public String replace(char oldChar,char newChar)


用newChar替换字符串中所有的oldChar,并返回替换后的字符串。如果oldChar在这个String对象表示的字符串序列中没有出现,则直接返回该对象的引用,否则返回一个替换后的新的String对象。


* public String replace(CharSequence target , CharSequence replacement)


用replacement指定的字符序列替换字符串中出现的target字符序列。


String类中还有两个使用正则表达式来匹配要替换的字符串的方法:


* public String replaceFirst(String regex,String replacement)


用replacement替换此字符串匹配给定的正则表达式(regax)的第一个子串。


* public String replcceAll(String regex,String replacement)


用replacement替换此字符串匹配给定的正则表达式(regax)的所有子串。



String str = “habcjkmkabc”;
Syttem.out.println(str.replaceFirst(“abc”,“def”); // 输出:hdefjkmkabc
Syttem.out.println(str.replaceAll(“abc”,“def”); // 输出:hdefjkmkdef


上面没有使用到正则表达式。


(8)合并字符串


在String类中有一个静态方法join,可以通过指定一个分隔符来合并字符串。


* public static String join(CharSequence delimiter, CharSequence... elements)
* delimited:分割字符串的分隔符
* elements:是变长参数,可以传入任意多个字符串



String names = String.join(“,”, “张山”,“李四”,“王二”);
System.out.println(names); // 输出:张山,李四,王二


(9)重复字符串



String str = “abc”;
System.out.println(str.repeat(5)); //输出abcabcabcabcabc


* public String repeat(int count)


将字符串重复参数count指定的次数,并串联起来返回一个新的字符串。


(10)大小写转换


* public String toLowerCase()  :把字符串全部字符都转换为小写
* public String toUpperCase()  :把字符串全部字符都转换为大写


Java没有提供首字母转为大写或小写方法,可以使用substring()和上面的方法结合实现相应的功能:



public class Main {
public static void main(String[] args) {
String word = “hello”;
System.out.println(capitalizeFirstLetter(word)); // 输出 “Hello”
System.out.println(uncapitalizeFirstLetter(word)); // 输出 “hello”
}

// 把首字母大写  
public static String capitalizeFirstLetter(String original) {  
    if (original == null || original.length() == 0) {  
        return original;  
    }  
    return original.substring(0, 1).toUpperCase() + original.substring(1);  
}  

// 把首字母小写  
public static String uncapitalizeFirstLetter(String original) {  
    if (original == null || original.length() == 0) {  
        return original;  
    }  
    return original.substring(0, 1).toLowerCase() + original.substring(1);  
}  

}


(11)去掉字符串的首尾空白


空白包含空格、制表符、换行、换页和回车,在字符串中属于合法的字符,但是字符串首尾的空白通常是没有用的,一般是误输入产生的。调用String类的trim()方法可以去掉:


public String trim()    // 删除首尾空白


public String strip()   // 删除首尾空白


public String stripLeading()   // 删除首空白


**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/09fe5305b4392dcc93bea35c7470be03.png)
![img](https://img-blog.csdnimg.cn/img_convert/42b2b7a69ed58baad4123510db8af6ba.png)
![](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)
![](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)
![](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png) 
![](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)**
![img](https://img-blog.csdnimg.cn/img_convert/b001d9940b43a591303ba45b37edb06f.png)



现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。



分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

![](https://img-blog.csdnimg.cn/img_convert/21b2604bd33c4b6713f686ddd3fe5aff.png)




[**一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!**](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)

**AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算**

604bd33c4b6713f686ddd3fe5aff.png)




[**一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!**](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)

**AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算**

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值