字符串比较(检查字符串是否以另一个字符串开始)

时间:2022-08-13 22:51:12

I need to check whether an std:string begins with "xyz". How do I do it without searching through the whole string or creating temporary strings with substr().

我需要检查std:string是否以“xyz”开头。如何在不搜索整个字符串或使用substr()创建临时字符串的情况下进行操作呢?

4 个解决方案

#1


147  

I would use compare method:

我会用比较的方法:

std::string s("xyzblahblah");
std::string t("xyz")

if (s.compare(0, t.length(), t) == 0)
{
// ok
}

#2


12  

An approach that might be more in keeping with the spirit of the Standard Library would be to define your own begins_with algorithm.

一种可能更符合标准库精神的方法是定义自己的begins_with算法。

#include <algorithm>
using namespace std;


template<class TContainer>
bool begins_with(const TContainer& input, const TContainer& match)
{
    return input.size() >= match.size()
        && equal(match.begin(), match.end(), input.begin());
}

This provides a simpler interface to client code and is compatible with most Standard Library containers.

这为客户端代码提供了一个更简单的接口,并且与大多数标准库容器兼容。

#3


9  

Look to the Boost's String Algo library, that has a number of useful functions, such as starts_with, istart_with (case insensitive), etc. If you want to use only part of boost libraries in your project, then you can use bcp utility to copy only needed files

查看Boost的字符串Algo库,它有许多有用的函数,比如starts_with、istart_with(大小写不敏感)等等

#4


1  

I feel I'm not fully understanding your question. It looks as though it should be trivial:

我觉得我没有完全理解你的问题。看起来它应该是琐碎的:

s[0]=='x' && s[1]=='y' && s[2]=='z'

This only looks at (at most) the first three characters. The generalisation for a string which is unknown at compile time would require you to replace the above with a loop:

这只查看(至多)前三个字符。对于在编译时未知的字符串的概括,需要您用一个循环替换上面的内容:

// look for t at the start of s
for (int i=0; i<s.length(); i++)
{
  if (s[i]!=t[i])
    return false;
}

#1


147  

I would use compare method:

我会用比较的方法:

std::string s("xyzblahblah");
std::string t("xyz")

if (s.compare(0, t.length(), t) == 0)
{
// ok
}

#2


12  

An approach that might be more in keeping with the spirit of the Standard Library would be to define your own begins_with algorithm.

一种可能更符合标准库精神的方法是定义自己的begins_with算法。

#include <algorithm>
using namespace std;


template<class TContainer>
bool begins_with(const TContainer& input, const TContainer& match)
{
    return input.size() >= match.size()
        && equal(match.begin(), match.end(), input.begin());
}

This provides a simpler interface to client code and is compatible with most Standard Library containers.

这为客户端代码提供了一个更简单的接口,并且与大多数标准库容器兼容。

#3


9  

Look to the Boost's String Algo library, that has a number of useful functions, such as starts_with, istart_with (case insensitive), etc. If you want to use only part of boost libraries in your project, then you can use bcp utility to copy only needed files

查看Boost的字符串Algo库,它有许多有用的函数,比如starts_with、istart_with(大小写不敏感)等等

#4


1  

I feel I'm not fully understanding your question. It looks as though it should be trivial:

我觉得我没有完全理解你的问题。看起来它应该是琐碎的:

s[0]=='x' && s[1]=='y' && s[2]=='z'

This only looks at (at most) the first three characters. The generalisation for a string which is unknown at compile time would require you to replace the above with a loop:

这只查看(至多)前三个字符。对于在编译时未知的字符串的概括,需要您用一个循环替换上面的内容:

// look for t at the start of s
for (int i=0; i<s.length(); i++)
{
  if (s[i]!=t[i])
    return false;
}