char *和string的字符串处理常用接口函数

本文详细介绍了C++中字符串的基本操作方法,包括查找、提取、添加及删除等实用技巧,并通过示例展示了如何使用find、substr、insert和erase等函数进行高效字符串处理。

1.字符串中查找字符或字符串并返回索引,字符串中提取字符或字符串

函数find_first_of() 查找在字符串中第1个出现的字符c,而函数find_last_of()查找最后一个出现的c。匹配的位置是返回值。如果没有匹配发生,则函数返回-1。

int find_first_of(char c, int start = 0):以索引为start为开头,往字符串搜寻字符c,匹配的位置是返回值。如果没有匹配发生,则函数返回-1。

int find_last_of(char c, int start = 0):以索引为start为开头,往字符串搜寻字符c,匹配的位置是返回值。如果没有匹配发生,则函数返回-1。

string str = "Mississippi";
int index;
index = str.find_first_of('s',0);    // index为 2
index = str.find_first_of('s',4);    // index为 5
index = str.find_first_of('s',7);    // index为 -1

index = str.find_last_of('s',3);//index为 3



basic_string substr(size_type _Off = 0,size_type _Count = npos) const;

_Off: 所需的子字符串的起始位置。字符串中第一个字符的索引为 0,默认值为0.

_Count:复制的字符数目

返回值:一个子字符串,从其指定的位置开始

如果substr只有一个参数,则一直复制字符串直至字符串的末尾

string str1 = "helloworld";
string str2 = str1.substr(0, 2);
cout<<str2;// str2为 he

string str3 = str1.substr(1);
cout<<str3;// str3为 elloworld



find:

string中 find()的应用  ( rfind() 类似,只是从反向查找)
原型如下:
(1)size_t find (const string& str, size_t pos = 0) const;  //查找对象-- string类对象
(2)size_t find (const char* s, size_t pos = 0) const; //查找对象-- 字符串
(3)size_t find (char c, size_t pos = 0) const;  //查找对象-- 字符
(4)size_t find (const char* s, size_t pos, size_t n) const;  //查找对象--字符串的前n个字符(暂时不懂,待测试)
结果:找到 -- 返回 第一个字符的索引
          没找到--返回   -1


example:

string fullname = "Mark Tompkin", firstname, lastname;
int index;
  
index = fullname.find_last_of(' ');   // index is 4
// firstname = "Mark" lastname = "Tompkin"
firstname = fullname.substr(0,index);
lastname = fullname.substr(index+1);
cout<<firstname<<endl;//firstname为Mark
cout<<lastname<<endl;//lastname为Tompkin

char *s1 = "kin";
string s2 = "omp";

index = fullname.find(s1);         // 在 index = 9 匹配 "kin"
index = fullname.find(s2, 0);    //
 在 index = 6 匹配 "omp"
index = fullname.find("omp",7);    // index is -1 (
无匹配)

index = fullname.find('n',3);    // index is 11




extern char *strstr(char *str1, const char *str2);

str1: 被查找目标 string expression to search.

str2: 要查找对象 The string expression to find

返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。

char *str1 = "helloworld";
char *str2 = "owo";
char *str3 = strstr(str1, str2);
cout<<str3;// str3为 oworld




strchr函数原型:extern char *strchr(const char *s,char c);

函数功能:查找字符串s中首次出现字符c的位置。

strrchr函数原型:char *strrchr(const char *str, char c);

函数功能:查找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回这个位置的地址。如果未能找到指定字符,那么函数将返回NULL。使用这个地址返回从最后一个字符c到str末尾的字符串。

char *str1 = "helloworld";
char c = 'o';
char *str2 = strchr(str1, c);

cout<<str2;// str2为 oworld

char *str3 = strrchr(str1, c);

cout<<str3;// str3为 orld



2.添加和删除字符串
   
   
 字符连接(++=)是在字符串尾添加字符串。insert()函数扩展了这个能力,允许在任意位置添加字符串。为了从字符串。为了从字符串中删除字符串,
   
 函数erase()可以从指定的位置开始删除字符。
   
    void insert(int statr,const string& s):
              
 将子串s放入字符串中,起始于位置start。插入操作增加了原始字符串的长度。
    
     void erase(int start=0,int count=-1):
               
 从start开始,从字符串中删除count个字符。如果现有的字符串少于count个字符,或者count-1,则删除到字符串尾部的所有字符。默认情况下,start0,函数从字符串是起始位置开始删除字符串。默认情况下,函数也删除到字符串尾。需要注意的是,不使用参数调用erase()函数时,将把字符串截断为长度为0的空字符串。
    
    
 示例:
     string str = "endfile";
     string s = "string object type";
     str += " mark";
     str.inset(3,   "-of-"); // str
 是 "end-of-file mark"
     s.erase(7,7);        // s
 是 "string type"

     s.erase(3,4);       // 从index 为3处删除4个字符
     cout << s;          //
 输出:"strtype"


string str;

if(str.empty()) //如果字符串str长度为0则返回true,否则返回false


//string::npos是一个常数,用来表示不存在的位置

if(str.find(".xml") == string::npos)//如果字符串str中不存在子字符串“.xml”则返回true


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值