我有一个文本框,根据用户输入提出建议,我的一个文本框是基于位置的.
问题是,如果用户在伊利诺伊州的芝加哥打字,一切正常,但如果他们输入伊利诺伊州的芝加哥,建议就会停止.两者之间的唯一区别是逗号后面的空格.
我该如何解决这个问题,以便即使用户在逗号后放入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);
字符串变量用于调用数据库;这就是为什么我必须消除任何空格.