StringSplitter库

它用于将一个字符串按照指定的字符进行分割和管理。

1. StringSplittter.h

#ifndef StringSplitter_h
#define StringSplitter_h

#include <Arduino.h>

class StringSplitter{
    private:
        static const unsigned int MAX = 5;
        String op[MAX];
        int count = 0;
        int countOccurencesOfChar(String s, char c);

    public:
        StringSplitter(String s, char c, unsigned int l);       
        int getItemCount();
        String getItemAtIndex(int index);
};

#endif
  1. 头文件保护,这些指令确保在编译时只包含一次头文件,防止重复定义。
  2. 库,用于支持Arduino平台相关的函数和定义。
  3. 类定义(class StringSplitter)
    • 私有部分(private:):包括静态常量 MAX,用于定义数组op的最大大小。以及一个字符串数组op[MAX]和两个私有成员变量countcountOccurencesOfChar
    • 公共部分(public:):包括构造函数StringSplitter和两个公共成员函数getItemCountgetItemAtIndex
  4. 成员变量:
    • static condt unsigned int MAX = 5;静态常量,定义了数字op的最大大小为5。
    • String op[MAx]; 存储分割后的子字符串的数组。
    • int count = 0 计数器,记录当前存储的子字符串的数量。
  5. 成员函数:
    • StringSplitter(String s,char c, unsigned int 1 ): 构造函数,接收到一个字符串"s",一个字符 ‘c’ (用于分割字符串),以及一个无符号整数 ‘1’ 。
    • int getItemCount(): 返回当前存储的字符串数量。
    • String getItemAtIndex(int index):返回指定索引位置的子字符串。
  6. 头文件结束,结束头文件的定义。

2. StringSplitter.cpp

StringSplitter::StringSplitter(String s, char c, unsigned int limit)
{
    count = countOccurencesOfChar(s, c) + 1;  

    if(count <= 1 || limit <= 1)
    {
        count = 1;
        op[0] = s;
        return;
    }

    if(count > limit) count = limit;
    if(count > MAX) count = MAX;

    String d = String(c);
    String first;
    String second = s;

    int current = 0;
    while(second.indexOf(d) > -1)
    {
        if(current >= (count - 1))
        {
      //current++;
        break;
        }
    for (int i = 0; i < second.length(); i++) 
    {
      if (second.substring(i, i + 1) == d)
      {
        first = second.substring(0, i);
        second = second.substring(i + 1);
        if(first.length() > 0)
            op[current++] = first;
        break;
      }
    }
  }
    //current = (current < MAX - 1) ? current : MAX;
  if(second.length() > 0)
        op[current] = second;
    //  else
    //  --count;
}

int StringSplitter::countOccurencesOfChar(String s, char c)
{
  int size = 0;
  for(int x = 0; x < s.length(); (s[x] == c) ? size++ : 0, x++);
  return size;
}

int StringSplitter::getItemCount()
{
  return count;
}

String StringSplitter::getItemAtIndex(int index)
{
    if((index >= 0) && (index < count))
        return op[index];
  else
        return "";
}

StringSplitter类构造函数的一部分,用于初始化对象并进行字符串分割。

  1. 计算分割后的子字符串的数量

    • count = countOccurencesOfChar(s, c) + 1;

      • countOccurencesOfChar(s,c)这是一个私有成员函数,用于计算字符串 sc出现的次数。
      • count 的值被设为 countOccurencesOfChar(s, c) + 1 ,表示分割后的子字符串的数量。
  2. 处理无效情况

if(count <= 1 || limit <= 1)
{
    count = 1;
    op[0] = s;
    return;
}
  • 如果 count 小于等于1或者 limit 小于等于1,则将 count 设置为1,表示不进行分割,直接将整个字符串 s 存储在数组中。

  • op[0] = s; 将整个输入字符串 s 存储在数组的第一个位置。

  • return; 提前结束构造函数的执行。

  1. 限制子字符串的数量
if(count > limit)
    count = limit;
if(count > MAX)
    count = MAX;
  • 如果 count 超过了 limit ,则将 count 设置为 limit
  • 如果 count 超过了 MAX (数组 op 的最大容量),则将 count设置为 MAX
  1. 执行字符串分割
String d = String(c);
String first;
String second = s;
int current = 0;

while (second.indexOf(d) > -1) 
{
    if (current >= (count - 1)) 
    {
        break;
    }
    for (int i = 0; i < second.length(); i++) 
    {
        if (second.substring(i, i + 1) == d) 
        {
            first = second.substring(0, i);
            second = second.substring(i + 1);
            if (first.length() > 0)
                op[current++] = first;
            break;
        }
    }
}
  • 使用 while 循环和 for 循环来逐个查找分隔符 c ,并将字符串 s 分割成多个子字符串。
  • first 存储每次找到的子字符串, second 不断更新为剩余未处理的部分。
  • 将找到的子字符串存储在 op 数组中,同时更新 current 指示当前存储的位置。
  1. 处理剩余部分
if(second.length() > 0)
{
    op[current] = second;
}
  • 如果 second 还有剩余未处理的部分(即未包含在之前的分割中),则将剩余部分作为最后一个子字符串存储在 op 数组中。
  1. 获取字符出现次数
int StringSplitter::countOccurencesOfChar(String s, char c)
{
    int size = 0;
    for(int x = 0; x < s.length(); (s[x] == c) ? size++ : 0, x++);
    return size;
}

countOccurencesOfChar 函数:计算字符串 s 中字符 c 出现的次数。

  • 使用 for 循环遍历字符串 s 的每个字符,如果当前字符等于 c ,则增加 size 计数器。最终返回 size ,即字符 c 在字符串 s 中出现次数。
  1. 获取子字符串数量
int StringSplitter::getItemCount()
{
    return count;
}

获取当前对象中存储的子字符串的数量。

  • 直接返回成员变量 count 的值,该变量记录了对象中存储的子字符串数量。
  1. 获取子字符串
String StringSplitter::getItemAtIndex(int index)
{
    if ((index >= 0) && (index < count))
        return op[index];
    else
        return "";
}

根据指定的索引 index 获取存储在对象中的子字符串。

  • 首先检查 index 是否在有效范围内(即大于等于0且小于 count
  • 如果是,则返回 op[index],即存储在 op 数组中索引为 index 的子字符串
  • 如果 index 不在有效范围内,则返回空字符串 ""

3. second.indexOf()函数

"second.indexOf()" 函数是用于在字符串 "second" 中查找子字符串或字符的位置的函数,它返回找到的子字符串或字符的索引位置,如果未找到,则返回 '-1' 。

For example:

  • 如果调用 "second.indexOf("abc")" ,它将在字符串 'second' 中查找子字符串 "abc" 的位置,并返回第一个 "abc" 出现的索引位置。如果未找到 "abc" ,则返回 '-1' 。

  • 如果调用 "second.indexOf('x')" ,它将在字符串 'second' 中查找字符 'x' 的位置,并返回第一个 'x' 出现的索引位置。如果未找到 'x', 则返回 '-1' 。

4. second.substring(beginIndex)函数

"second.substring(beginIndex)" 函数是用于字符串 'second' 中提取字符串的函数,它可以接收一个或两个参数,具体取决于您希望提取的子字符串的方式。

For example:

  • "second.substring(beginIndex)" : 这个用法接收一个参数 'beginIndex' ,表示子字符串的起始索引(包括起始索引处的字符)。函数将从 'beginIndex' 处开始提取字符,直到字符串的末尾,并返回提取的子字符串。

  • "second.substring(beginIndex, endIndex)" :这个用法接受两个参数 'beginIndex' 和 'endIndex' ,分别表示子字符串的起始索引(包括起始索引处的字符)和结束索引(不包括结束索引处的字符)。
    函数将从 'beginIndex' 处开始提取字符,一直提取到 'endIndex-1' 处的字符,并返回提取的子字符串。

假设 second 是一个字符串 "Hello, World!",以下是一些示例:

  • "second.substring(0)" 返回 "Hello, World!",因为它从索引 0 开始提取整个字符串。
  • "second.substring(6)" 返回 "World!",因为它从索引 6 开始提取到字符串的末尾。
  • "second.substring(0, 5)" 返回 "Hello",因为它从索引 0 开始提取到索引 5-1=4 处的字符。

易碎

易碎

我看到的今夜的星空,是几万年前的光,我眼中的你是此时的你!

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注

网站ICP备案皖ICP备2024045222号-1