String.replace is returning extra quotation marks

1k views Asked by At

Ok so this may be a very stupid question but I honestly can't quite figure out what's going on here, I'm trying to compare the current SSID to another variable like this:

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String SSID = wifiInfo.getSSID().replace("SSID: ","");
if(myWifi.equals(SSID)){

} else {

}

Now, as you know, wifiInfo.getSSID() returns a String like this SSID: wifi1. Let's say myWifi's value is wifi1, if it gets compared to wifiInfo.getSSID() it will say it's false because it is, so I'm trying to replace the SSID: part for nothing, hopefully getting only the SSID name (wifi1), so the strings can be compared and return true. But using the code above makes the SSID variable have the value of "wifi1", and so the app says it's false because "wifi1" is not the same as wifi1.

Why is this happening?

2

There are 2 answers

3
Alex Newman On BEST ANSWER

Simple just use .replaceAll("\"",""); after removing the SSID part. So you will have:

String SSID = wifiInfo.getSSID().replace("SSID: ","").replaceAll("\"","");

This will remove all the " no matter where they are in the String.

2
JavaadPatel On

I would use a String.Contains to check for the string you want.

bool wifiNameExists = wifiInfo.Contains("wifi1")

You can ofcourse replace "wifi1" with a string containing the wifi name you wish to search for.