【问题标题】:Split String to single variables between separators将字符串拆分为分隔符之间的单个变量
【发布时间】:2020-09-24 15:27:04
【问题描述】:

我正在尝试找到一种更优雅/内存占用更少的方法来通过分隔符拆分 char 数组。

字符数组:“192.168.178\nWiFiSSID\nWiFiPassword\n123”

我的白痴分裂方式:

void convertPayload() 
{
qrData = (char*)mypayload;

String ssidtmp = qrData;
ssidtmp.remove(qrData.indexOf("\n"), qrData.length()+1);
EEPROM.writeString(ssidAddr,ssidtmp);
EEPROM.commit();

String passtmp = qrData;
passtmp.remove(0, passtmp.indexOf("\n")+1);
passtmp.remove(passtmp.indexOf("\n"),passtmp.length()+1);

EEPROM.writeString(passAddr, passtmp);
EEPROM.commit();

String modulenrtmp = qrData;
modulenrtmp.remove(0, modulenrtmp.indexOf("\n") + 1);
modulenrtmp.remove(0, modulenrtmp.indexOf("\n") + 1);
modulenrtmp.remove(modulenrtmp.indexOf("\n") , modulenrtmp.length());
int modNRINT = modulenrtmp.toInt();

EEPROM.write(moduleNraddress, modNRINT);
EEPROM.commit();

String ftptmp = qrData;
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(ftptmp.indexOf("\n") , ftptmp.length());

EEPROM.writeString(ftpAddr, ftptmp);
EEPROM.commit();

EEPROM.writeBool(configModeAddr, true);
EEPROM.commit();

//indicate QR succesfully read
blinkBurst(2, 300);

ESP.restart();
}

如您所见,我正在创建不必要的字符串。正确的做法是什么?

干杯并感谢您的宝贵时间!

【问题讨论】:

标签: c++ string split


【解决方案1】:

如果你可以编辑mypayload,你可以使用std::strtok

void convertPayload() {

String ssidtmp = std::strtok(mypayload, "\n");
EEPROM.writeString(ssidAddr,ssidtmp);
EEPROM.commit();

String passtmp = std::strtok(nullptr, "\n");
EEPROM.writeString(passAddr, passtmp);
EEPROM.commit();

String modulenrtmp = std::strtok(nullptr, "\n");
int modNRINT = modulenrtmp.toInt();
EEPROM.write(moduleNraddress, modNRINT);
EEPROM.commit();

String ftptmp =  = std::strtok(nullptr, "\n");
EEPROM.writeString(ftpAddr, ftptmp);
EEPROM.commit();

EEPROM.writeBool(configModeAddr, true);
EEPROM.commit();

//indicate QR succesfully read
blinkBurst(2, 300);

ESP.restart();
}

【讨论】:

  • 感谢您的回复!我只是调查了一下......似乎我可以用谷歌搜索更多......
猜你喜欢
  • 1970-01-01
  • 2011-08-22
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2013-12-03
  • 1970-01-01
  • 2015-11-30
相关资源
最近更新 更多