C++string的基本概念及相关操作

本文详细介绍了 C++ 中 string 类的各种操作方法,包括构造、赋值、拼接、查找替换、比较、存取、插入删除及子串提取等功能。通过具体实例展示了 string 类在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string的构造函数
 *
 * string();  创建一个空的字符串,如 string str;
 * string(const char * s);   使用字符串s初始化
 * string(const string &str);  使用一个string对象初始化另一个string对象
 * string(int n,char c);     使用n的字符c初始化
 */
void test() {
    string str1;//默认构造
    const char *str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
    string s3(s2);
    cout << "s3=" << s3 << endl;
    string s4(10, 'c');
    cout << "s4=" << s4 << endl;
}

int main() {
    test();
    return 0;
}
s2=hello world
s3=hello world
s4=cccccccccc

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string的赋值操作
 *
 */
void test() {
    string str1;
    str1 = "hello world";
    cout << "str1=" << str1 << endl;
    string str2;
    str2 = str1;
    cout << "str2=" << str2 << endl;

    string str3;
    str3 = 'a';
    cout << "str3=" << str3 << endl;

    string str4;
    str4.assign("hello c++");
    cout << "str4=" << str4 << endl;

    string str5;
    str5.assign("hello c++", 5);//"hello"
    cout << "str5=" << str5 << endl;
    str5.assign(str4, 5);//" c++" ???
    cout << "str5=" << str5 << endl;

    string str6;
    str6.assign(str4);
    cout << "str6=" << str6 << endl;

    string str7;
    str7.assign(4,'c');
    cout << "str7=" << str7 << endl;

}

int main() {
    test();
    return 0;
}
str1=hello world
str2=hello world
str3=a
str4=hello c++
str5=hello
str5= c++
str6=hello c++
str7=cccc

 

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string的拼接
 *
 */
void test() {
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << str1 << endl;

    str1 += ':';
    cout << str1 << endl;

    string str2 = "LOL DNF";
    str1 += str2;
    cout << str1 << endl;

    string str3 = "I";
    str3.append(" love ");
    cout << str3 << endl;

    str3.append("game abcd", 4);
    cout << str3 << endl;

    str3.append(str2);
    cout << str3 << endl;

    string str4 = "I love ";
    str4.append(str2, 0, 3);
    cout << str4 << endl;

}

int main() {
    test();
    return 0;
}
我爱玩游戏
我爱玩游戏:
我爱玩游戏:LOL DNF
I love 
I love game
I love gameLOL DNF
I love LOL

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string查找和替换
 *
 */
//查找
void test() {
    string str1 = "abcdefg";
    int pos = str1.find("de");
    cout << pos << endl;
    pos = str1.rfind("de");
    cout << pos << endl;

    string str2 = "abcdefgde";
    pos = str2.rfind("de");
    cout << pos << endl;
}


//替换
void test2() {
    string str1 = "abcdefg";
    str1.replace(1, 3, "1111");
    cout << str1 << endl;
}

int main() {
    test();
    test2();
    return 0;
}
3
3
7
a1111efg

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string比较
 *
 */
void test() {
    string str1 = "hello world";
    string str2 = "hello world";
    if (str1.compare(str2) == 0) {
        cout << "str1等于str2" << endl;
    } else if (str1.compare(str2) > 0) {
        cout << "str1大于str2" << endl;
    } else {
        cout << "str1小于str2" << endl;
    }
}


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

 

 

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

/*
 *string字符存取
 *
 */
void test() {
    string str = "hello";
    cout << str << endl;

    //1 通过中括号访问
    for (int i = 0; i < str.size(); i++) {
        cout << str[i] << endl;
    }

    cout << "---------------" << endl;

    //2 通过at访问单个字符
    for (int j = 0; j < str.size(); j++) {
        cout << str.at(j) << endl;
    }

    //修改
    str[0] = 'x';
    cout << str << endl;
    str.at(1) = 'x';
    cout << str << endl;
}


int main() {
    test();
    return 0;
}
hello
h
e
l
l
o
---------------
h
e
l
l
o
xello
xxllo

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string插入和删除
 *
 */
void test() {
    string str = "hello world";
    str.insert(1, "xo");
    cout << str << endl;

    //删除
    str.erase(0, 2);
    cout << str << endl;
}


int main() {
    test();
    return 0;
}
hxoello world
oello world

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/*
 *string子串
 *
 */
void test() {
    string str = "hello world";
    str = str.substr(0, 3);
    cout << str << endl;
}

void test2() {
    string str = "zhangsan@sina.com";
    //从邮件中获取 用户名信息
    int pos = str.find("@");
    string username = str.substr(0, pos);
    cout << username << endl;
}

int main() {
//    test();
    test2();
    return 0;
}
hel
zhangsan

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值