简体   繁体   English

如何忽略子字符串中的空格?

[英]How can I ignore spaces in a substring?

I have a textbox that gives out suggestions based on user input and one of my textboxes is location based. 我有一个文本框,根据用户输入提出建议,我的一个文本框是基于位置的。

The problem is, if a user types in Chicago,IL , everything works, but if they type in Chicago, IL , the suggestions stop. 问题是,如果用户在伊利诺伊州的 芝加哥打字,一切正常,但如果他们输入伊利诺伊州的芝加哥,建议就会停止。 The only difference between the two is the space after the comma. 两者之间的唯一区别是逗号后面的空格。

How can I fix this, so that even if a user puts in 2 or 4 spaces after the comma it still shows the same results as the first case? 我该如何解决这个问题,以便即使用户在逗号后放入2或4个空格,它仍然显示与第一个案例相同的结果?

This is my code: 这是我的代码:

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 ?";

  }

I have also tried this: 我也试过这个:

 String state = location.substring(location.indexOf(",".trim()) + 1);

The string variables are used to make calls to the database; 字符串变量用于调用数据库; that is why I have to eliminate any spaces. 这就是为什么我必须消除任何空格。

How can I fix this, so that even if a user puts in 2 or 4 spaces after the comma it still shows the same results as the first case? 我该如何解决这个问题,以便即使用户在逗号后放入2或4个空格,它仍然显示与第一个案例相同的结果?

you can use location.replaceAll(" ", "") 你可以使用location.replaceAll(" ", "")

for extracting the location into city,state you can use split() method as 为了将位置提取到city,state您可以使用split()方法作为

String location[]=location.split(",");

Now 现在

    String city=location[0];
    String state=location[1];

EDIT:(for Whome) 编辑:(对于谁)

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);

you were in the right direction by using trim(). 通过使用trim(),你在正确的方向。 However, you put it in the wrong place. 但是,你把它放在了错误的地方。
",".trim() will always yield "," . ",".trim()将始终产生"," you want to trim the result of the substring operation: 你想修剪子串操作的结果:

String state = location.substring(location.indexOf(",") + 1).trim();

try using java.lang.String trim() function in the correct place. 尝试在正确的位置使用java.lang.String trim()函数。

trim on ",".trim() will produce "," . 修剪",".trim()将产生","

Need to trim() the final result. 需要trim()最终结果。

if (location.contains(",")) {
String city = location.substring(0, location.indexOf(",")).trim();
String state = location.substring(location.indexOf(",")).trim();
}

Trim the entire result. 修剪整个结果。 For example: 例如:

String city = (location.substring(0, location.indexOf(","))).trim();
String state = (location.substring(location.indexOf(",") + 1)).trim();

Use 使用

String state = location.substring(location.indexOf(",") + 1).trim();

Instead of 代替

String state = location.substring(location.indexOf(",".trim()) + 1);

That should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM