1

I am receiving a location address from GPS(Latitude/Longitude) programmatically; however, sometimes I receive a location address with special/unwanted characters.

When I send the addresses containing special/unwanted characters to the server, they are declined by the server.

Example 1: , delhi, 中�

Example 2: स्क

How can I prevent this issue?

Evan Bashir
  • 5,501
  • 2
  • 25
  • 29
harikrishnan
  • 1,985
  • 4
  • 32
  • 63

1 Answers1

0

You can use regex to avoid this problem and match all the characters are of US-ASCII code point type.

String testText [] = new String[] { "delhi", "中�", 
    "à¤", "à¥à¤• ", "tessst½", "some valid test text1213"
};

for (String str : testText) {
    if (str.matches("\\A\\p{ASCII}*\\z")) {
        //do something
        Log.d("TAG", str + " - String is valid");
    }
}

If you are using Java 8 you can do it this way:

textInfoFromLocation.chars().allMatch(c -> c < 128)

In case if you just want to check each character, do the following:

for (Character c : str.toCharArray()) {
    if (c > 127) //character is invalid
        Log.d("TAG","Character " + c + " is invalid");
}

More information could be found in this answer

Community
  • 1
  • 1
Fedor Tsyganov
  • 992
  • 13
  • 30
  • thank you Fedor. I have tried this. here am getting true/false only. i tried the above , delhi, 中å?½ to check this string. am getting false only. even, i want to filter the string from the junk characters. – harikrishnan Oct 13 '16 at 09:41