C++ string

C++ string append方法的常用用法

append函数是向string的后面追加字符或字符串。
1).向string的后面加C-string
string s = “hello “; const char *c = “out here “;
s.append(c); // 把c类型字符串s连接到当前字符串结尾
s = “hello out here”;
2).向string的后面加C-string的一部分
string s=”hello “;const char *c = “out here “;
s.append(c,3); // 把c类型字符串s的前n个字符连接到当前字符串结尾
s = “hello out”;
3).向string的后面加string
string s1 = “hello “; string s2 = “wide “; string s3 = “world “;
s1.append(s2); s1 += s3; //把字符串s连接到当前字符串的结尾
s1 = “hello wide “; s1 = “hello wide world “;
4).向string的后面加string的一部分
string s1 = “hello “, s2 = “wide world “;
s1.append(s2, 5, 5); 把字符串s2中从5开始的5个字符连接到当前字符串的结尾
s1 = “hello world”;
string str1 = “hello “, str2 = “wide world “;
str1.append(str2.begin()+5, str2.end()); //把s2的迭代器begin()+5和end()之间的部分连接到当前字符串的结尾
str1 = “hello world”;
5).向string后面加多个字符
string s1 = “hello “;
s1.append(4,’!’); //在当前字符串结尾添加4个字符!
s1 = “hello !!!!”;

C++ string append()添加文本

使用append()添加文本常用方法:

直接添加另一个完整的字符串:

如str1.append(str2);

添加另一个字符串的某一段子串:

如str1.append(str2, 11, 7);

添加几个相同的字符:

如str1.append(5, '.');

注意,个数在前字符在后.上面的代码意思为在str1后面添加5个".".

例子:

     //========================================  
      
    #include<iostream>  
      
    using namespace std;  
      
    //========================================  
      
    int main()  
      
    {  
      
        string str1="I like C++";  
      
        string str2=",I like the world.";  
      
        string str3="Hello";  
      
        string str4("Hi");  
      
        //====================================  
      
        str1.append(str2);  
      
        str3.append(str2, 11, 7);  
      
        str4.append(5, '.');  
      
        //====================================  
      
        cout<<str1<<endl;  
      
        cout<<str3<<endl;  
      
        cout<<str4<<endl;  
      
        system("pause");  
      
        return 0;     
      
    }  
      
    //========================================  

运行结果为

I like C++,I like the world.

Hello World.

Hi.....

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用:在C++中,可以使用string类来表示字符串。可以通过索引或at()函数来访问string字符串的元素。例如,字符串str中的第三个字符可以使用str或str.at(2)来获取。 引用:如果需要将数值转换为字符串,在C++中可以使用stringstream类来实现。首先创建一个stringstream对象,然后使用<<操作符将数值插入对象中。最后可以使用>>操作符或str()函数将stringstream对象转换为字符串。下面是一个示例代码: ```c++ #include <string> #include <sstream> #include <iostream> using namespace std; int main() { double a = 123.32; string res; stringstream ss; ss << a; ss >> res; // 或者 res = ss.str(); cout << res; return 0; } ``` 这段代码将数值a转换为字符串,并将其输出。 对于string类型的字符串,可以使用比较操作符来比较字符串的大小。与C语言中的字符串比较函数不同,C++中的string类可以直接使用>、<、==、>=等数学符号进行比较。例如,可以使用==操作符来判断两个字符串是否相等,或者使用>操作符来比较两个字符串的大小。下面是一个示例代码: ```c++ #include <string> #include <iostream> using namespace std; int main() { string a; a = "hello"; if (a == "world") cout << "yes" << endl; else if (a > "world") { cout << "You win." << endl; } else cout << "You failed." << endl; a = " world"; cout << a << endl; } ``` 这段代码首先给字符串a赋值为"hello",然后使用==操作符判断a是否等于"world",如果不等于则继续使用>操作符判断a是否大于"world",如果大于则输出"You win.",否则输出"You failed."。最后将字符串a赋值为" world"并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小火球2.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值