String.substring

public class Demo {
    public static void main(String[] args) {
        File file = new File("aaa.png");
        String name = file.getName();
        String s = name.substring(name.lastIndexOf("."));
        String sub = name.substring(name.lastIndexOf(".") + 1);
        System.out.println(name);
        System.out.println(s);
        System.out.println(sub);
    }
}
 控制台打印
 aaa.png
.png    
png

String . lastIndexOf(".") 从字符串最后出现 . 的地方截取
包括 . 在内
. 为 索引 0开始截取

此用法 常用于 获取 文件的格式

#######
在后面使用过程中
public class SubstringTest {

public static void main(String[] args) {
    String path="111.222.333.444";
    String substring = path.substring(0, path.lastIndexOf("."));
    System.out.println(substring);//111.222.333

    String substring1 = path.substring(path.indexOf("."));
    System.out.println(substring1);//.222.333.444

    String substring2 = path.substring(path.indexOf("."),path.lastIndexOf("."));
    System.out.println(substring2);//.222.333
}

}

规律是 [左边, 右边) 类似区间的效果,左边包含,右边不包含

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值