Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827
string的操作
s.empty() //Returns true if s is empty,otherwise returns false
s.size() //Returns numbers of characters of s
s[n] //Returns the character at position n in s,positions start at 0
- s1 + s2 //Returns a string equals to the concatenation of s1 and s2
- s1 = s2 //Replaces characters in s1 by a copy of s2
- v1 == v2 //Returns true if v1 and v2 are equal,false otherwise
- !=,<,<=,>,>= //Have their normal meanings
以下为示例代码:
#include<iostream>
#include<string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string st("The expense of spirit\n");
cout<<"The size of "<<st<<"is "<<st.size()
<<" characters, including the newline"<<endl;
if(st.size() == 0)
cout<<"st is empty"<<endl;
/*
*equals to:
*if(st.empty()){...}
*/
string st1 = st;
cout<<"st1 is "<<st1;
return 0;
}
运行结果为:
The size of The expense of spirit is
22 characters, including the newline
st1 is The expense of spirit
如果再插入如下代码:
string s1 = "Hello";
string s2 = " World!\n";
string s3 = s1 + s2;
cout<<s3<<endl;
运行结果为
Hello World!
字符串字面常量与string相加
请看下列代码:
string s1 = "hello";
string s2 = "world";
string s3 = s1 + ","; //ok,adding a string and a literal
strign s4 = "hello" + ",";//error,no string operand
string s5 = s1 + "," + " world";//ok,each + has a string operand
string s6 = "hello" + "," + s2;//error,can't add strign literals
s1和s2都是直接初始化为string的,但是对于s3就不同了,首先“,”是一个字符串字面常量,它与s1相加,由于s1是string,它被隐式转换为string后再与s1相加;
对于s4,两个都是字符串字面常量,它们相加是非法的,必须要用strcat(s1,s2)将它们“连接”,而不是“相加”;
对于s5,其实它和s3是一样的:s1和“,”相加,它先将“,”转换为string再与s1相加,返回的是string,然后再与”world”相加…
对于s6,一开始是”hello”和”,”相加,和s4的错误相同。
string:: size_type
逻辑上来说,string的size()函数的返回值应为int型,更精确的说,是unsigned型。但是在实际应用中,我们从某一个文件读到的字符数量很容易就超过了unsigned型的范围,string为size()函数里提供了一种更为安全的返回类型string:: size_type。
从string中取得字符
示例如下:
string str(“some string”);
for(string::size_type ix = 0; ix != str.size(); ++ix)
cout<<str[ix]<<endl;
也可以改变string的值:
//将str的字符全置为*
for(string::size_type ix = 0; ix != str.size(); ++ix)
str[ix] = 'x';
作者注:从改变string的值的方式(是str[x] = ‘x’而不是str[x] = “x”)我们可以看出,我们所得到的每一个str的字符都是char型,而不是string。这是值得注意的。
处理string字符串的函数
处理string字符串的函数包含在头文件cctype中,编程时应将该头文件包含:
#include<cstring>
该头文件包含的一部分函数清单如下:
// 本文所有代码均出自《C++ primer》
// 上次敲英文敲得太累了,这次直接翻译成中文了(其实真正的原因是英文版没人看,赚不到访问量: ( ),结果还是花了将近两个小时,天哪我还没复习啊。。。
Library string type(2)——关于String的操作的更多相关文章
- Library string Type
The string type supports variable-length character strings.The library takes cares of managing memor ...
- [Cpp primer] Library string Type
In order to use string type, we need to include the following code #include<string> using std: ...
- 【java】String类和StringBuffer类常用操作
String类是字符串常量,是不可更改的常量.而StringBuffer是字符串变量,它的对象是可以扩充和修改的.StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于Stri ...
- spring3+struts2+hibernate3整合出现的问题,No mapping found for dependency [type=java.lang.String, name='struts.objectFactory.spring.enableAopSupport']
七月 11, 2016 3:49:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...
- A const field of a reference type other than string can only be initialized with null Error [duplicate]
I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...
- 出错:Failed to convert property value of type 'org.apache.ibatis.session.defaults.DefaultSqlSessionFactory' to required type 'java.lang.String' for property 'sqlSessionFactoryBeanName';
出错的详细信息: 3 ERROR [http-nio-80-exec-3] org.springframework.web.servlet.DispatcherServlet - Context in ...
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
问题一:The type java.lang.String cannot be resolved. It is indirectly referenced from required .class f ...
- Python把json格式的string对象转变成dict对象操作、Python3不能使用urllib2、urllib.parse.urlencode(params).encode(encoding='UTF8')
son格式的string对象转变成dict对象操作 content=eval(content)#json字典转化 Python3不能使用urllib2 直接使用urllib.request替换urll ...
- 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'
springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...
随机推荐
- BCP的用法
bcp kaiser..kp_rates in c:\kp.bcp -U buykporg -P buykporg -S localhost -c bcp ccrm_oem_shqc.."s ...
- poj 1438--One-way Traffic(边的双连通)
给定一个图,并给定边,a b c(c==1||c==2) 表示ab之间有c条边 求把尽可能多的有向边定向变成强联通图. 先把图当做无向图,加边时记录是否有边,dfs的时候不要把本没有的边用到!因为这个 ...
- HTTP状态码及其含义 503 500 401 200 301 302
下表显示了常见的HTTP 1.1状态代码以及它们对应的状态信息和含义. 应当谨慎地使用那些只有HTTP 1.1支持的状态代码,因为许多浏览器还只能够支持HTTP 1.0.如果你使用了HTTP 1.1特 ...
- 遇到Class Not registered的COM异常怎么办
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:遇到Class Not registered的COM异常怎么办.
- Xcode6中使用initWithTitle:title image:image selectedImage:自定义图片
使用xcode6来运行项目,发现使用原生的tabbar上的图片不显示了. 这个问题是因为xcode6中的一些api方法被废弃了,同时tabbar上图片的渲染方式发生了改变. 先看xcode6中的tab ...
- iOS 基础知识
1获取系统语言设置 NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSArray *languages = ...
- PowerShell优化和性能测试
measure-command -expression {$null = "abc"} 效率高于 {"abc" |out-null } foreach {$p ...
- excel导入数据库iis设置
导入成功以后,基本这个小项目的所有功能都开发完成了,请IT部门帮我设定了一个固定IP,我以本机作为服务器,在本机IIS上发布了一个测试版,结果上传Excel数据报错, 错误信息“未在本地计算机上注册“ ...
- Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置
QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m ...
- 【转】cocos2d-x获取系统时间——2013-08-25 10
欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...