C++ char String

发布时间 2023-10-24 20:41:15作者: 贝壳里的星海

C++ char string

字符char

占据一个字节的内存空间,并可用于存储ASCII字符集中的字符,包括数字、字母、标点符号

C语言风格的字符串

单个字符是单引号  char s= '';     //单引号
字符数组是双引号  char s[]=" ";   //双引号

字符数组遍历

char str[] = "Hello, World!";

// 遍历字符串并输出每个字符
for (int i = 0; str[i] != '\0'; ++i) {
    cout << str[i] << " ";
}
cout << endl;

基本函数

C++提供了一些与char类型相关的函数,用于处理字符数据

isalpha()   判断字符是否为字母字符
isdigit()   判断字符是否为数字字符
tolower()  将字符转换为小写字母。
toupper()  将字符转换为大写字母。
#include <string>
#include <iostream>
#include <cctype>
using namespace std;

int main(int argc, char const *argv[])
{

    char ch = 'A';
    char lowercaseCh = tolower(ch);
    cout << "Lowercase character: " << lowercaseCh;
}

字符比较

#include<iostream>
#include<cstring>
using namespace std;
//若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数
int main(){
    char str1[20]="123421";
    char str2[20]="32432";
    cout << strcmp(str1,str2);

    return 0;
}

char*、char[]转换为string

参考资料

https://blog.csdn.net/liitdar/article/details/80498634

字符串 String 类

string是C++标准库的重要部分,主要用于字符串处理。使用string库需要在同文件中包括该库 #include<string>

1.声明和初始化

多种初始的方式
#include<string>
字符串是双引号  string  s="";
    
string s1;          //默认初始化
string s2(s1);       //s2是s1的副本
string s2 = s1;          //等价于s2(s1),s2是s1的副本
string s3("value");    //s3是字面值"value"的副本,除了字面值最后的那个空字符外
string s3 = "value";   //等价于s3("value"),s3是字面值"value"的副本
string s4(n,'c');     //把s4初始化为由连续n个字符'c'组成的串

string str1;               //生成空字符串
string str2("123456789");  //生成"1234456789"的复制品
string str3("12345", 0, 3);//结果为"123"
string str4("012345", 5);  //结果为"01234"
string str5(5, '1');       //结果为"11111"
string str6(str2, 2);      //结果为"3456789"


2.string的大小和容量

#include<string>

str.size()和 str.length();  //返回string对象的字符个数,他们执行效果相同。

str.max_size();            //返回string对象最多包含的字符数,超出会抛出length_error异常
str.capacity();            //重新分配内存之前,string对象能包含的最大字符数

str.front()           // 返回 第一个
str.back()             // 返回 最后一个

3.拼接append()&+操作符

用于拼接两个 字符串 string 类型的数据

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

int main(int argc, char const *argv[])
{
    // string str = "value";

    // cout << str.size() << "," << str.length() << endl;
    // cout << str.max_size() << endl;
    // cout << str.capacity() << endl;
    // cout << str.front() << endl;
    // cout << str.back() << endl;

    // 方法一:append()
    string s1("qwertyuiop");
    s1.append("1234567890");
    cout << "s1:" << s1 << endl; // s1:abcdef
    // 方法二:+ 操作符
    string s2 = "qwertyuiop";
    string s3 = "1234567890";
    s2 += s3.c_str();
    cout << "s2:" << s2 << endl; // s2:abcdef
}

//执行结果
s1:qwertyuiop1234567890
s2:qwertyuiop1234567890
用+来拼接字符串时,运算符的两边可以都是 string 字符串,也可以是一个 string 字符串和一个C风格的字符串,
还可以是一个 string 字符串和一个字符数组,或者是一个 string 字符串和一个单独的字符。
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1 = "first ";
    string s2 = "second ";
    const char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';
    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;

    cout << s5 << endl
         << s6 << endl
         << s7 << endl
         << s8 << endl;
    return 0;
}

first second 
first third
first fourth
first @

4.插入push_back()&insert()

insert() 函数可以在 string 字符串中指定的位置插入另一个字符串,它的一种原型为:
string& insert (size_t pos, const string& str);
#include <string>
#include <iostream>
using namespace std;

void test_push_back()
{

    string s1 = "12345";
    // 尾插一个字符
    s1.push_back('q');
    s1.push_back('w');
    s1.push_back('e');
    cout << "s1:" << s1 << endl; // s1:abc

    // insert(pos,char):在制定的位置pos前插入字符char
    s1.insert(s1.begin(), '-');
    // s1.insert(12, 1, '2');          // 插入index 越界 报错

    s1.insert(s1.end(), '0');           // 成功
    // s1.insert(5, '0');                // 报错

    s1.insert(5, "zxc");         // 成功
    cout << "s1:" << s1 << endl; // s1:1abc
}

int main(int argc, char const *argv[])
{

    test_push_back();
}

// 执行结果
s1:12345qwe
s1:-1234zxc5qwe0

5.string字符串遍历

下标法

迭代器方法

// 下标方法 
for( int i = 0; i < s1.size() ; i++ )
    {
        cout<<s1[i];
    }


//正向迭代法
    // 方法二:正向迭代器

string::iterator iter = s1.begin();
for( ; iter < s1.end() ; iter++)
    {
        cout<<*iter;
    }
cout<<endl;


// 反向 迭代 
for(int i=s1.size()-1;i>=0;i--){
    cout<< s1[i]<< endl;
}


string::reverse_iterator riter = s1.rbegin();
for( ; riter < s1.rend() ; riter++)
    {
        co ut<<*riter;
    }
cout<<endl;
}


6.string 删除erase()

  s1.erase(1,6);       // 删除 (起始位置,个数)

7.string 查找与替换

替换 str.replace()

 s1.replace(6,5,"girl");      // 起始位置,需要替换的长度,替换的字符串
 s1.replace(6,5,2,'.');      // 起始位置,需要替换的长度, 字符的数量,替换的字符

查找 str.find()

string s("dog bird chicken bird cat");

//字符串查找-----找到后返回首字母在字符串中的下标

// 1. 查找一个字符串
cout << s.find("chicken") << endl;        // 结果是:9

// 2. 从下标为6开始找字符'i',返回找到的第一个i的下标
cout << s.find('i',6) << endl;            // 结果是:11

// 3. 从字符串的末尾开始查找字符串,返回的还是首字母在字符串中的下标
cout << s.rfind("chicken") << endl;       // 结果是:9

// 只要含有任意包含中的字符都算, bird中含有i,则输出
cout<< s.find_first_of("chick")<< endl;   //输出结果是5
cout << s.find_first_not_of("dog bird 2006") << endl;  // 结果是:9

7.排序字符串

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

void test9()
{
    string s = "cdefba";
    sort(s.begin(),s.end());
    cout<<"s:"<<s<<endl;     // 结果:abcdef
    
}

8.分割字符串

strtok():分割字符串

string str="12345678"
string s1= str.substr(2,4);  //从第二个开始,个数是4个
s1="3456"

string s2=str.substr(3)     //从第三个开始以后 
s2="45678"

9.交换连个string字符串对象

stirng str1("west"),str2("east");
str1.swap(str2)
// 输出 str1=="east", str2=="west";

参考资料

https://blog.csdn.net/qq_37941471/article/details/82107077