如何删除字符串值

时间:2018-03-08 13:30:05

标签: java javafx

当我在表格上选择一行时,它会在字符串中打印出所选项目,如图所示。如何从字符串的任一侧删除方括号。

下面是我用数据填充TextFields的代码。您可以看到它已成功填充,但使用[]。

表和输出

the table and output

tableView.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
    String input = newValue.toString();
    String[] str_array = input.split(", ");
    String code = str_array[0]; 
    String name = str_array[1]; 
    String lecturer = str_array[2];
    String year = str_array[3];
    String semester = str_array[4];

    codeInput.setText(code);
    nameInput.setText(name);
    lectureInput.setText(lecturer);
    yearInput.setText(year);
    semesterInput.setText(semester);
    System.out.println(input);
});

3 个答案:

答案 0 :(得分:3)

newValue应该是Object。假设它是SchoolInfo的对象,变量为String codeString nameString lecturerint yearint semester。您的Object应该有GettersSetters。你应该做点什么。

警告:其他一些答案可能会完成工作,但这些想法非常糟糕。你试图处理这些数据的方式是个坏主意。

tableView.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
    SchoolInfo tempSchoolInfo = (SchoolInfo)newValue;

    String code = tempSchoolInfo.getCode(); 
    String name = tempSchoolInfo .getName(); 
    String lecturer = tempSchoolInfo.getLecturer;
    int year = tempSchoolInfo.getYear();
    int semester = tempSchoolInfo.getSemester();

    codeInput.setText(code);
    nameInput.setText(name);
    lectureInput.setText(lecturer);
    yearInput.setText(Integer.toString(year));
    semesterInput.setText(Integer.toString(semester));
    System.out.println(input);
});

答案 1 :(得分:1)

您可以使用input.substr(1, input.length - 1)删除第一个和最后一个字符,也可以只使用input.replace("[","").replace("]","")删除它们,如果没有其他可能的话。

答案 2 :(得分:1)

Arrays.stream(str_array).collect(Collectors.joining(","));

这样可以产生所需的结果而无需“后期处理”