如何解决从单个 JTextField 获取多个字符串
我在对话框中有这个文本字段
dialog.setSize(350,350);
dialog.getContentPane().add(BorderLayout.CENTER,new JTextField(text));
我的目标是当用户在该文本字段中添加多个字符串时,提取文本并将其拆分为字符串,然后能够在数据库中搜索每个字符串,如下所示:
var res = request_handler.search(
"SELECT Name,ingredients FROM " +
"food WHERE FIND_IN_SET('"+containerObj+"',ingredients)");
我正在考虑这样的事情:
String foodSearch = ActionField.getText();
int index = 0;
//create a contaner for the strings
ArrayList<String> container = new ArrayList<>();
container.add("");
//loop until end of string
while(index <= foodSearch.length()){
//if we aren't at new string
if(foodSearch.charAt(index) != ','){
//get the char
char temp = foodSearch.charAt(index);
//then append that char to the String in the container
}
else{
container.add("");
}
++index;
}
基本上将使用逗号符号作为字符串之间的分隔符,循环遍历整个对象。但是有没有更好的方法来处理和解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。