1

I'm working an project where the program is supposed to analyze other project, the other projects are located in a specisfic directory. The projects are named under a standard similar to the following:

Projectname-version-downloadingDate example:

ElasticSearch-1.0-20160417

right now the program can return the whole project name as one string and save it to CSV file, using the following methods:

  private String projectName;

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

and here is the method call for writing the name of the project:

    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;
    }
}

now my question is how can I split the name of the project into three parts and write each one separated in the CSV file?

the project name, the version and the download date? meaning that it should take the project name from the substring located before the first "-" and the version after the first "-" and finally the date after the second "-"?

any tips how to do that?

thanks

Abdullah Asendar
  • 574
  • 1
  • 5
  • 31
user5923402
  • 217
  • 3
  • 12

1 Answers1

3

Use the string.split method.

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
Joe
  • 6,773
  • 2
  • 47
  • 81