c++中字符串操作函数合集

在 C++中,有许多字符串操作的函数可供使用,以下是其中一些常见的函数:

  1. std::string::size(): 返回字符串的长度(字符数)。
    1.std::string::size()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        std::cout << "The length of the string is: " << str.size() << std::endl;
        return 0;
    }
    
    //输出结果为:
    The length of the string is: 13
  2. std::string::substr(): 返回一个子字符串。
    2.std::string::substr()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        std::string sub_str = str.substr(7, 5);
        std::cout << "The substring is: " << sub_str << std::endl;
        return 0;
    }
    
    //输出结果为:
    The substring is: world
  3. std::string::append(): 在字符串末尾添加另一个字符串或字符。
    3.std::string::append()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, ";
        str.append("world!");
        std::cout << str << std::endl;
        return 0;
    }
    
    //输出结果为:
    Hello, world!
  4. std::string::insert(): 在字符串的指定位置插入另一个字符串或字符。
    4.std::string::insert()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, !";
        str.insert(7, "world");
        std::cout << str << std::endl;
        return 0;
    }
    
    //输出结果为:
    Hello, world!

  5. std::string::erase(): 从字符串中删除指定位置或范围的字符。
    5.std::string::erase()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        str.erase(5, 7);
        std::cout << str << std::endl;
        return 0;
    }
    
    //输出结果为:
    Hello!

  6. std::string::replace(): 将字符串的指定位置或范围的字符替换为另一个字符串。
    6.std::string::replace()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        str.replace(7, 5, "everyone");
        std::cout << str << std::endl;
        return 0;
    }
    
    //输出结果为:
    Hello, everyone!

  7. std::string::find(): 查找一个子字符串,并返回其在字符串中第一次出现的位置。
    7.std::string::find()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.find("world");
        if (pos != std::string::npos) {
            std::cout << "Found at position " << pos << std::endl;
        } else {
            std::cout << "Not found" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    Found at position 7

  8. std::string::rfind(): 从字符串的末尾开始查找一个子字符串,并返回其在字符串中最后一次出现的位置。
    8.std::string::rfind()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.rfind("l");
        if (pos != std::string::npos) {
            std::cout << "Found at position " << pos << std::endl;
        } else {
            std::cout << "Not found" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    Found at position 10

  9. std::string::find_first_of(): 在字符串中查找给定字符集中的任何一个字符,并返回其在字符串中第一次出现的位置。
    9.std::string::find_first_of()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.find_first_of("aeiou");
        if (pos != std::string::npos) {
            std::cout << "Found vowel at position " << pos << std::endl;
        } else {
            std::cout << "No vowel found" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    Found vowel at position 1

  10. std::string::find_last_of(): 在字符串中查找给定字符集中的任何一个字符,并返回其在字符串中最后一次出现的位置。
    10.std::string::find_last_of()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.find_last_of("aeiou");
        if (pos != std::string::npos) {
            std::cout << "Found vowel at position " << pos << std::endl;
        } else {
            std::cout << "No vowel found" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    Found vowel at position 8

  11. std::string::find_first_not_of(): 在字符串中查找不属于给定字符集中的任何一个字符,并返回其在字符串中第一次出现的位置。
    11.std::string::find_first_not_of()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.find_first_not_of("Helo, wrd!");
        if (pos != std::string::npos) {
            std::cout << "Found non-matching character at position " << pos << std::endl;
        } else {
            std::cout << "All characters match" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    Found non-matching character at position 7

  12. std::string::find_last_not_of(): 在字符串中查找不属于给定字符集中的任何一个字符,并返回其在字符串中最后一次出现的位置。
    12.std::string::find_last_not_of()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str = "Hello, world!";
        size_t pos = str.find_last_not_of("Helo, wrd!");
        if (pos != std::string::npos) {
            std::cout << "Found non-matching character at position " << pos << std::endl;
        } else {
            std::cout << "All characters match" << std::endl;
        }
        return 0;
    }
    //
    输出结果为:
    Found non-matching character at position 11

  13. std::string::compare(): 比较两个字符串,返回一个整数表示它们的关系。
    13.std::string::compare()
    
    #include <iostream>
    #include <string>
    
    int main() {
        std::string str1 = "Hello";
        std::string str2 = "Hello, world!";
        int result = str1.compare(str2);
        if (result == 0) {
            std::cout << "The strings are equal" << std::endl;
        } else if (result < 0) {
            std::cout << "The first string is less than the second" << std::endl;
        } else {
            std::cout << "The first string is greater than the second" << std::endl;
        }
        return 0;
    }
    
    //输出结果为:
    The first string is less than the second

这些函数的用法和参数可以参考 C++ 标准库的文档或教程。除此之外,C++ 还提供了许多其他字符串操作函数和算法,例如 std::transformstd::reversestd::replace_ifstd::count 等,可以根据需要选择使用。

猜你喜欢

转载自blog.csdn.net/weixin_55735677/article/details/130263190