如何用Arduino中的另一个字符串将字符串拆分

How to split a string by another string in Arduino?

本文关键字:字符串 拆分 另一个 何用 Arduino      更新时间:2023-10-16

我有一个字符数组,如下:

char array[] = "AAAA... A1... 3. B1.";

如何通过Arduino中的字符串"..."拆分此数组?我尝试了:

ptr = strtok(array, "...");

,输出为以下:

AAAA,
 A1,
 3,
 B1

,但我实际上希望输出为

AAAA,
A1,
3.B1.

如何获取此输出?

编辑:

我的完整代码是:

char array[] = "AAAA... A1... 3. B1.";
char *strings[10];
char *ptr = NULL;`enter code here` 
void setup()
{
       Serial.begin(9600);
       byte index = 0;
       ptr = strtok(array, "...");  // takes a list of delimiters
       while(ptr != NULL)
       {
            strings[index] = ptr;
            index++;
            ptr = strtok(NULL, "...");  // takes a list of delimiters
       }
       for(int n = 0; n < index; n++)
       {
          Serial.println(strings[n]);
       }
    }

主要问题是 strtok do not 在另一个字符串中找到字符串。strtok在字符串中寻找字符。当您将多个字符给strtok时,它会寻找这些字符的任何。因此,编写strtok(array, "...");与编写strtok(array, ".");完全相同。这就是为什么您在" 3"之后分手的原因。

有多种方法可以做您想做的事情。在下面,我将向您展示一个使用strstr的示例。与strtok不同,strstr功能确实在字符串中找到一个子字符串 - 正是您要寻找的内容。但是.. strstr不是令牌,因此需要一些额外的代码来打印子字符串。

这样的事情应该做:

int main()
{
  char array[] = "AAAA... A1... 3. B1...";
  char* ps = array;
  char* pf = strstr(ps, "..."); // Find first substring
  while(pf)
  {
    int len = pf - ps;          // Number of chars to print
    printf("%.*sn", len, ps);
    ps = pf + 3;
    pf = strstr(ps, "...");     // Find next substring
  }
  return 0;
}

您可以将自己的 split 用作 strtok ,但第二个参数的角色:

#include <stdio.h>
#include <string.h>
char * split(char *str, const char * delim)
{
  static char * s;
  char * p, * r;
  if (str != NULL)
    s = str;
  p = strstr(s, delim);
  if (p == NULL) {
    if (*s == 0)
      return NULL;
    r = s;
    s += strlen(s);
    return r;
  }
  r = s;
  *p = 0;
  s = p + strlen(delim);
  return r;
}
int main()
{
  char s[] = "AAAA... A1... 3. B1.";
  char * p = s;
  char * t;
  while ((t = split(p, "...")) != NULL) {
    printf("'%s'n", t);
    p = NULL;
  }
  return 0;
}

汇编和执行:

/tmp % gcc -g -pedantic -Wextra s.c
/tmp % ./a.out
'AAAA'
' A1'
' 3. B1.'
/tmp % 

我在"'"之间打印以显示返回空间,因为我不确定您想要它们,所以DeLim不仅是...在这种情况下

因为您将其标记为C ,这是代码的C '版本:

#include <iostream>
using std::cout;
using std::endl; 
#include <vector>
using std::vector;
#include <string>
using std::string;

class T965_t
{
   string         array;
   vector<string> strings;
public:
   T965_t() : array("AAAA... A1... 3. B1.") 
      {
         strings.reserve(10);
      }
   ~T965_t() = default;
   int operator()() { return setup(); } // functor entry
private: // methods
   int setup()
      {
         cout << endl;
         const string pat1 ("... ");
         string s1 = array; // working copy
         size_t indx = s1.find(pat1, 0); // find first ... pattern
         // start search at ---------^
         do
         {
            if (string::npos == indx)  // pattern not found
            {
               strings.push_back (s1); // capture 'remainder' of s1
               break;                  // not found, kick out
            }
            // else 
            // extract --------vvvvvvvvvvvvvvvvv
            strings.push_back (s1.substr(0, indx));  // capture
            // capture to vector
            indx += pat1.size(); // i.e. 4
            s1.erase(0, indx); // erase previous capture 
            indx = s1.find(pat1, 0);          // find next
         } while(true);
         for(uint n = 0; n < strings.size(); n++)
            cout << strings[n] << "n";
         cout << endl;
         return 0;
      }
}; // class T965_t
int main(int , char**) { return T965_t()(); } // call functor

输出:

AAAA
A1
3. B1.

注意:我离开更改" 3. B1"。到" 3.b1。",并在每行的末端添加逗号(最后一个)作为OP的练习。

我正在寻找一个拆分功能,但我没有找到满足我要求的一个,所以我做了一个,这对我来说,当然,我将来会做一些改进,但这让我摆脱了麻烦。

,但也有strtok函数,可以更好地使用它。https://www.delftstack.com/es/howto/arduino/arduino-strtok/

我有分裂功能

arduino代码:

void split(String * vecSplit, int dimArray,String content,char separator){
if(content.length()==0)
    return;
 content = content + separator;
 int countVec = 0;
 int posSep = 0;
 int posInit = 0;
 while(countVec<dimArray){
   posSep = content.indexOf(separator,posSep);
   if(posSep<0){
    return;
   } 
 countVec++;       
 String splitStr = content.substring(posInit,posSep);
 posSep = posSep+1; 
 posInit = posSep;
 vecSplit[countVec] = splitStr;
 countVec++;    

}}

llamada a funcion:

smsContent = "APN:4g.entel;DOMAIN:domolin.com;DELAY_GPS:60";
String vecSplit[10];
split(vecSplit,10,smsContent,';');
for(int i = 0;i<10;i++){
   Serial.println(vecSplit[i]);    
}

字符串输入:APN:4gentel;域:domolin.com; delay_gps:60

输出:

  • apn:4g.entel
  • 域:domolin.com
  • delay_gps:60
  • 重置:true

在此处输入图像描述