0

请我需要帮助来完成以下任务:

我在数组中有许多如下文件:

jack+0.txt
jack+2.txt
jack+4.txt
tim+0.txt
tim+2.txt
tim+4.txt
raph+0.txt
raph+2.txt
raph+4.txt
wells+0.txt
wells+2.txt
wells+4.txt
etc.

我想编写一个程序来执行以下操作:

if the filename is like *0.txt, a++;
if the filename is like *2.txt, b++;
if the filename is like *4.txt, c++;

需要的核心帮助是使用正则表达式(在 Java 中)。

4

4 回答 4

2

这个想法,如果你有少量不同的结局:

if (filename.endsWith("0.txt")) a++;
于 2012-10-11T14:51:15.393 回答
2

我只会在加号上进行字符串拆分,然后检查第二部分。

String[] parts = s.split("+");
if (parts[1].equals("0.txt") {

} else // the rest of the tests
于 2012-10-11T14:51:36.750 回答
1
for (File file : files) {
    if (file.getName().matches(".*0[.]txt") {
        a++;
    } else if ...
}
于 2012-10-11T14:51:53.320 回答
0

这将从文件名中取出数字,然后你可以做一个 switch case

string filename;

int category = Integer.parseInt(filename.substring(filename.length() - 5, filename.length() - 4))
于 2012-10-11T14:53:17.717 回答