C++字符串(String)
字符串的声明:
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)的更多相关文章
- javascript类型系统——字符串String类型
× 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- Java常量字符串String理解
Java常量字符串String理解 以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用 "eq ...
- 字符串string类型转换成DateTime或DateTime?类型
常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...
- Javascript基础系列之(三)数据类型 (字符串 String)
javascript一共有9种数据类型 字符串 String 数值型 Number 布尔型 Boolean 未定义 Undefine 空值 Null 对象 Object 引用Refernce 列表型 ...
- Java基础——数组应用之字符串String类
字符串String的使用 Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的. Java中没有内置的字符串类型,而是在标准Java类库中提供了一 ...
- java中字符串String 转 int(转)
java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...
- 字符串String类
1. String类是一个密封类.用关键字sealed修饰: 2. 字符串的两个特性: ·不可变性:string类型变量,一旦声明就表明它是不会被改变的.因此,string中的方法对strin ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- 【stanford C++】字符串(String)与流(Stream)
字符串(String)与流(Stream) 一.C++中字符串(String) 字符串(String):就是(可能是空的)字符序列. C++中的字符串在概念上和Java中的字符串类似. C++字符串用 ...
随机推荐
- px_ipc_name.c
/* include px_ipc_name */ #include "unpipc.h" char * px_ipc_name(const char *name) { char ...
- P1371 NOI元丹
luogu月赛的题 本来想爆搜,但是经过ly大佬的点拨,明白这是一个dp. 我们定义dp[n]为从n开始的可行串的数目,具体如下:如果n为'I',则是从n开始有多少个I,如果n为'O',既是从n开始有 ...
- 【转】Unity 解析Json字符串
http://blog.csdn.net/qq_15267341/article/details/52013190 LitJSON使用很简单,两个步骤: 1 将LitJSON.dll文件拖动到unit ...
- [dpdk] 读官方文档(2)
续前节.切好继续: 一,文档里提到uio_pci_generic, igb_uio, vfio_pci三个内核模块,完全搞不懂,以及dpdk-devbind.py用来查看网卡状态,我得到了下边的输出: ...
- Netty 4(一) zero copy
Netty的“零拷贝”主要体现在如下三个方面: 1) Netty的接收和发送ByteBuffer采用DIRECT BUFFERS,使用堆外直接内存进行Socket读写,不需要进行字节缓冲区的二次拷贝. ...
- MSVC和MinGW组件dll相互调用
http://www.mingw.org/wiki/msvc_and_mingw_dlls MinGW调用VC: The other way is to produce the .a files fo ...
- java System.out
从写Hello World开始,我们就开始使用System.out了.System.out是一个static final 的PrintStream对象. 引用一段jdk API document的内容 ...
- EntityFramework执行SQL语句
在EF中执行Sql语句. using (var context = new EFRecipesEntities()) { string sql = @"insert into Chapter ...
- 纯Python包发布setup脚本编写示例
如果你有多个模块需要发布,而它们又存在于多个包中,那么指定整个包比指定模块可能要容易地多.即使你的模块并不在一个包内,这种做法也行的通:你可以告诉Distutils从根包(root package)处 ...
- jQuery实现隔行变色
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...