Search code examples
androidstringcomparisonwifiwifimanager

String.replace is returning extra quotation marks


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?


Solution

  • 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.