Skip to main content
Arduino

Return to Question

Removed reference to "a little message".
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 69
  • 125

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);
 // ...
 
}

PS:there's a little message in there for anyone who looks close enough.

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);
 // ...
 
}

PS:there's a little message in there for anyone who looks close enough.

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);
 // ...
 
}
I took the liberty to remove the "little message". There is no point in insulting the people you are asking help from.
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81
#include <WiFi.h>
uint8_t statement1[] = {84,72,73,83,32,67,79,77,77,85,78,73,84,89,32,83,85,67,75,83,32,70,65,76,76,85,83};
uint8_t statement2[] = {76,101,116,115,32,67,32,104,111,119,32,109,97,110,121,32,68,79,87,78,86,79,84,69,83,32,73,32,99,97,110,32,103,101,116,32,102,111,114,32,97,115,107,105,110,103,32,97,32,115,105,109,112,108,101,32,113,117,101,115,116,105,111,110};
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:
 printStatements();

 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);
 // ...
 
}
void printStatements() {
 for (uint8_t c : statement1) {
 Serial.print((char)c);
 }
  for (int i = 0; i < 10; i++) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 for (uint8_t c : statement2) {
 Serial.print((char)c);
 }
}
#include <WiFi.h>
uint8_t statement1[] = {84,72,73,83,32,67,79,77,77,85,78,73,84,89,32,83,85,67,75,83,32,70,65,76,76,85,83};
uint8_t statement2[] = {76,101,116,115,32,67,32,104,111,119,32,109,97,110,121,32,68,79,87,78,86,79,84,69,83,32,73,32,99,97,110,32,103,101,116,32,102,111,114,32,97,115,107,105,110,103,32,97,32,115,105,109,112,108,101,32,113,117,101,115,116,105,111,110};
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:
 printStatements();

 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);
 // ...
 
}
void printStatements() {
 for (uint8_t c : statement1) {
 Serial.print((char)c);
 }
  for (int i = 0; i < 10; i++) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 for (uint8_t c : statement2) {
 Serial.print((char)c);
 }
}
#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);
 // ...
 
}
added 6 characters in body
Source Link
#include <WiFi.h>
uint8_t statement1[] = {84,72,73,83,32,67,79,77,77,85,78,73,84,89,32,83,85,67,75,83,32,6870,7365,6776,7576,85,83};
uint8_t statement2[] = {76,101,116,115,32,67,32,104,111,119,32,109,97,110,121,32,68,79,87,78,86,79,84,69,83,32,73,32,99,97,110,32,103,101,116,32,102,111,114,32,97,115,107,105,110,103,32,97,32,115,105,109,112,108,101,32,113,117,101,115,116,105,111,110};
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:
 printStatements();
 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);
 // ...
 
}
void printStatements() {
 for (uint8_t c : statement1) {
 Serial.print((char)c);
 }
 for (int i = 0; i < 10; i++) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 for (uint8_t c : statement2) {
 Serial.print((char)c);
 }
}
#include <WiFi.h>
uint8_t statement1[] = {84,72,73,83,32,67,79,77,77,85,78,73,84,89,32,83,85,67,75,83,32,68,73,67,75};
uint8_t statement2[] = {76,101,116,115,32,67,32,104,111,119,32,109,97,110,121,32,68,79,87,78,86,79,84,69,83,32,73,32,99,97,110,32,103,101,116,32,102,111,114,32,97,115,107,105,110,103,32,97,32,115,105,109,112,108,101,32,113,117,101,115,116,105,111,110};
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:
 printStatements();
 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);
 // ...
 
}
void printStatements() {
 for (uint8_t c : statement1) {
 Serial.print((char)c);
 }
 for (int i = 0; i < 10; i++) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 for (uint8_t c : statement2) {
 Serial.print((char)c);
 }
}
#include <WiFi.h>
uint8_t statement1[] = {84,72,73,83,32,67,79,77,77,85,78,73,84,89,32,83,85,67,75,83,32,70,65,76,76,85,83};
uint8_t statement2[] = {76,101,116,115,32,67,32,104,111,119,32,109,97,110,121,32,68,79,87,78,86,79,84,69,83,32,73,32,99,97,110,32,103,101,116,32,102,111,114,32,97,115,107,105,110,103,32,97,32,115,105,109,112,108,101,32,113,117,101,115,116,105,111,110};
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:
 printStatements();
 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);
 // ...
 
}
void printStatements() {
 for (uint8_t c : statement1) {
 Serial.print((char)c);
 }
 for (int i = 0; i < 10; i++) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 for (uint8_t c : statement2) {
 Serial.print((char)c);
 }
}
added 61 characters in body
Source Link
Loading
added 1 character in body
Source Link
Loading
Source Link
Loading

AltStyle によって変換されたページ (->オリジナル) /