关于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的操作的更多相关文章

  1. Library string Type

    The string type supports variable-length character strings.The library takes cares of managing memor ...

  2. [Cpp primer] Library string Type

    In order to use string type, we need to include the following code #include<string> using std: ...

  3. 【java】String类和StringBuffer类常用操作

    String类是字符串常量,是不可更改的常量.而StringBuffer是字符串变量,它的对象是可以扩充和修改的.StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于Stri ...

  4. 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 ...

  5. 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 ...

  6. 出错: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 ...

  7. 问题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 ...

  8. 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 ...

  9. 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'

    springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...

随机推荐

  1. 【Java基础】关于String的总结

    String构造方法初始化和常量赋值初始化区别 下面的代码是一个String对象的两种不同的初始化方式,关于这两种不同初始化方式的区别,本文通过画内存图来进行解释,首先代码如下: public cla ...

  2. iOS的UILabel设置居上对齐,居中对齐,居下对齐

    在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐.具体如下: // //  myUILabel.h ...

  3. Zabbix lld发现磁盘监控

    一.软件版本 操作系统:CentOS-6.5-x86_64 zabbix版本:3.0.3 二.脚本编写: 1.python版本: #!/usr/bin/env python import json i ...

  4. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

  5. gallery左右滑动时图片淡入淡出

    前几天,公司项目有一个功能要做成滑动图片的淡入淡出,要一边滑动一边改变,所以ViewFlipper左右滑动效果就不能了.网上找了很久,也找不到资料,所以自己写了一个,通过滑动改变imageView的透 ...

  6. (转载)Ant教程

    ant教程(一) 写在所有之前 为了减少阅读的厌烦,我会使用尽量少的文字,尽量明白的表达我的意思,尽我所能吧.作为一个学习者,在我的文章中会存在各种问题,希望热心人指正.目录大概是这样 ant教程 ( ...

  7. android开发之使用上下文菜单

    android中的上下文菜单类似于PC上的鼠标右键单击,不同的是android上没有鼠标这一概念,更谈不上右键单击,在android中,一般是长按某个View,调出上下文菜单.与OptionsMenu ...

  8. Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...

  9. Android(java)学习笔记210:采用post请求提交数据到服务器(qq登录案例)

    1.POST请求:  数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦   2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...

  10. iOS之定位与地图

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用 和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一 ...