C/C++字符串string操作的全面总结(二)

简介: C/C++字符串string操作的全面总结

3、适合string类型操作的函数

substr()主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度。

append() 方法在被选元素的结尾(仍然在内部)插入指定内容。提示:如需在被选元素的开头插入内容,请使用prepend()方法。

replace() 该函数返回一个字符串,其中指定的字符串已经被替换为另一字符串,并且替换的次数也可以指定。


下面是代码实例:

#include <iostream>
#include <string>
using namespace std;
//公众号:C语言与CPP编程
int main()
{
  string s("Hello world");
  string s2 = s.substr(6,5);  //从第6个开始取5个
  cout << s2 << endl ;        //s2为world
  s2 = s.substr(6);  //从第6个开始取拷贝所有的
  cout << s2 << endl ;        //s2为world
  s2 = s.substr(6);    //s2拷贝s的全部,相当于s2=s
  cout << s2 << endl ;  //s2为Hello world
  s = "C++ Primer";
  s.append(" 3rd Ed");   //再s最后添加3rd Ed
  cout << s<< endl ;  //s为C++ Primer 3rd Ed
  s = "C++ Primer";
  s.insert(s.size()," 3rd Ed"); //最后插入
  cout << s<< endl ;  //s为C++ Primer 3rd Ed
  s.replace(11,3,"4th");       //下标11开始3个替换4th
    cout << s<< endl ;  //s为C++ Primer 4th Ed
  s.replace(11,3,"Fourth");       //下标11开始3个替换Fourth
    cout << s<< endl ;  //s为C++ Primer Fourth Ed
  s = "C++ Primer 3rd Ed";    //replace相当于先删除后插入
  s.erase (11,3);            //删除3rd
  s.insert(11,"Fourth");      //插入Fourth
  cout << s<< endl ;  //s为C++ Primer Fourth Ed
  return 0;
}

4、string类型的查找

s.find( args); //在 s 中查找 args 的第一次出现

s.rfind( args) // 在 s 中查找 args 的最后一次出现

s.find_first_of( args) //在 s 中查找 args 的任意字符的第一次出现

s.find_last_of( args) //在 s 中查找 args 的任意字符的最后一次出现

s.find_first_not_of( args) //在 s 中查找第一个不属于 args 的字符

s.find_last_not_of( args) //在 s 中查找最后一个不属于 args 的字符

#include <iostream>
#include <string>
using namespace std;
// 公众号:C语言与CPP编程
int main()
{
  string name("AnnaBelle");
  string::size_type pos1 = name.find("Bell");  
  cout << pos1 << endl;     //返回下标4,如果没找到返回npos
  if(pos1 == string::npos)
     cout << "没找到!" << endl; 
  else
     cout << "找到了!下标:" << pos1 <<endl; 
  name = "2sn3";
  string numerics("0123456789");
  string::size_type pos = name.find_first_of(numerics);  //在2sn3中查找0123456789中任意一个第一次出现
  if(pos == string::npos)
     cout << "没找到!" << endl; 
  else
     cout << "找到了!下标:" << pos <<endl;       //找到了!下标:1
  //其他类型的查找这里就不举例子了
  return 0;
}
5、string对象的比较
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(void){
  string str1="hi,test,hello";
  string str2="hi,test";
  //字符串比较
  if(str1.compare(str2)>0)
    printf("str1>str2\n");
  else if(str1.compare(str2)<0)
    printf("str1<str2\n");
  else
    printf("str1==str2\n");
  //str1的子串(从索引3开始,包含4个字符)与str2进行比较
  if(str1.compare(3,4,str2)==0)
    printf("str1的指定子串等于str2\n");
  else
    printf("str1的指定子串不等于str2\n");
  //str1指定子串与str2的指定子串进行比较
  if(str1.compare(3,4,str2,3,4)==0)
    printf("str1的指定子串等于str2的指定子串\n");
  else
    printf("str1的指定子串不等于str2的指定子串\n");
  //str1指定子串与字符串的前n个字符进行比较
  if(str1.compare(0,2,"hi,hello",2)==0)
    printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");
  else
    printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");
  return 0;
}
相关文章
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
394 100
|
6月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
553 99
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
6月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
7月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
378 92
|
8月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
429 14
|
5月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
365 5
|
9月前
|
存储 编译器 C语言
关于string的‘\0‘与string,vector构造特点,反迭代器与迭代器类等的讨论
你真的了解string的'\0'么?你知道创建一个string a("abcddddddddddddddddddddddddd", 16);这样的string对象要创建多少个对象么?你知道string与vector进行扩容时进行了怎么的操作么?你知道怎么求Vector 最大 最小值 索引 位置么?
223 0
|
12月前
|
缓存 安全 Java
《从头开始学java,一天一个知识点》之:字符串处理:String类的核心API
🌱 **《字符串处理:String类的核心API》一分钟速通!** 本文快速介绍Java中String类的3个高频API:`substring`、`indexOf`和`split`,并通过代码示例展示其用法。重点提示:`substring`的结束索引不包含该位置,`split`支持正则表达式。进一步探讨了String不可变性的高效设计原理及企业级编码规范,如避免使用`new String()`、拼接时使用`StringBuilder`等。最后通过互动解密游戏帮助读者巩固知识。 (上一篇:《多维数组与常见操作》 | 下一篇预告:《输入与输出:Scanner与System类》)
321 11