c++ - 检查一个字符串是否以另一个已知字符串开头?

标签 c++ string

<分区>

我主要用 Java 编写代码,也开始使用 C++。 我想知道如何检查 C++ 中给定的字符串是否以另一个指定的字符串开头。我在下面的 java 中发布了等效代码。

public boolean check(String string) //ENTERED string
{
    String another_string="SSS"; //to be checked if the ENTERED string starts with this string

    return (string.startsWith(another_string)); //<string>.startsWith(<string>) returns a boolean value

}

最佳答案

http://ideone.com/w1ifiJ

#include <iostream>
using namespace std;

int main() {
    string str ("abcdefghijklmnoabcde");
    string str2 ("abcde");

    size_t found = str.find(str2);

    if(found == 0)
    {
         cout << "found";
    }

    return 0;
  }

更多信息:http://www.cplusplus.com/reference/string/string/find/

关于c++ - 检查一个字符串是否以另一个已知字符串开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527404/

相关文章:

c++ - 使用notify all进行多次等待

c++ - Haxe - 生成 Exe (cpp)

C++ 类模板(类的队列)

python - 如何将位串转换为 utf-8 字符串?

r - 将单列拆分为四列并计算 R 中的重复模式

java - 分布式子串计数

c++ - 在 64 位 Windows 中 Hook 32 位进程

c++ - 共享内存进程间通信

python - 在 Python 中将 STDOUT 十六进制输出转换为字符串

sql - Postgresql 连接列并在列为 NULL 或为空字符串时跳过列