![](/img/trans.png)
[英]How to remove the square brackets at the beginning and end of the special characters String?
[英]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 code
, String name
, String lecturer
, int year
和int semester
变量。 您的Object
应该具有Getters
和Setters
。 你应该做类似的事情。
警告:某些其他答案可能会完成工作,但不是很好的主意。 您尝试处理此数据的方式是一个坏主意。
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.