java 中几个常见的字符串问题

字符串的处理(String下方法)

1)拿到某一个字符串后检查是否是以某一个指定的字符串开头

String.startsWith(“指定的字符串”)

public void startString() {
        String attachmentPath = "file:C:\\Windows\\AppPatch\\Custom";
        if (attachmentPath.startsWith("file:")) {
            System.out.println("是以‘file:’开头的!");
        }
}

String.StartsWith(“指定的字符串”,“开始检查的位置”);

public void startString() {
        String attachmentPath = "file:C:\\Windows\\AppPatch\\Custom";
        // 从第3个字符开始检查
        if (attachmentPath.startsWith("file:",3)) {
            System.out.println("是以‘file:’开头的!");
        }
}

2)字符串截取

String.substring(“字符串开始截取位置”);   
// 例如:
"string".substring(2); 返回  “ring”,从字符串的第二位开始截取后面的

3)字符串的替换

String.replaceAll(“匹配此字符串的正则表达式”,“用来替换每个匹配项的字符串”); 匹配失败后返回原字符串

public void findByStr() {
		String str = "bdaasfasfannnnnakkkdnasnl\\72";
		// 将连续的重叠字符替换成“#”
		// 不管重叠几次都只替换成一个 #
		String at = str.replaceAll("(.)\\1+", "#");
		System.out.println(at);	
	}

结果: “ bd#sfasfa#a#dnasnl\72

4)检查字符串结尾是否以某一个字符串结尾

String.endsWith("结尾的字符串");     // 和检查开始的同理
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值