如何 trim 一个字符串两次并获得不同的信息?

how to trim a string twice and getting different info?

我正在处理一个项目,该程序应该分析其他项目,其他项目位于特定目录中。这些项目的命名标准类似于以下内容:

项目名称-版本-下载日期示例:

ElasticSearch-1.0-20160417

现在程序可以return将整个项目名称作为一个字符串保存到CSV文件中,方法如下:

  private String projectName;

public void setProjectName(String projectName){
    this.projectName = projectName;
}
public String getProjectName(){
    return projectName;
}

这里是写入项目名称的方法调用:

    private void writeReport() {
    BufferedWriter out = null;
    try {
        File file = new File("out.csv");
        boolean exists = file.exists();
        FileWriter fstream = new FileWriter(file, exists /*=append*/);
        out = new BufferedWriter(fstream);
        if (!exists) {
            out.write("File Name;");

            out.write(System.getProperty("line.separator"));
        }

        out.write(String.valueOf(newFiles.getProjectName()) + ";"); //method call


        out.write(System.getProperty("line.separator"));
      //  }

        //Close the output stream
        out.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        //  return;
    }
}

现在我的问题是如何将项目名称拆分为三个部分并将每个部分分开写入 CSV 文件中?

项目名称、版本和下载日期?这意味着它应该从第一个“-”之前的子字符串和第一个“-”之后的版本以及第二个“-”之后的日期中获取项目名称?

有什么技巧吗?

感谢

使用string.split方法。

String string = "ElasticSearch-1.0-20160417";
String[] parts = string.split("-");
String projectName = parts[0]; // ElasticSearch
String versionNumber= parts[1]; // 1.0
String downloadDate= parts[2]; // 20160417