美文网首页
c++ 7、字符串

c++ 7、字符串

作者: 八戒无戒 | 来源:发表于2020-05-06 21:31 被阅读0次

1、字符串常用函数(原生)

#include <iostream>
#include <stdio.h>
#include <cstring>
using nameapace std;

extern void cstring_related();

int main()
{
    cstring_related();
    return 0;
}

void cstring_related()
{
    char a[20] = "hello ";
    char b[20] = "world";
    char c[20];
    int k;
    size_t n;
    char *p, *q;
    // strcpy(str1, str2)|strcpy_s(str1, str2)将字符串str2复制到字符串str1中
    strcpy_s(c, a);
    cout << "strcpy_s(c, a) = " << c << endl;

    // strcat_s(str1, str2)  将字符串str2拼接到str1后
    strcat_s(a, b);
    cout << "strcat(a, b)后a=" << a << endl;
    // strlen() 计算字符串的长度,不包含'\0'
    n = strlen(a);
    cout << "srelen(a)=" << n << endl;

    // strcmp(str1, str2)     字符串str1与字符串str2比较,如果str1==str2,返回0, 
    // a小于b返回-1,a大于b返回1,只要首字母前者大于或小于后者,则前者整个字符串也大于或小于后者
    k = strcmp(a, b);
    cout << "strcmp(a, b)=" << k << endl;

    // strchr(str1, char) 返回一个指针(数组),为字符char的起始位置到字符串结束
    p = strchr(a, 'l');
    cout << "strchr(a, 'l') = " << p << endl;
    q = strstr(a, "lo");

    // strstr(str1, str2) 返回一个指针(数组),为字符串str2的起始位置到字符串结束
    cout << "strstr(a, \"lo\") = " << q << endl;
}

运行结果>>>>>>>>>>>>>>>>>>>>>:
strcpy_s(c, a) = hello
strcat(a, b)后a=hello world
srelen(a)=11
strcmp(a, b)=-1
strchr(a, 'l') = llo world
strstr(a, "lo") = lo world

2、字符串常用函数(扩展)

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
using nameapace std;
extern void string_related();

int main()
{
    cstring_related();
    return 0;
}

void string_related()
{
    string str1 = "hello";
    string str2 = "world";
    string str3;
    // length()  返回字符串的长度
    cout << "str1.length()=" << str1.length() << endl;
    //>>>>>>>>>>>>>>>>>>>>>>str1.length() = 5

    // size() 返回字符串的大小
    cout << "str1.size()=" << str1.size() << endl;
    //>>>>>>>>>>>>>>>>>>>>>>str1.size() = 5

    // max_size() 返回该变量最大可容纳的字符大小
    cout << "str1.max_size()=" << str1.max_size() << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.max_size() = 9223372036854775807

    // empty() 返回字符串是否为空
    cout << "str1.empty()=" << str1.empty() << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.empty() = 0

    // = 直接赋值, 相当于strcpy
    str3 = str1;
    cout <<"str3=" << str3 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str3 = hello

    // + 追加, 相当与strcat,append
    // str1 += "love"   和 str1.append("love")是一样的
    cout << "str1 + str3 =" << str1 + str2 << endl;
    str3 = str1;
    str1 += "love";
    cout << "str1 += \"love\"; str1=" << str1 << endl;
    str3 = str3.append("love");
    cout << "str3.append(\"love\"); str3=" << str3 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1 + str3 = helloworld
    //>>>>>>>>>>>>>>>>>>>>>str1 += "love"; str1 = hellolove
    //>>>>>>>>>>>>>>>>>>>>>str3.append("love"); str3 = hellolove

    // insert 指定位置插入
    str1 = "hello";
    str1.insert(0, "mongo ");
    cout << "str1.insert(0, \"mongo\"), str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.insert(0, "mongo"), str1 = mongo hello

    // assign 将原字符串清空,然后赋予新的值作替换
    str1.assign("hello");
    cout << "str1.assign(\"hello\"), str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.assign("hello"), str1 = hello

    // < 和 > == 相当于strcmp和compare, 判断和比较字符串是否相等,相等返回0
    int i;
    str1 = "hello";
    i = str1.compare("hello");
    cout << "i = str1.compare(\"hello\"); i = " << i << endl;
    //>>>>>>>>>>>>>>>>>>>>>i = str1.compare("hello"); i = 0
    
    // swap 交换两个字符串
    str1 = "hello";
    str2 = "world";
    str1.swap(str2);
    cout << "str1.swap(str2); str1=" << str1 << endl;
    cout << "str1.swap(str2); str2=" << str2 << endl;
    //>>>>>>>>>>>>>>>>>>>>> str1.swap(str2); str1 = world
    //>>>>>>>>>>>>>>>>>>>>> str1.swap(str2); str2 = hello

    // substr(int n, int k) 返回一个指定位置n开始的k个字符串, k可以不传参,相当于切片操作
    str1 = "hello";
    str3 = str1.substr(1);
    cout << "str3.substr(1)=" << str3 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str3.substr(1) = ello

    // c_str() 字符串转为c风格的字符数组
    str1 = "hello";
    char s[20];
    strcpy_s(s, str1.c_str());
    cout << "strcpy_s(s, str1.c_str()); s=" << s << endl;
    //>>>>>>>>>>>>>>>>>>>>>strcpy_s(s, str1.c_str()); s = hello

    // replace(int n, int k, const string &s) 替代,删除从n开始的k个字符,然后从n插入字符串s
    str1 = "hello";
    str1.replace(2, 2, "ww");
    cout << "str1.replace(2, 2, \"ww\"); str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.replace(2, 2, "ww"); str1 = hewwo

    // erase(int n, int k) 删除从n开始的k个字符串
    str1 = "hello";
    str1.erase(2, 1);
    cout << "str1.erase(2, 1); str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.erase(2, 1); str1 = helo

    // []和at(),通过索引取值
    str1 = "hello";
    cout << "str1[1]=" << str1[1] << endl;
    cout << "str1.at(1)=" << str1.at(1) << endl;
    //>>>>>>>>>>>>>>>>>>>>> str1[1] = e
    //>>>>>>>>>>>>>>>>>>>>> str1.at(1) = e

    // begin()和end() 返回一个迭代器变量,分别指向字符串开始位置和最后一个字符的后面一个位置
    str1 = "hello";
    string::iterator itr;
    for (itr = str1.begin(); itr < str1.end(); itr++)
    {
        *itr = toupper(*itr);
    }
    cout << "转换后: str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>转换后 : str1 = HELLO

    // clear() 清空字符串内容
    str1 = "hello";
    str1.clear();
    cout << "str1.clear(); str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.clear(); str1 =

    // reverse(), 反转,将字符串字符位置颠倒
    str1 = "hello";
    reverse(str1.begin(), str1.end());
    cout << "reverse(str1.begin(), str1.end()), str1=" << str1 << endl;
    //>>>>>>>>>>>>>>>>>>>>>reverse(str1.begin(), str1.end()), str1 = olleh

    // find(char, n), 从头部开始在字符串中从n开始查找字符,成功返回位置 ,查找失败,返回-1
    // find(string, n), 从头部开始在字符串中从n开始查找字符串开始位置,成功返回位置 ,查找失败,返回-1
    // rfind(char, n), 从尾部开始在字符串中从n开始查找字符,成功返回位置 ,查找失败,返回-1
    str1 = "hello";
    i = str1.find('e');
    cout << "str1.find('e') = " << i << endl;
    i = str1.rfind('o');
    cout << "str1.rfind('e') = " << i << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.find('e') = 1
    //>>>>>>>>>>>>>>>>>>>>>str1.rfind('e') = 4

    // find_first_of(char|string, n), 查找是否包含有子串中任何一个字符,有返回位置,无返回-1
    // find_first_not_of ()末尾查找, 从末尾处开始,向前查找是否包含有子串中任何一个字符
    str1 = "hello";
    i = str1.find_first_of("abcdel");
    cout << "str1.find_first_of(\"abcdel\") = "  << i << endl;
    //>>>>>>>>>>>>>>>>>>>>>str1.find_first_of("abcdel") = 1
}

相关文章

网友评论

      本文标题:c++ 7、字符串

      本文链接:https://www.haomeiwen.com/subject/hemgghtx.html