JAVA 学习-字符串处理

匹配

内容较多,写在另一篇博客

正则表达式的使用:https://blog.csdn.net/weixin_43732798/article/details/100067094

Pattern与Matcher的使用:https://blog.csdn.net/weixin_43732798/article/details/100073668

切割

String[] split(String regex)

按照指定的条件分割字符串

public class Main {
	
	public static void main(String[] args) {
		String str = "h,e,l,l,o";
		String[] result = str.split(",");
		for (String string : result) {//输出hello
			System.out.print(string);
		}
	}
	
}

替换

String replace(char oldChar,char newChar)---不支持正则

String replaceFirst(String regex,String replacement)---支持正则(只替换第一个)

String replaceAll(String regex, String replacement)---支持正则

将某个字符串或符合某个规则的字符串替换成另一个字符串

public class Main {
	
	public static void main(String[] args) {
		String str = "123123";
		System.out.println("-----------将1替换成#-----------");
		System.out.println("全部替换:"+str.replace("1", "#"));
		System.out.println("只替换第一个:"+str.replaceFirst("1", "#"));
		
		
		System.out.println("\n-------使用正则将一串数字变成#-------");
		System.out.println("replace不支持正则:"+str.replace("\\d+", "#"));
		System.out.println("replaceAll支持正则:"+str.replaceAll("\\d+", "#"));
	}
	
}
输出
-----------将1替换成#-----------
全部替换:#23#23
只替换第一个:#

-------使用正则将一串数字变成#-------
replace不支持正则:123123
replaceAll支持正则:#

 

刷个题练练手?

CCF-201509-3 模板生成系统:https://blog.csdn.net/weixin_43732798/article/details/100066242

CCF-201803-3 URL映射:https://blog.csdn.net/weixin_43732798/article/details/100104580

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值