在某些字符串之前提取字符串

时间:2018-03-12 22:12:22

标签: c++ c arrays string substring

我希望在一系列特定字符或子字符串之前获取字符串。 例如,我有

unsigned char KeyStr[BUFFER_SIZE] = "-#NOT#"

unsigned char SomeStr[BUFFER_SIZE] = "this is the string i want to extract-#NOT#"

从char数组上面,我想获得this is the string i want to extract。 我想在-#NOT#KeyStr之前获取任何字符串。

我知道我可以使用strstr检查KeyStr中是否存在SomeStr,但我如何在KeyStr之前提取字符串。 据我所知,我不能使用strtok原因,它会检查分隔符而不是子字符串。

我是C的新手,所以绝对无能为力。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

C ++有一些简洁的功能,但是当你似乎在使用C字符串和C方法时,我会给出一个C答案。在大多数情况下,C中的子字符串搜索必须手动实现。但是,对于这个简单的例子,我会给你一个天真的方法。你可以做的一件事是遍历你的字符串并寻找匹配的字符。如果找到匹配的字符,则可以查看下一个字符,看它们是否与keyString中的下一个字符相对应。有点像:

int simpleSubStrSearch(char * st1, char * needle) {
 int i;
 int i2;
 int j;
 int subStrIndex = 0;
 int haystackSize = strlen(st1);
 int needleSize = strlen(needle);
 int succeed = 1;
 for (i = 0; i < haystackSize; i++) {
     if (*(st1 + i) == *needle) /*Tests if the ith character equals the first character in the string we are looking for*/ {
         subStrIndex = i;
         j = 0;
         for (i2 = i; i2 < needleSize; i2++) { /*If so, then we continue looping unless the characters no longer match up*/
             if (*(st1 + i2) == '\0') return -1; /*Return if its the end of the string*/
             else if (*(st1 + i2) != *(needle + j)) { /*If characters no longer match in the haystack and needle string*/
                 succeed = 0;
                 break;
             }
             j++;
         }
         if (succeed) return subStrIndex;
     }
     else if (*(st1 + i) == '\0') return -1;
  }
}

从那里你可以获取这个函数返回的索引,只要它不是-1(找不到针),你就可以使用memcpy复制你想要的字符串部分。

int index = simpleSubStrSearch(someStr, keyStr);
if (index > 0) {
    char * noKey = (char*)malloc(index + 1);
    memcpy(noKey, someStr, index);
    *(noKey + index) = '\0'; /*Remember the null terminator*/
    printf("Succeeded: %s \n", noKey);
    free(noKey);
} 

显然,这不是一种有效的方法,所以我会留给你探索更好的子串算法。如果您实际上正在使用C ++,那么最简单的方法是使用std::string及其substr()indexOf()方法从std :: string的开头到子串keyString的索引。

答案 1 :(得分:1)

1 :(我假设您有充分的理由使用Dear xxxx., The following build has completed processing: Platform: iOS App Name: xxx Build Number: 2.5.6 Version Number: 2.5 App SKU: xxxx App Apple ID: xxxx You can now use this build for TestFlight testing or submit it to the App Store. If you have any questions regarding your app, click Contact Us in iTunes Connect. Regards, The App Store team 代替unsigned char,以及为什么使用旧式C在C ++中起作用,而不是使用现代C ++方法。)

就像您在问题中所说,您可以使用char来查找令牌。找到令牌后,您可以使用简单的指针算法来知道要复制的字符数,使用strstr()来制作实际副本,例如:

strncpy()

现在,请说明,请考虑使用实际的C ++技术而不是C:

unsigned char KeyStr[BUFFER_SIZE] = "-#NOT#";
unsigned char SomeStr[BUFFER_SIZE] = "this is the string i want to extract-#NOT#";

unsigned char *found = (unsigned char*) strstr((char*)SomeStr, (char*)KeyStr);
if (!found) ...

unsigned char extracted[BUFFER_SIZE];
strncpy((char*)extracted, (char*)SomeStr, found - SomeStr);

答案 2 :(得分:0)

这可能不是最好的方法,但这就是我实施解决方案的方式。

unsigned char* buffer = (unsigned char*)calloc(BUFFER_SIZE, sizeof(char));
strcpy((char*)buffer, "this is the worst way possible to do this-yaL8r");

unsigned char ExtractedStr[BUFFER_SIZE] = "";
printf(">> %s\n", buffer);

// "-yaL8r" is the Key string
for (unsigned int i = 0; i < strlen((char*)buffer); i++)
{
    if (buffer[i] == '-' && buffer[i + 1] == 'y' && buffer[i + 2] == 'a' && buffer[i + 3] == 'L' && buffer[i + 4] == '8'
        && buffer[i + 5] == 'r')
    {
        break;
    }
    else
    {
        ExtractedStr[i] = buffer[i];
    }
}

printf(">> %s\n", ExtractedStr);