【发布时间】:2015-12-17 17:42:16
【问题描述】:
我有一个文本框,它根据用户输入给出建议,我的一个文本框是基于位置的。
问题是,如果用户输入 Chicago,IL,一切正常,但如果他们输入 Chicago, IL,建议就会停止。两者的唯一区别是逗号后面的空格。
我该如何解决这个问题,以便即使用户在逗号后放置 2 或 4 个空格,它仍然显示与第一种情况相同的结果?
这是我的代码:
if (location.contains(",")) {
// the city works correctly
String city = location.substring(0, location.indexOf(","));
// state is the problem if the user puts any space after the comma
// it throws everything off
String state = location.substring(location.indexOf(",") + 1);
String myquery = "select * from zips where city ilike ? and state ilike ?";
}
我也试过这个:
String state = location.substring(location.indexOf(",".trim()) + 1);
字符串变量用于调用数据库;这就是为什么我必须消除任何空格。
【问题讨论】:
-
试试
String state = location.substring(location.indexOf(",") + 1).trim(); -
使用 state.trim() 和 city.trim()