zoukankan      html  css  js  c++  java
  • #include <string>

    1 append(string T&);字符串拼接

    2 c_str

    string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址。

    3 empty();判断是否为空

    4 erase

    删除

    5 find

    在字符串中查找Find content in string

    6 find_first_not_of

    Find absence of character in string

    7 find_first_of

    Find character in string

    8 find_last_not_of

    Find non-matching character in string from the end

    9 find_last_of

    Find character in string from the end

    10 rfind

    Find last occurrence of content in string

    11 insert

    插入

    12 replace

    替换

    打印字符串,并执行

    system(string1[i].c_str());

    //std::array<std::string, 数组元素个数>

     1 #include <iostream>
     2 #include <array>
     3 #include <string>
     4 using namespace std;
     5 
     6 void main()
     7 {
     8     std::array<std::string, 5> string1 = { "calc","notpad","tasklist","mspaint","write" };//std::array<std::string, 数组元素个数>
     9 
    10     for (int i = 0; i < 5; i++)
    11     {
    12         std::cout << string1[i] << std::endl;//打印
    13         system(string1[i].c_str());//执行
    14     }
    15 
    16     system("pause");
    17 };

    字符串相加,类似C的strcat函数

     1 #include <iostream>
     2 #include <array>
     3 #include <string>
     4 using namespace std;
     5 
     6 void main()
     7 {
     8     std::string str1 = "task";
     9     std::string str2 = "list";
    10     std::string str3 = str1 + str2;
    11 
    12     system(str3.c_str());
    13 
    14     system("pause");
    15 }

    C: char str[]

    C++: string

    注意区分两者不同,string是类

    error C3863: 不可指定数组类型“char [100]”

     1 #include <iostream>
     2 
     3 void main()
     4 {
     5     char str[100];
     6     str = "hello";//error C3863: 不可指定数组类型“char [100]”
     7 
     8     std::string str1;
     9     str1 = "world";
    10 }

    append可以实现字符串拼接

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str1("hello");
     7     std::string str2("world");;
     8 
     9     str1.append(str2);//字符串拼接
    10 
    11     std::cout << str1 << std::endl;
    12 }

    str1.insert(str1.begin(), 'X');//头部插入

    str1.insert(str1.end(), 'X');//尾部插入

    str1.insert(str1.begin(), 3, 'X');//头部插入3个X

    str1.insert(str1.begin() + 3, 3, 'X');//头部插入3个X,在开头第3个位置插入

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str1("hello");
     7     std::string str2("world");;
     8 
     9     str1.insert(str1.begin(), 'X');//头部插入
    10     str1.insert(str1.end(), 'X');//尾部插入
    11 
    12     str1.insert(str1.begin(), 3, 'X');//头部插入3个X
    13 
    14     str1.insert(str1.begin() + 3, 3, 'X');//头部插入3个X,在开头第3个位置插入
    15 
    16     std::cout << str1 << std::endl;
    17 }

    str.erase(str.begin());//删除第一个字符
    str.erase(3, 4);//从下标3开始,删除4个字符(下标从0开始)

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str("helloworld");
     7 
     8     str.erase(str.begin());//删除第一个字符
     9     str.erase(3, 4);//从下标3开始,删除4个字符(下标从0开始)
    10 
    11     std::cout << str << std::endl;
    12 }

    str.replace(0, 3, "china");//从下标0开始,把3个字符替换成china
    //位置,长度,字符串

    str.replace(0, 3, "china");//从下标0开始,直接插入china

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str("helloworld");
     7 
     8     str.replace(0, 3, "china");//从下标0开始,把3个字符替换成china
     9     //位置,长度,字符串
    10 
    11     str.replace(0, 3, "china");//从下标0开始,直接插入china
    12 
    13     std::cout << str << std::endl;
    14 }

    各种find

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str("helloworld");
     7 
     8     std::cout << str.find("wo") << std::endl;//正向查找
     9 
    10     std::cout << str.rfind("wo") << std::endl;//反向查找
    11 
    12     std::cout << str.find_first_of("wo") << std::endl;//最后一个找到与字符串匹配的字符位置
    13     
    14     std::cout << str.find_first_not_of("wo") << std::endl;
    15 
    16     std::cout << str.find_last_of("wo") << std::endl;
    17 
    18     std::cout << str.find_last_not_of("wo") << std::endl;
    19 
    20     std::cout << str << std::endl;
    21 }

    string判断是否一样

     1 #include <iostream>
     2 #include <string>
     3 
     4 void main()
     5 {
     6     std::string str1("helloworld");
     7     std::string str2("helloworld");
     8 
     9     char str3[100] = "helloworld";
    10     char str4[100] = "helloworld";
    11     
    12     std::cout << (str1 == str2) << std::endl;//string重载==
    13     std::cout << (str3 == str4) << std::endl;//判断地址是否一样,地址肯定不一样,永远是0
    14 
    15     std::cout << str1.empty() << std::endl;//判断是否为空
    16 }

    121456

  • 相关阅读:
    说说与线程相关的方法
    sleep()和yield()有什么区别?
    同步和异步有何异同,分别在什么情况下使用?
    如何保证多个线程同时启动?
    volatile关键字能否保证线程安全?
    使用对象的wait()方法需要注意什么?
    乐观锁与悲观锁是什么?
    Condition实现等待、唤醒
    LongAdder与AtomicLong有什么区别?
    介绍一下ForkJoinPool的使用
  • 原文地址:https://www.cnblogs.com/denggelin/p/5655189.html
Copyright © 2011-2022 走看看