std::string 比较(检查字符串是否以另一个字符串开头)

发布于 2024-07-22 07:28:11 字数 78 浏览 4 评论 0原文

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

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().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

与君绝 2024-07-29 07:28:11

我会使用比较方法:

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

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

I would use compare method:

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

if (s.compare(0, t.length(), t) == 0)
{
// ok
}
太阳公公是暖光 2024-07-29 07:28:11

一种可能更符合标准库精神的方法是定义您自己的 begin_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());
}

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

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

#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.

凝望流年 2024-07-29 07:28:11

查看 Boost 的 String Algo 库,它有一个许多有用的函数,例如starts_with、istart_with(不区分大小写)等。如果您只想在项目中使用部分boost库,那么您可以使用bcp实用程序仅复制所需的文件

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

相权↑美人 2024-07-29 07:28:11

看来 std::string::starts_with 在 C++20 内部,同时 std::string::find 可以使用

std::string s1("xyzblahblah");
std::string s2("xyz")

if (s1.find(s2) == 0)
{
   // ok, s1 starts with s2
}

It seems that std::string::starts_with is inside C++20, meanwhile std::string::find can be used

std::string s1("xyzblahblah");
std::string s2("xyz")

if (s1.find(s2) == 0)
{
   // ok, s1 starts with s2
}
山田美奈子 2024-07-29 07:28:11

我觉得我没有完全理解你的问题。 看起来它应该是微不足道的:

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

这只查看(最多)前三个字符。 编译时未知的字符串的泛化需要您用循环替换上面的内容:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文