如何将字符串子串到java中的最后一个点(。)?

时间:2016-03-24 05:19:40

标签: java split

我有一个文本文件data.txt。我想将数据输入到Hashmap中并进行一些数据映射。什么时候我没有点()来达到值。我会收到错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

如何通过跳过没有点(。)的条目来克服它。

我创建了一个小片段来说明我的问题。

  static HashMap<String, String> newList = new HashMap<>();
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String inputFile = "data.txt";
        BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
        String line;

        while ((line = brInput.readLine()) != null) {
            newList.put(line, "x");
        }

        for (Map.Entry<String, String> entry : newList.entrySet()) {

            String getAfterDot = entry.getKey();
            String[] split = getAfterDot.split("\\.");
            String beforeDot = "";
            beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
            System.out.println(beforeDot);
        }

    }

data.txt中

0
0.1
0.2
0.3.5.6
0.2.1
2.2

我打印地图时的预期结果(不需要按顺序)

0
0
0.3.5
0.2
2

6 个答案:

答案 0 :(得分:5)

使用String方法lastIndexOf(int ch)

int lastIndxDot = st.lastIndexOf('.');

st.substring(0, lastIndxDot);将是您想要的子字符串。如果它返回-1,则没有&#39;。&#39;在字符串中。

编辑:

for (Map.Entry < String, String > entry: newList.entrySet()) {
    String getAfterDot = entry.getKey();
    int lastIndxDot = getAfterDot.lastIndexOf('.');
    if (lastIndxDot != -1) {
        String beforeDot = getAfterDot.substring(0, lastIndxDot);
        System.out.println(beforeDot);
    }
}

答案 1 :(得分:1)

尝试使用此代码:

static HashMap < String, String > newList = new HashMap < > (); 
public static void main(String[] args) throws FileNotFoundException, IOException {

    String inputFile = "input";
    BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
    String line = null;

    while ((line = brInput.readLine()) != null) {

        newList.put(line, "x");
    }

    for (Map.Entry < String, String > entry: newList.entrySet()) {

        String getAfterDot = entry.getKey();
        if (getAfterDot.contains(".")) {
            String[] split = getAfterDot.split("\\.");
            String beforeDot = "";
            beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
            System.out.println(beforeDot);
        }
    }

}

我用上面的代码得到了这个输出:

0
0
2
0.3.5
0.2

答案 2 :(得分:0)

在调用字符串上的子字符串函数之前,可以使用getAfterDot.contains(“。”)检查字符串是否有点。

static HashMap<String, String> newList = new HashMap<String, String>();
public static void main(String[] args) throws FileNotFoundException, IOException {
    String inputFile = "data.txt";
    BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
    String line;
    while ((line = brInput.readLine()) != null) {
        newList.put(line, "x");
    }
    for (Map.Entry<String, String> entry : newList.entrySet()) {
        String getAfterDot = entry.getKey();
        String[] split = getAfterDot.split("\\.");
        String beforeDot = "";
        if(getAfterDot.contains(".")){
            beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
            System.out.println(beforeDot);
        }
    }

}

答案 3 :(得分:0)

一个选项是将String.replaceAll()与正则表达式一起使用,该正则表达式提取在句点之前的字符串部分(如果句点存在)。

for (Map.Entry<String, String> entry : newList.entrySet()) {
    String getAfterDot = entry.getKey();
    String beforeDot = getAfterDot.replaceAll("(.*)\\..*", "$1");
    System.out.println(beforeDot);
}

答案 4 :(得分:0)

    String[] lines = new String[]{"0", "0.1", "0.2", "0.3.5.6", "0.2.1", "2.2"};
    for (String line : lines) {
        int lastIndexOf = line.lastIndexOf(".");
        if (lastIndexOf > -1) {
            line = line.substring(0, lastIndexOf);
            System.out.println(line);
        }
    }

答案 5 :(得分:-1)

我只是想我会指出这可以在一行代码中完成:

foo.substringAfterLast(".")