簡體   English   中英

如何刪除字符串值兩邊的方括號

[英]How to remove square brackets on either side of string value

當我在表上選擇一行時,它將以字符串形式打印出所選項目,如圖所示。 如何從弦的兩邊移除方括號。

以下是我的數據填充TextFields的代碼。 您可以看到它成功填充,但是帶有[]。

表和輸出

表和輸出

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

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

您可以使用input.substr(1, input.length - 1)刪除第一個和最后一個字符,或者如果沒有input.replace("[","").replace("]","")可以使用input.replace("[","").replace("]","")刪除它們其他情況也是可能的。

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

無需“后處理”即可產生所需的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM