如何在Java中的第三个斜杠后获取字符串 [英] How to fetch string after the third slash in java

查看:371
本文介绍了如何在Java中的第三个斜杠后获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在第三个斜杠之后获取字符串.但是我不知道该怎么做.我已经使用了split,但是那不是我想要的.

I am trying to fetch the string after the third slash. But i don' know how to do it. I have used split but that's not what I want.

for(String obj2: listKey.getCommonPrefixes()){
    Map<String, String> map = new HashMap<String, String>();
    String[] id = obj2.split("/");
    if (id.length > 3) {
        String name = id[3];
        map.put("id", name);
        map.put("date", "null");
        map.put("size", String.valueOf(obj2.length()));
        keys.add(map);
    }
}

id[3]仅给我id[3],但我要在第三个斜杠之后输入所有内容?我该怎么办?

id[3] gives me only id[3] but i want everything after the third slash? how can i do that?

推荐答案

您可以替换

 String[] id = obj2.split("/");

作者

 String[] id = obj2.split("/", 4);

来自 javadoc :

limit参数控制应用图案的次数,因此会影响结果数组的长度.如果限制n大于零,则将最多应用n-1次该模式,该数组的长度将不大于n,并且该数组的最后一个条目将包含除最后一个匹配的定界符之外的所有输入.如果n为非正数,则该模式将被尽可能多地应用,并且数组可以具有任何长度.如果n为零,则将尽可能多地应用该模式,该数组可以具有任何长度,并且尾随的空字符串将被丢弃.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

这篇关于如何在Java中的第三个斜杠后获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆