java – 如何忽略子字符串中的空格?
发布时间:2020-12-15 04:29:50 所属栏目:Java 来源:网络整理
导读:我有一个文本框,根据用户输入提出建议,我的一个文本框是基于位置的. 问题是,如果用户在伊利诺伊州的芝加哥打字,一切正常,但如果他们输入伊利诺伊州的芝加哥,建议就会停止.两者之间的唯一区别是逗号后面的空格. 我该如何解决这个问题,以便即使用户在逗号后放
我有一个文本框,根据用户输入提出建议,我的一个文本框是基于位置的.
问题是,如果用户在伊利诺伊州的芝加哥打字,一切正常,但如果他们输入伊利诺伊州的芝加哥,建议就会停止.两者之间的唯一区别是逗号后面的空格. 我该如何解决这个问题,以便即使用户在逗号后放入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); 字符串变量用于调用数据库;这就是为什么我必须消除任何空格. 解决方法
你可以使用location.replaceAll(“”,“”) 用于将位置提取到城市,州 String location [] = location.split(“,”); 现在 String city=location[0]; String state=location[1]; 编辑:(对于谁) String location="New York,NY"; String loc[]=location.split(","); String city=loc[0].trim(); String state=loc[1].trim(); System.out.println("City->"+city+"nState->"+state); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读