简体   繁体   English

如何检查const char *是否以特定字符串开头? (C ++)

[英]How do I check if a const char* begins with a specific string? (C++)

I have a const char* variable and I want to check if it begins with a certain string. 我有一个const char *变量,我想检查它是否以某个字符串开头。

For example: 例如:

string sentence = "Hello, world!";
string other = "Hello";
const char* c = sentence.c_str();

if(/*"c" begins with "other"*/)
{
    //Do something
}

How can I do this using the if statement? 我该如何使用if语句呢?

To check whether a C string begins with a certain substring, you could use strncmp() . 要检查C字符串是否以某个子字符串开头,可以使用strncmp()

For C++ strings, there is a std::string::compare() overload that accepts offsets and lengths. 对于C ++字符串,有一个std::string::compare()重载,它接受偏移量和长度。

You can use the c function strstr(string1, string2) which returns a pointer to the first occurence of string2 in string1. 您可以使用c函数strstr(string1, string2)返回指向string1在string2中首次出现的指针。 If the pointer returned is to string1 then string1 begins with what you wanted to match. 如果返回的指针指向string1,则string1以您要匹配的内容开头。

const char* str1 = "Hello World";
const char* ptr = strstr(str1, "Hello");
// -----
if(str1 == ptr)
  puts("Found");

Keep in mind that you're other variable will need to use it's .c_str() method in the context of the strstr function. 请记住,您是另一个变量,需要在strstr函数的上下文中使用它的.c_str()方法。

There are a few options that come to mind, one using legacy C calls and another two being more C++ specific. 我想到了几个选择,一个选择使用旧式C调用,另外两个选择更特定于C ++。

If you really have a const char * , it's probably best to use the legacy C one but, since your sample code only creates a const char * from a std::string , I've offered other solutions as you seem to be working exclusively with strings as the true source of the data. 如果您确实有一个const char * ,那么最好使用传统的C语言,但是,由于您的示例代码只能从std::string创建const char * ,因此我提供了其他解决方案,因为您似乎在专门工作使用字符串作为数据的真实来源。

In C++, you can use string::compare or string::find thus, though the compare is likely to be more efficient since it only checks at the start of the string rather than checking everywhere and comparing the return value to zero (the find seems more concise so, if you value that and speed is not of the utmost importance, you can use it instead): 在C ++中,您可以使用string::comparestring::find ,尽管compare可能会更高效,因为它只检查字符串的开头,而不是检查所有位置并将返回值与零进行比较( find似乎更简洁,因此,如果您重视但速度不是最重要的,则可以使用它来代替):

if (haystack.compare(0, needle.length(), needle) == 0)
if (haystack.find(needle) == 0)

Using the legacy C stuff, you can do: 使用旧版C语言,您可以执行以下操作:

if (strncmp (haystack.c_str(), needle.c_str(), needle.length()) == 0)

See the following complete program for an example: 有关示例,请参见以下完整程序:

#include <iostream>
#include <string>
#include <cstring>

int main (void) {
    std::string haystack = "xyzzy";
    std::string needle = "xy";
    std::string other = "99";

    if (haystack.compare(0, needle.length(), needle) == 0)
        std::cout << "xy found\n";
    else
        std::cout << "xy not found\n";

    if (haystack.compare(0, other.length(), other) == 0)
        std::cout << "xx found\n";
    else
        std::cout << "xx not found\n";

    return 0;
}

For the other options, simply make changes to the if statements shown above to match the samples given. 对于其他选项,只需对上面显示的if语句进行更改以匹配给定的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM