java - How can I ignore spaces in a substring? -


i have textbox gives out suggestions based on user input , 1 of textboxes location based.

the problem is, if user types in chicago,il, works, if type in chicago, il, suggestions stop. difference between 2 space after comma.

how can fix this, if user puts in 2 or 4 spaces after comma still shows same results first case?

this code:

if (location.contains(",")) { // city works correctly    string city = location.substring(0, location.indexof(","));     // state problem if user puts space after comma    // throws off        string state = location.substring(location.indexof(",") + 1);     string myquery = "select * zips city ilike ? , state ilike ?";    } 

i have tried this:

 string state = location.substring(location.indexof(",".trim()) + 1); 

the string variables used make calls database; why have eliminate spaces.

how can fix this, if user puts in 2 or 4 spaces after comma still shows same results first case?

you can use location.replaceall(" ", "")

for extracting location city,state can use split() method as

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

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -