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
Post a Comment