#include <iostream>
#include <cstring>
#include <string> using namespace std; int main()
{
//直接赋值
string test;//定义了一个空字符串str
test = "test_the_string"; cout << "——————string字符串截取————————" << endl; //利用已有string初始化
string s1(test); //复制字符串,s1=test,深拷贝
cout << "s1 - " << s1 << endl; string s2(test, );//s2=test[6:]
cout << "s2 - " << s2 << endl; string s3(test, , );//s3=test[6:(6+3)]
cout << "s3 - " << s3 << endl; string s3_(test.begin()+,test.begin()+); //s3=test[6:(6+3)]
cout << "s3_ - " << s3_ << endl; cout << "——————字符串数组截取————————" << endl; // C风格字符串转string初始化
char testC[] = "test_the_string"; //定义了一个C字符串 string s4(testC); //s3=test
cout << "s4 - " << s4 << endl; string s5(testC,); //s3=test[:7]
cout << "s5 - " << s5 << endl; string s6(,'A'); //生成一个字符串,包含5个'A'字符
cout << "s6 - " << s6 << endl; cout << "——————字符串比较操作符————————" << endl;
/* ==、>、<、>=、<=、和!=比较字符串(自左向右逐个按ASCII值大小相比较)
* 可以用+或者+=操作符连接两个字符串,
* 并可以用[]获取特定的字符 */ bool temp = (s1 > "temper");//s>m
cout << "s1 > s2 --- " << temp << endl; cout << "——————字符串比较操作符————————" << endl;
/* string的一些特性
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const; //返回string对象中可存放的最大字符串的长度
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c); //把字符串当前大小置为len,多去少补,多出的字符c填充不足的部分*/ cout << "s1是否为空:" << s1.empty() << endl;
cout << "s1的长度为:" << s1.length() << endl;
cout << "s1的当前容量(不新分配内存):" << s1.capacity() << endl;
cout << "s1的最大容量(重新分配内存):" << s1.max_size() << endl;
cout << "s1的长度:" << s1.size() << endl;
s1.resize(, '-');
cout << "s1修改长度后,变成了:" << s1 << endl; cout << "——————字符串的查找————————" << endl;
/* 查询结果类型为string::size_type,没有查询到,则返回string::npos;
* 主要函数:find, rfind, find_first_of,find_last_of,s.find_first_not_of */ cout << "->方法1,使用string::npos判断\n";
string::size_type loc;
string s = "study hard and make progress everyday! every day!!";
loc = s.find("make", ); if(loc != string::npos){ //搜不到为string::npos
cout << "在" << loc << "找到" << endl;
}
else{
cout << "没有找到元素" << endl;
}; cout << "->方法2,string::npos可强制转换为-1\n";
int local;
local = static_cast<int>(s.find("make", )); if(local != -){ //显式转换为int,搜不到为-1
cout << "在- " << local << " -找到元素" << endl;
}
else{
cout << "没有找到元素" << endl;
}; cout << "——————字符串的常用函数————————" << endl; cout << s1.erase(,) << endl << endl; //【删除】第2个位置的后2个字符,返回删除后的结果
cout << s1.insert(, "st") << endl; //在第2个位置【插入】字符串“st”
cout << s1.append("尾部新添加的") << endl; //【append】末尾添加
cout << s1.replace(, , "修改值") << endl; //索引5开始的3个字符【替换】成"修改值"
cout << s1.substr(, ) << endl; //【截取】字符串,s1[1:4] return ;
}

c++字符串string的操作的更多相关文章

  1. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  2. redis:string字符串类型的操作

    1. string字符串类型的操作: 1.1. set 设置单个值 语法:set key value [EX seconds] [PX milliseconds] [NX|XX] 注: EX seco ...

  3. Python操作redis字符串(String)详解 (三)

    # -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...

  4. [Swift]字符串(String类、NSString类)常用操作

    NS是Cocoa类对象类型的前缀,来源于乔布斯建立的另一家公司--NeXTNSString的使用方法,和Swift语言中的String有很多相似之处. 1.字符串的定义String类 var str1 ...

  5. openresty开发系列18--lua的字符串string操作

    openresty开发系列18--lua的字符串string操作 string的相关操作 1)string.upper(s)接收一个字符串 s,返回一个把所有小写字母变成大写字母的字符串.print( ...

  6. redist命令操作(一)--键key,字符串String

    1.Redis 字符串(String) 参考菜鸟教程:http://www.runoob.com/redis/redis-strings.html 设置指定key的值,如果原来有,覆盖 127.0.0 ...

  7. StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/ 目录 一.基础配置封装 二.String字符串类型数据操作封 ...

  8. redis 的使用 (基础, key操作, string类型操作)

    使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...

  9. Java基础——数组应用之字符串String类

    字符串String的使用 Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的. Java中没有内置的字符串类型,而是在标准Java类库中提供了一 ...

随机推荐

  1. Confluence 6 后台中为站点添加应用导航

    Confluence 6 后台中为站点添加应用导航的连界面和方法. https://www.cwiki.us/display/CONFLUENCEWIKI/Configuring+the+Site+H ...

  2. Swift可选项

  3. RefineDet算法笔记

    ---恢复内容开始--- 一.创新点 针对two-stage的速度慢以及one-stage精度不足提出的方法,refinedet 包括三个核心部分:使用TCB来转换ARM的特征,送入ODM中进行检测: ...

  4. bat命令行实现全盘遍历搜索文件

    背景:当想要查找一个文件时,记得放在某个盘里.手动去遍历时感觉好心累,找了半天还是没有找着(虽然win有自带的搜索框,但是看着进度条的速度,我便果断的点了取消).基于这个情况,所以写了脚本满足自身查找 ...

  5. vue的多选框

  6. day 08字符编码 文件处理

    字符编码1.软件启动流程(打开notepad++文档)从硬盘将软件加载到内存上加载test.txt到内存中执行notepad++的代码,将test.txt打到屏幕上 python解释器也是一个应用软件 ...

  7. ActiveMQ消息的持久化策略

    持久化消息和非持久化消息的存储原理: 正常情况下,非持久化消息是存储在内存中的,持久化消息是存储在文件中的.能够存储的最大消息数据在${ActiveMQ_HOME}/conf/activemq.xml ...

  8. 【python】给正则匹配部分命名

    可以用?P<name>的方法给正则匹配的部分命名. 例:要将<字母,数字>的部分命名为test x = "abc <haha,123> test @@&q ...

  9. C++ friend友元函数和友元类(转)

    一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有本类中的函数可以访问本类的 private 成员.现在,我们来介绍一种例外情 ...

  10. css 清除浮动的几种方式

    1.给浮动的元素的父级添加 overflow:hidden;属性 ul>不浮动 添加overflow:hidden; li>浮动 2.给浮动的元素的父级添加after伪类 ul:after ...