字符串的声明:

    string s;
string str="abcdefg";
char ch[]="abcdefg";
//使用string类型初始化另一个string类型
string s0(str);
string s1(str,); //第2个字符开始复制cdefg
string s2(str,,); //从第二个字母开始复制连续3个字符cde
cout<<s2<<endl;
//使用char类型初始化另一个string类型
string s3(ch);
string s4(ch,); //开始复制前三个abc
string s5(ch,,); //从第二个字母开始复制连续3个字符cde
cout<<s3<<endl; //abcdefg
cout<<s4<<endl; //abc
cout<<s5<<endl; //cde

 字符串的输入:

    cin>>s;     //不能有空格
cout<<s<<endl; s=cin.get(); //每次输入一个字符
cout<<s<<endl; getline(cin,s,'\n'); //每次输入一个字符串,以第三个参数作为结束符号
cout<<s<<endl;

String的常用方法:

    //copy
string s="abcde123";
string s1=s;
cout<<s1<<endl;
//link
string s2="Hello ";
string s3="World";
s2+=s3;
cout<<s2<<endl;
//compare
string s4="abc";
string s5="ea";
int c=s4>s5;
cout<<c<<endl;
int d=s4<s5;
cout<<d<<endl;
int e=s4==s5;
cout<<e<<endl;
//flip #include<algorithm>
string ss="Hello World!";
reverse(ss.begin(),ss.end());
cout<<ss<<endl;
//find:return the index of the first find of the string "ll"
string str="Hello ,yello";
cout<<str.find("ll")<<endl;
//replace
string ch="thisismywork";
ch.replace(,,"dd12"); //the substring(a,b) of String ch is replaced "dd12";
cout<<ch<<endl;
//append
string s6="Hello ";
s6.append("World"); //add a string "World" after the string s6;
cout<<s6<<endl;
//push_back
string s7="Hello";
s7.push_back('!'); //add a character after the string s7;
cout<<s7<<endl;
//insert
string s8="HelloWorld";
s8.insert(,"PPP");
cout<<s8<<endl; //insert a string "PPP" into the index(2) of the string s8 ;
//erase
string s9="";
s9.erase(,);
cout<<s9<<endl; //delete the substring from the index(3) continuous 4 of the string s9;
//swap
string str1="Hello";
string str2="world";
str1.swap(str2);
cout<<str1<<" "<<str2<<endl; //swap the string str1 and the string str2
//size();
string str3="abcd e";
cout<<str3.size()<<endl; //the size of the string str3;
//length();
string str4="abcd e";
cout<<str4.length()<<endl; //the length of the string str4; cout<<str4.max_size()<<endl; //return the max length of the string str4; s.clear();
s.empty();

C++字符串(String)的更多相关文章

  1. javascript类型系统——字符串String类型

    × 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...

  2. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  3. Java常量字符串String理解

    Java常量字符串String理解 以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用  "eq ...

  4. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

  5. Javascript基础系列之(三)数据类型 (字符串 String)

    javascript一共有9种数据类型 字符串 String 数值型 Number 布尔型 Boolean 未定义 Undefine 空值 Null 对象 Object 引用Refernce 列表型 ...

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

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

  7. java中字符串String 转 int(转)

    java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...

  8. 字符串String类

    1. String类是一个密封类.用关键字sealed修饰: 2. 字符串的两个特性:     ·不可变性:string类型变量,一旦声明就表明它是不会被改变的.因此,string中的方法对strin ...

  9. 字符串string和内存流MemoryStream及比特数组byte[]互转

    原文:字符串string和内存流MemoryStream及比特数组byte[]互转   字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...

  10. 【stanford C++】字符串(String)与流(Stream)

    字符串(String)与流(Stream) 一.C++中字符串(String) 字符串(String):就是(可能是空的)字符序列. C++中的字符串在概念上和Java中的字符串类似. C++字符串用 ...

随机推荐

  1. Operand forms

    Operand forms Computer Systems A Programmer's Perspective Second Edition

  2. 用户登录验证例题用的ajax

    1.登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  3. IOS常见的三种回调方法介绍

    认识下三种IOS常见的回调模式. 代理模式作为IOS中最常见的通讯模式,代理几乎无处不在. 这里有一个数组,我们首先通过代理的方式将数组传递到其他方法中去. 设置协议及方法 @protocol Cal ...

  4. Android高级之第十一讲Hybird开发

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 随着移动端应用平台的兴起,需求和交互方式的多样化,H5开发逐渐在移动端流行起来:常见的移动产品有We ...

  5. 使用bcrypt进行用户密码加密的简单实现

    Bcrypt百度百科: bcrypt,是一个跨平台的文件加密工具.由它加密的文件可在所有支持的操作系统和处理器上进行转移.它的口令必须是8至56个字符,并将在内部被转化为448位的密钥. 除了对您的数 ...

  6. ArcGIS Engine开发之旅07---文件地理数据库、个人地理数据库和 ArcSDE 地理数据库中的栅格存储加以比较 、打开栅格数据

    原文:ArcGIS Engine开发之旅07---文件地理数据库.个人地理数据库和 ArcSDE 地理数据库中的栅格存储加以比较 .打开栅格数据 对文件地理数据库.个人地理数据库和 ArcSDE 地理 ...

  7. MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集(转)

    namespace Entity { public interface IPagedList { /// <summary> /// 记录数 /// </summary> in ...

  8. 判断cdn上的图片可以正常访问到

    昨天晚上cdn宕机1小时,要对上传的资检查,写了个简单的小脚本来实现上传过的资源都是正常的(其实非必须),就是练手防止生疏. arr.each do |a | res = Net::HTTP.get_ ...

  9. 【转】shell脚本中echo显示内容带颜色

    shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e.格式如下:   echo -e "\033[41;36m something here \033[0m&qu ...

  10. java 获取请求客户端的真实IP地址

    转载自:http://leiyongping88.iteye.com/blog/1545930 用request.getRemoteAddr();方法获取的IP地址是:127.0.0.1或192.16 ...