字符串的声明:

    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. Delphi中如何控制其他程序窗体上的窗口控件

    回调函数一般是按照调用者的要求定义好参数和返回值的类型,你向调用者提供你的回调函数的入口地址,然后调用者有什么事件发生的时候就可以随时按照你提供的地址调用这个函数通知你,并按照预先规定好的形式传递参数 ...

  2. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  3. spm完成dmp在windows系统上导入详细过程

    --查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...

  4. Tab指示符——Indicator

    先说说我们的思路吧. 其实思路也很简单,就是在咱们的导航下面画一个小矩形,不断的改变这个矩形距离左边的位置. 思路就这么简单,有了思路,接下来就是实现了,看代码: public class Indic ...

  5. (转)js闭包初入门

    先看一段JS代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 function a(){             var num = 0;             function  ...

  6. zabbix basic concept

    tomcat/mysql/hadoop http://www.linuxidc.com/Linux/2014-06/103776p2.htm http://www.aikaiyuan.com/2993 ...

  7. Qt调用dll中的功能函数

    声明: 事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的.并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下:   ...

  8. 移动设备优先viewport

    Bootstrap 3 的设计目标是移动设备优先,然后才是桌面设备.这实际上是一个非常及时的转变,因为现在越来越多的用户使用移动设备. 为了让 Bootstrap 开发的网站对移动设备友好,确保适当的 ...

  9. RESTFul Web Api 服务框架(一)

    简介: 基于 REST 的 Web 服务日益成为后端企业服务集成的首选,因为它比 SOAP 更加简单.这篇文章介绍了一 个简单的可扩展框架,使用Asp.net Web Api作为 REST 服务的实现 ...

  10. 网页上的表格数据table

    格式: <table> <tr> <th> </th> </tr> <tr> <td> </td> &l ...