0

I want to replace the ":"(colon) in the MAC address with no char (remove the ":").

But I can't find any good solution.

String.replace(), can't replace with (void).

Note: String.replace(':',(char)0) //also doesn't work

String.remove() is ugly and requires that you know exactly where each iteration is (indexOf()), and this method doesn't return anything, it updates the string directly so you have to call : String.remove() for each occurence.

and String.substring() has most of the same problems as remove, except that it does return the substring, so you can do: String.substring(i,l).substring(i2,l)...

#include <WiFi.h>
String MAC;
char ssid[] = "WiFi SSID"
char psw[] = "WiFi PSW"
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin(ssid,psw);
  MAC = WiFi.macAddress();
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println("MAC Address unformatted")
  Serial.println(MAC);
  String MAC_f = MAC.replace(':', ' ');
  // Now print the MAC without the ":"
  Serial.println(MAC_f);
  // This replaces the ':' with a space.  I want to replace(remove) the ':' with no char
  // Looking for an elegant, simple, 1 liner to accomplish this.
  
  /* This looks horrible */
  MAC_f = MAC.substring(0,2)+MAC.substring(3, 5)+MAC.substring(6, 8)+MAC.substring(9, 11)+MAC.substring(12, 14)+MAC.substring(15, 17);
  /* .remove also looks terrible */
  MAC_f = MAC;
  MAC_f.remove(2,1);
  MAC_f.remove(2,1);
  // ...
  
}
1
  • Please do not attempt to answer questions in the comments. See How do comments work?. The comments on this question have been deleted, including the ones about "a special message". Appropriate action has been taken in regard to this message.
    – Nick Gammon
    Mar 1, 2022 at 6:59

3 Answers 3

3

Don´t use String() manipulations inside loop(). It causes heap fragmentation and program failure. Use a char array instead:

char MAC[18] = "01:23:45:67:89:AB"; // including terminating zero

for (int src = 0, dst = 0; src < sizeof(MAC); src++)
   if (MAC[src] != ':') MAC[dst++] = MAC[src];
2
  • 1
    It works because the terminating zero is copied to the end of the modified string
    – SBF
    Feb 28, 2022 at 13:09
  • @Instigator in which way is this inefficient? Do you know what String.replace() does?
    – Sim Son
    Feb 28, 2022 at 13:13
3

As stated by Mat, the empty string in C++ is "". This works:

MAC_f = MAC;
MAC_f.replace(":", "");

That being said, I concur with SBF's answer: String object are not friendly with the Arduino's memory. SBF's solution is better than String::replace().

1
0

others a right that String is not totally Arduino friendly, WiFi.macAddress() returns String, SO

though written for ESP32 this is an approach i used

 // Get the station interface MAC address.
 // @return String MAC, no camas
String getMacAddress(void)
{
    WiFi.mode(WIFI_AP_STA);   // required to read NVR before WiFi.begin()
    uint8_t baseMac[6];
    esp_read_mac( baseMac, ESP_MAC_WIFI_STA );  // Get MAC address for WiFi station
    char macStr[18] = { 0 };
    sprintf(macStr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
    return String(macStr);
}
// END getMacAddress() 
1
  • this maybe is how it should be done, but it doesn't answer the question
    – Juraj
    Mar 2, 2022 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.