如何检查字符串的内容是否以另一个字符串的内容开头?

时间:2022-08-01 07:32:21

I'm trying to figure out if there is a way to compare if a content of a string is the start of another string. For example, I want to know the number of strings that start with the string "c", in an array whose content's is [cowboy, air, cow, cat]. Using the compare function from the string library works fine. The issue is when instead of trying with "c", I try with "b" I get the same number of answers. I don't know the reason why, Does anybody have a suggestion on how to fix the problem? Here are the two versions that I have.

我想弄清楚是否有一种方法来比较字符串的内容是否是另一个字符串的开头。例如,我想知道以字符串“c”开头的字符串数,在一个内容为[cowboy,air,cow,cat]的数组中。使用字符串库中的compare函数可以正常工作。问题是当我尝试用“b”而不是尝试“c”时,我会得到相同数量的答案。我不知道原因,有没有人建议如何解决这个问题?这是我的两个版本。

 #include <iostream>
 #include <string>
using namespace std;
 int main() {
// insert code here...
string A[4] = {"cowboy", "air", "c", "count"};
string b = "c";
int count = 0;

for(int i = 0; i < 4; i++)
{
    if(b.compare(A[i]) == 0 || b.compare(A[i]) == -1)
        count++;
}
cout << count << endl;
}

The output for this part is 3, which is right

这部分的输出是3,这是对的

 #include <iostream>
 #include <string>
using namespace std;
 int main() {
// insert code here...
string A[4] = {"cowboy", "air", "c", "count"};
string b = "b";
int count = 0;

for(int i = 0; i < 4; i++)
{
    if(b.compare(A[i]) == 0 || b.compare(A[i]) == -1)
        count++;
}
cout << count << endl;
}

The output for this part is also 3, which is wrong.

这部分的输出也是3,这是错误的。

Any help would be really appreciated!

任何帮助将非常感激!

4 个解决方案

#1


0  

There is an overload of the compare function which accepts position and a length, and compares only a part of the string to the other string. If you pass 0 for the position, and the size of the string you are searching for as the length, it will only compare that many characters at the start of the string, instead of comparing the whole thing.

比较函数有一个重载,它接受位置和长度,并且只将字符串的一部分与另一个字符串进行比较。如果你为位置传递0,并且你要搜索的字符串的大小为长度,它只会比较字符串开头的那么多字符,而不是比较整个事物。

if(A[i].compare(0, b.size(), b) == 0)
    count++;

By the way, the only reason your first test appeared to be working, was that you were basically checking if "c" is lexicographically less than or equal to your target strings. And since that is the case for "cowboy", "c" and "count", but not "air", your result was 3. But if you added a string like, "direwolf", which comes lexicographically after "c", but does not start with "c", you would find that your results were not what you are expecting.

顺便说一句,你的第一个测试似乎工作的唯一原因是你基本上检查“c”是否按字典顺序小于或等于你的目标字符串。因为“牛仔”,“c”和“计数”就是这种情况,而不是“空气”,你的结果就是3.但是如果你添加一个字符串,“direwolf”,它在“c”后按字典顺序排列,但不是以“c”开头,你会发现你的结果并不是你所期望的。

#2


0  

You can try using the find function:

您可以尝试使用find函数:

string A[4] = {"cowboy", "air", "c", "count"};
string b = "b";
int count = 0;

for(int i = 0; i < 4; i++)
{
    auto found = A[i].find(b);
    if(found != string::npos && found == 0)
        count++;
}
cout << count << endl;

What I do here is find b in A[i], if I get npos which means it wasn't found if I do find it, I check if its at the start by checking found == 0

我在这里做的是在A [i]中找到b,如果我得到npos,这意味着如果我找到它就找不到它,我检查它是否在开始时通过检查找到== 0

Similarly to simplify if I only want to check for just a character match at the start I could simply A[i] == b[0]

类似于简化,如果我只想在开始时检查一个字符匹配,我可以简单地A [i] == b [0]

Live Demo

#3


0  

Replace your if condition with the following. This compares the string b with A[i]'s 1st character.

用以下内容替换if条件。这将字符串b与A [i]的第一个字符进行比较。

if(b.compare(0, A[i].length(), A[i], 0,  1) == 0)

In your for loop when you pass in b = "c" the first conditions satisfies b.compare(A[i]) == 0 and for b="a" second condition satisfies. So in both cases you see 3.

在传入b =“c”时的for循环中,第一个条件满足b.compare(A [i])== 0并且对于b =“a”满足第二个条件。所以在这两种情况下你都会看到3。

#4


0  

You can do it easily by changing string b to character b and then checking if the first letter of each string in the array is equal to b or not and incrementing the count when they are equal.

您可以通过将字符串b更改为字符b,然后检查数组中每个字符串的第一个字母是否等于b,并在它们相等时递增计数来轻松完成。

#1


0  

There is an overload of the compare function which accepts position and a length, and compares only a part of the string to the other string. If you pass 0 for the position, and the size of the string you are searching for as the length, it will only compare that many characters at the start of the string, instead of comparing the whole thing.

比较函数有一个重载,它接受位置和长度,并且只将字符串的一部分与另一个字符串进行比较。如果你为位置传递0,并且你要搜索的字符串的大小为长度,它只会比较字符串开头的那么多字符,而不是比较整个事物。

if(A[i].compare(0, b.size(), b) == 0)
    count++;

By the way, the only reason your first test appeared to be working, was that you were basically checking if "c" is lexicographically less than or equal to your target strings. And since that is the case for "cowboy", "c" and "count", but not "air", your result was 3. But if you added a string like, "direwolf", which comes lexicographically after "c", but does not start with "c", you would find that your results were not what you are expecting.

顺便说一句,你的第一个测试似乎工作的唯一原因是你基本上检查“c”是否按字典顺序小于或等于你的目标字符串。因为“牛仔”,“c”和“计数”就是这种情况,而不是“空气”,你的结果就是3.但是如果你添加一个字符串,“direwolf”,它在“c”后按字典顺序排列,但不是以“c”开头,你会发现你的结果并不是你所期望的。

#2


0  

You can try using the find function:

您可以尝试使用find函数:

string A[4] = {"cowboy", "air", "c", "count"};
string b = "b";
int count = 0;

for(int i = 0; i < 4; i++)
{
    auto found = A[i].find(b);
    if(found != string::npos && found == 0)
        count++;
}
cout << count << endl;

What I do here is find b in A[i], if I get npos which means it wasn't found if I do find it, I check if its at the start by checking found == 0

我在这里做的是在A [i]中找到b,如果我得到npos,这意味着如果我找到它就找不到它,我检查它是否在开始时通过检查找到== 0

Similarly to simplify if I only want to check for just a character match at the start I could simply A[i] == b[0]

类似于简化,如果我只想在开始时检查一个字符匹配,我可以简单地A [i] == b [0]

Live Demo

#3


0  

Replace your if condition with the following. This compares the string b with A[i]'s 1st character.

用以下内容替换if条件。这将字符串b与A [i]的第一个字符进行比较。

if(b.compare(0, A[i].length(), A[i], 0,  1) == 0)

In your for loop when you pass in b = "c" the first conditions satisfies b.compare(A[i]) == 0 and for b="a" second condition satisfies. So in both cases you see 3.

在传入b =“c”时的for循环中,第一个条件满足b.compare(A [i])== 0并且对于b =“a”满足第二个条件。所以在这两种情况下你都会看到3。

#4


0  

You can do it easily by changing string b to character b and then checking if the first letter of each string in the array is equal to b or not and incrementing the count when they are equal.

您可以通过将字符串b更改为字符b,然后检查数组中每个字符串的第一个字母是否等于b,并在它们相等时递增计数来轻松完成。