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++字符串用 ...
随机推荐
- improve performance whilemaintaining the functionality of a simpler and more abstract model design of processor hardware
Computer Systems A Programmer's Perspective Second Edition In this chapter, we take a brief look at ...
- Apache Kafka源码分析 - autoLeaderRebalanceEnable
在broker的配置中,auto.leader.rebalance.enable (false) 那么这个leader是如何进行rebalance的? 首先在controller启动的时候会打开一个s ...
- ORACLE的安装与网页版创建表空间的简单操作以及PLsql的简单操作
1.oracle的安装: 安装简单易学,在这里不做解释.下载看装包后耐心等待,注意安装目录不要有中文字符,尽量按照指定目录进行安装.安装完成后会占用有大约5g的内存. 如果要卸载oracle,需要用其 ...
- Git学习笔记(持续更新)
1.强制同步为远程的代码 远程仓库回退了commit的情况下(第2条描述之情况),强制同步远程的代码到本地 #更新远程最新的所有代码,但是不merge或者rebase git fetch --all ...
- spark之combineByKey
combineByKey def combineByKey[C](createCombiner: (V) => C, mergeValue: (C, V) => C, mergeCombi ...
- day11
JSP入门 1 JSP概述 1.1 什么是JSP JSP(Java Server Pages)是JavaWeb服务器端的动态资源.它与html页面的作用是相同的,显示数据和获取数据. 1.2 ...
- Object的属性property详细解释(自动生成成员变量)
类Class中的属性property: 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfac ...
- Android生命周期详细说明
提供两个关于Activity的生命周期模型图示帮助理解: 图1 图2 从图2所示的Activity生命周期不难看出, ...
- c#导出excel(转)
C#导出Excel文件实例代码 2010-08-03 14:10:36| 分类: 软件编程 | 标签:excel c#导出excel |字号大中小 订阅 /// <summary> ...
- css中的zoom的使用
css中的zoom的使用 zoom : normal | number normal : 默认值.使用对象的实际尺寸 number : 百分数 | 无符号浮点实数.浮点实数值为1.0或百分数为1 ...