c++中字符串的截取:
c++中字符串的截取:
string 类提供字符串处理函数,利用这些函数,程序员可以在字符串内查找字符,
提取连续字符序列(称为子串),以及在字符串中删除和添加。我们将介绍一些主要函数。
1.函数find_first_of()和 find_last_of() 执行简单的模式匹配
例如:在字符串中查找单个字符c。
函数find_first_of() 查找在字符串中第1个出现的字符c,而函数find_last_of()查找最后
一个出现的c。匹配的位置是返回值。如果没有匹配发生,则函数返回-1.
int find_first_of(char c, int start = 0):
查找字符串中第1个出现的c,由位置start开始。
如果有匹配,则返回匹配位置;否则,返回-1.默认情况下,start为0,函数搜索
整个字符串。
int find_last_of(char c):
查找字符串中最后一个出现的c。有匹配,则返回匹配位置;否则返回-1.
该搜索在字符末尾查找匹配,所以没有提供起始位置。
示例:
- string str = "Mississippi";
- int index;
- // 's ' 在index 为 2、3、5、6处出现
- 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
- // ‘s’的最后出现在 index= 6
- index = str.find_last_of('s');
- // while 循环输出每个'i'的index
- while((index = str.find_first_of('i', index))!= -1)
- {
- cout << "index" << index << " ";
- index++; // restart search at next indx
- }
输出结果: index 1 index 4 index 7 index 10
2.字符串中提取连续字符序列,既子串。
这个操作假定位置 start 和 字符数 count.
string substr(int start=0,int count= -1);
从起始位置开始复制字符串中的count 个字符,并返回这些字符作为子串。
如果字符串尾部小于count字符或者count 为-1,则字符串尾停止复制。
如果不使用参数调用只包括位置start,则substr()返回从位置开始到字符串尾部的子串。
find()函数在字符串中查找指定模式。该函数将字符串s和位置start作为参数,并查找
s的匹配作为子串。
int find(const string& s,int start = 0):
该搜索获得字符串s和位置start,并查找s的匹配作为子串。
如果有匹配,则返回匹配的位置;否则返回-1。默认情况下,
start为0,函数搜索整个字符串。
示例
- string fullname = "Mark Tompkin", firstname, lastname;
- int index;
- index = str.find_last_of(' '); // index is 4
- // firstname = "Mark" lastname = "Tompkin"
- firstname = fullname.sub string(0,index);
- lastname = fullname.substring(index+1);
- index = fullname.find("kin"); // 在 index = 9 匹配 "Kin"
- index = fullname.find("omp",0); // 在 index = 6 匹配 "omp"
- index = fullname.find("omp",7); // index is -1 (无匹配)
3.添加和删除字符串
字符连接(+、+=)是在字符串尾添加字符串。insert()函数扩展了这个能力,
允许在任意位置添加字符串。为了从字符串。为了从字符串中删除字符串,
函数erase()可以从指定的位置开始删除字符。
void insert(int statr,const string& s):
将子串s放入字符串中,起始于位置start。插入操作增加了原始字符串的长度。
void erase(int start=0,int count=-1):
从start开始,从字符串中删除count个字符。如果现有的字符串少于count个
字符,或者count为-1,则删除到字符串尾部的所有字符。默认情况下,start为0,函数
从字符串是起始位置开始删除字符串。默认情况下,函数也删除到字符串尾。
需要注意的是,不使用参数调用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"
- // 从index 为3处删除4个字符
- s.erase(3,4);
- cout << s; // 输出:"strtype"
4.c_str()返回c语言风格字符串的地址。
将字符串对象转换为c语言风格字符串。
char *c_str();
返回一个等价于字符串对象的c语言风格字符串的地址。返回类型char*表示c
语言风格字符串第1个字符的地址。
示例:
- string filename = "input.dat";
- // open 要求文件名是c语言风格的字符串
- fin.open(filename.c_str());
5.分离字符串路径的方法
处理文件的程序可能要分析文件名。这种算法要进行字符串处理。文件可以
由路径名指定,路径名包括由分隔符"/"分割的名称集。最后一个"/"前的名称序列
称为路径。最后一个名称是文件名,还可能包括扩展名。
路径名 /class/programs/testfile.cpp
路径 /class/programs/
文件名 testfile.cpp
扩展名 cpp
为了分析文件名,我们从键盘读入完整的路径名,并输出路径和文件名。
如果文件名具有扩展名"cpp",则在创建可执行文件名时,将用"exe"替代扩展名"cpp".
下面是程序结构的轮廓,以及如何使用字符串函数的说明:
1.输入文件名,使用函数find_last_of()在字符串中搜索最后一个出现的"/"。这个字符
确定了路径的结尾和文件名的开始。
2。路径是由最后一个"/"前所有字符串组成的子串。文件名是最后一个"/"后的
所有字符。使用最后一个"/"的位置和substr()提取出路径和文件名。
3.扩展名是文件名中最好一个"."后的字符串。调用find_last_of()搜索最后一个匹配,
则复制文件名,删除当前扩展名,并添加新的扩展名"exe"。 输出产生的可执行文件名。
// 文件prg1_3.cpp
// 此程序提示用户输入文件的路径
// 它使用string类操作来识别并输出
// 路径名和文件名。如果文件名有
// 扩展名"cpp",则创建并输出
// 可执行文件的名称,其扩展名为"exe",替换
// 扩展名"cpp"
- // WJ.cpp : 定义控制台应用程序的入口点。
- //
- #i nclude "stdafx.h"
- #i nclude<iostream>
- #i nclude<string>
- using namespace std;
- int main()
- {
- string pathname, path, filename,executableFile;
- // ‘/’和 '.'的位置
- int backslashIndex, dotIndex;
- cout << "Enter the path name: ";
- cin >> pathname;
- // 识别最后一个'/'的位置。注意:由于
- // 转义码如'/n'以/起始,
- // c++ 使用'//'表示 /
- backslashIndex = pathname.find_last_of('//');
- //路径名是最后一个'/'之前的字符
- path = pathname.substr(0,backslashIndex);
- cout << "path: " << path << endl;
- // 路径名尾部是文件名
- filename = pathname.substr(backslashIndex+1,-1);
- cout << "Filename: " << filename << endl;
- // 查看文件名是否有'.cpp'扩展名。
- // 首先找到最后一个'.'的位置。 如果
- // 没有'.',则dotIndex为-1
- dotIndex = filename.find_last_of('.');
- //测试是否有'.',其余的字符是否为"cpp"
- if (dotIndex != -1 && filename.substr(dotIndex+1) == "cpp")
- {
- // 删除"cpp",并加上"exe"设置可执行字符串
- executableFile = filename;
- executableFile.erase(dotIndex+1,3);
- executableFile+="exe";
- cout << "Executable: " << executableFile << endl;
- }
- return 0;
- }
输出结果:
第1次允许结果:
Enter the path name: /class/programs/testfile
path: /class/programs
Filename: testfile
第2次允许结果:
Enter the path name: programs/strings/filedemp.cpp
path: programs/strings
Filename: filedemo.cpp
Executable: filedemo.exe
第3次允许结果:
Enter the path name: /program.cpp
path:
Filename: program.cpp
Executable: program.exe
from:https://blog.csdn.net/zhenyusoso/article/details/7286456
c++中字符串的截取:的更多相关文章
- Python中字符串的截取,列表的截取
字符串的截取 Python中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符. 字符串的截取的语法格式如下: 变量[头下标:尾下标] 索引值以 0 为开始值,-1 ...
- python中字符串操作--截取,查找,替换
python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...
- Mysql 中字符串的截取
一.从左开始截取字符串 用法:left(str, length),即:left(被截取字符串, 截取长度) mysql> SELECT LEFT('hello,world',3); +----- ...
- js中字符串的截取
当需要从一组数据中移除到符合条件的某一个数据的时候,在这种情况下如何进行截取呢? 基本思路: ①将其通过特定的符号,将一组字符串进行拼接,或者用","或者用"+" ...
- go语言学习--string、int、int64互相转换,字符串的截取,数组和字符串的转换
下面总结了go中常用的转换 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt ...
- shell变量、函数和数组以及字符串的截取
一.变量 1.shell变量名 (1)可以由字母.数字.下划线等字符组成.但是第一个字符必须是字母或者下划线. (2)若果变量中包含下划线(_)则要特别注意,$project_svn_$date.ta ...
- c#中字符串截取使用的方法
AndyZhang welcome to java world c#中字符串截取使用的方法 String substring(int beginIndex) String substring(int ...
- java中常用的字符串的截取方法
java中常用的字符串的截取方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int l ...
- 【2017-06-27】Js中获取地址栏参数、Js中字符串截取
一.Js中获取地址栏参数 //从地址栏获取想要的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" ...
随机推荐
- systemd基本使用
参考金步国翻译的systemd中文手册: http://www.jinbuguo.com/systemd/index.html 金步国翻译质量都很高, 非常适合做参考 原文:https://wiki. ...
- Unigui unidbgrid+unidac uniquery联合使用时产生的奇葩问题
如下一个uniquery查询: SELECT a.id,a.userid,a.name,if(a.completed='T',CONCAT('<a class="icons" ...
- sqoop job从创建到执行
在学习sqoop job之前,最好先学习一下sqoop命令的导入导出 sqoop 使用 import 将 mysql 中数据导入到 hive sqoop 使用 import 将 mysql 中数据导入 ...
- 在解析XML时要注意解析元素和解析标签属性的区别
解析元素时: Sting str = ele.elementText("name"); 而解析标签属性时: String key = ele.attributeValue(&quo ...
- 仿联想商城laravel实战---3、前端页面搭建(什么情况下需要路由接参数)
仿联想商城laravel实战---3.前端页面搭建(什么情况下需要路由接参数) 一.总结 一句话总结: 比如访问课程的时候,不同的课程(比如云知梦),比如访问不同的商品,比如访问不同的分类 //商品详 ...
- Python Panda - 学习笔记
#Group by Function df.groupby('Date')[['Date']].count() df.groupby('Date')[['Date']].sum() # if it c ...
- appium界面元素介绍
一.主窗口 主页面顶部从左到右依次是: AndroidSettings:android相关的设置 GeneralSettings:全局设置,设置appium相关的内容 DeveloperSetting ...
- Python习题-列出目录下所有文件删除文件夹
需求描述: 1.当前目录下有很多文件夹.文件,统计/usr/local/这个目录下,如果是文件夹,就给删除 /usr/local/ f1 w1 f2 w2 w3 w4 ...
- MessFormat的简单使用
MessageFormat用法java.text.MessageFormat 作用:MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置. Messa ...
- HDU3037Saving Beans(组合数+lucas定理)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...