简体   繁体   English

Java字符串拆分,无法按我预期的方式运行

[英]Java string splitting, not working the way I envisioned it to be

So I'm trying to make a fan java program for a game for a few of my friends, I'm trying to read the contents of a text file to store into an array/arraylist in the future but I'm unable to get string split working the way I hoped it would. 所以我想为我的一些朋友制作一个游戏的粉丝Java程序,我试图读取文本文件的内容,以便将来存储到数组/数组列表中,但是我无法获取字符串拆分以我希望的方式工作。 I tried examples from this place that worked for people just to see if it will work but I get the same output. 我从这个地方尝试了一些对人们有用的示例,只是看它是否会起作用,但是我得到了相同的输出。

importCards.java importCards.java

BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("dark.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            read = in.readLine();
            String[] splited = read.split("||");
            for (String part : splited) {
                System.out.println(part);
            }
        }
    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }

The text file is formatted as follows 文本文件的格式如下

17||Dark Soul Endor||Dark||2||1||Human||Main Characters||5|500000||833||126||78||23||Release of Spirit - Dark||Dissolve all Light Runestones to inflict Dark on all enemies||Power of Dark||Dark Attack x 150%

However when I tried and printed it I got this 但是当我尝试打印时我得到了这个

1 1

8 8

| |

| |

D d

a 一种

r [R

k ķ

and so on 等等

Regular expression have special tokens such as | 正则表达式具有特殊标记,例如| which don't mean | 这不代表| literally, but in this case mean OR. 从字面上看,但是在这种情况下是OR。 ie in this case "" OR "" OR "" When you match by empty string it splits each character into it's own string. 即在这种情况下, "" OR "" OR ""当您用空字符串匹配时,它将每个字符分成自己的字符串。

What you intended was 你原本打算

String[] splited = read.split("\\|\\|");

You might be wondering, why two \\\\| 您可能想知道,为什么两个\\\\| and not \\| 而不是\\| The reason is that \\ has a special meaning in Java and regular expressions. 原因是\\在Java和正则表达式中具有特殊含义。 When you write \\\\| 当您写\\\\| in Java it becomes "\\|" 在Java中,它变成"\\|" as a string, ie two characters, which as a regular expression is | 作为字符串,即两个字符,作为正则表达式为| literally, instead of a special token. 从字面上看,而不是特殊标记。

BTW I suggest you use , or \\t (tab) instead. 顺便说一句,我建议你使用,\\t (选项卡)来代替。 This will not only be smaller but you will be able to edit the file in your favourite spread sheet editor such as Excel or LibreCalc. 这不仅会更小,而且您将能够在自己喜欢的电子表格编辑器(例如Excel或LibreCalc)中编辑文件。 This makes it much easier to see where the columns and even add/remove a column or change their order. 这样可以更轻松地查看列的位置,甚至可以添加/删除列或更改其顺序。

Diablo II used , for its raw data files. 暗黑破坏神II使用的,其原始数据文件。 ;) ;)

If you read CSV or TSV files, there is libraries to make it easier to read/import such as OpenCSV's CSVReader 如果您读取CSV或TSV文件,则可以使用诸如OpenCSV的CSVReader之类的库来简化读取/导入操作。

split() takes a regex, and | split()使用正则表达式,并且| is a regex special char which means "or", so you're splitting by "empty string" or "empty string" or "empty string" . 是一个正则表达式特殊字符,表示“或”,因此您将按“空字符串”或“空字符串”或“空字符串”进行拆分。

You need to escape them: "\\\\|\\\\|" 您需要转义它们: "\\\\|\\\\|" .

You need to scape || 您需要scape || as \\\\|\\\\| \\\\|\\\\|

Eg: 例如:

String str = "17||Dark Soul Endor||Dark||2||1||Human||Main Characters||5|500000||833||126||78||23||Release of Spirit - Dark||Dissolve all Light Runestones to inflict Dark on all enemies||Power of Dark||Dark Attack x 150%";
String[] splited = str.split("\\|\\|");
for(String i:splited){
    System.out.println(i);
}

Remove read = in.readLine(); 删除read = in.readLine(); because you read 2 line in your example and change te split to: 因为您在示例中读取了2行并将te split更改为:

String[] splited = read.split("\\|\\|");

becase || || is a special char in regex. 是正则表达式中的特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM