C 在基因中找到Whitespaces

C++ find whitespaces in substring

本文关键字:Whitespaces      更新时间:2023-10-16

我想知道如何在C 中的子字符串中找到空的或whitespaces。例如:

string str = "( )"; // or str = "()"

在这里,我想确保括号之间总是有一些东西。函数ISSPACE()只有一个字符,因此我必须循环搜索。有什么更好的方法吗?感谢您的帮助。

您可以使用 std::string::find()查找()字符,然后使用std::string::find_first_not_of()检查这些索引之间的任何非Whitespace字符。

string str = "( )"; // or str = "()"
string::size_type idx1 = str.find("(");
if (idx1 != string::npos) {
    ++idx1;
    string::size_type idx2 = str.find(")", idx1);
    if (idx2 != string::npos) {
        string tmp = str.substr(idx, idx2-idx1);
        string::size_type idx3 = tmp.find_first_not_of(" trn");
        if (idx3 != string::npos) {
            ...
        }
    }
}