[Cpp primer] Library string Type
In order to use string type, we need to include the following code
#include<string>
using std::string;
1. Defining and initializing strings
string s1; //Default initialization; s1 is the empty string
string s2(s1); //s2 is a copy of s1.
string s2 = s1; //Equivalent to s2(s1), s2 is a copy of s1.
string s3("value"); //Direct initialization, s3 is a copy of the string literal, not including the null
string s3 = "value"; //Equivalent to s3("value"), s3 is a copy of the string literal.
string s4(n, 'c'); //s4="cccc...ccc", n c
2. Operations on strings
os << s; //Write s onto output stream os. Return os. ex: cout<<s;
is >> s; //Reads whitespace-separeted(if it comes across a whitespace, it stops reading) string from is into s. Rreturn is. ex: cin>>s;
getline(is, s); //Reads a line of input from is to s. Return is. is can be 'cin' and other types.
//getline(cin, s) This function can read a total line, including whitespace. If it comes across the end-of-line, it stops reading.
s.empty();
s.size(); //Returns the number of characters in s. If s has whitespace, it still counts. Null character is not included in this size.
/*
size() doesn't return a int type value.
However, it returns a string::size_type value.
size_type is a companion type of string.
It's an unsigned type.
We should be aware that we'd better not to mix int with size_type.
*/
s1 == s2;
s1 != s2;
>, <, <=, >=
//We can use the strategy as a(case-sensitive) dictionary to compare twos strings.
s1 + s2;
s1 += s2;
/*
adding two strings together
It won't add a whitespace automatically.
s1 = "666", s2 = "777"
s3 = s1+s2 = "666777"
*/
string s1 = "mdzz";
s1 = s1 + "haha"; //1
s1 = "haha" + s1; //2
/*
String literals are not standard library strings.
Hence, string literals can not be the left-hand operand.
2 is wrong.
1 is ok.
*/
3. Dealing with the Characters in a string
#include<cctype>
//Using this header, we can use many functions to deal with the characters in a string.
isalnum(c); //True: c is a letter or a digit
isalpha(c); //True: c is a letter
iscntrl(c); //True: c is a control character
isdigit(c); //True: c is a digit
isgraph(c); //True: c is not a space but is printable
islower(c); //True: c is a lowercase letter
isupper(c);
isprint(c); //True: c is a printable character
ispunct(c); //True: c is a punctuation character
isspace(c); //True: c is a whitespace
isxdigit(c); //True: c is a hexadecimal digit
tolower(c);
toupper(c);
[Cpp primer] Library string Type的更多相关文章
- [Cpp primer] Library vector Type
#include<vector> using std::vector; //Vector is a container. //It has a collection of same typ ...
- Library string Type
The string type supports variable-length character strings.The library takes cares of managing memor ...
- Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...
- string Type
Notes from C++ Primer Operations Operations of string support lots of operations of sequential conta ...
- load_library(linker.cpp:759): library "libmaliinstr.so" not found
1.02-07 15:19:09.277: E/linker(16043): load_library(linker.cpp:759): library "libmaliinstr.so&q ...
- setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)
对于setCharacterEncoding(java.lang.String),这个方法是javax.servlet.ServletRequest和javax.servlet.ServletResp ...
- Library vector Type
vector的定义 vector是C++标准库里最常用的容器,它之所以被称为容器,是因为它可以存放多个对象,所有在用一个容器中的对象都应该具有相同的类型. vector也是一个类模板,这也是它能存放多 ...
- cpp 实现简易String类
需求 实现一个String类 自己写的String headers/String.h #ifndef __MYSTRING__ #define __MYSTRING__ #include <st ...
- Excel Sheet Column Title (STRING - TYPE CONVERTION)
QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet ...
随机推荐
- 04-python第四天学习
(1)for循环里的else In [1]: nums = [11,22,33,44] In [2]: for temp in nums: ...: print(temp) ...: else: #e ...
- iOS-Core Data 详解
使用Core Data 框架 Core Data框架本质就是一个ORM(对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一 ...
- Postfix常用命令和邮件队列管理(queue)
本文主要介绍一下postfix的常用命令及邮件队列的管理: Postfix有以下四种邮件队列,均由管理队列的进程统一进行管理: maildrop:本地邮件放置在maildrop中,同时也被拷贝到inc ...
- [IC]Lithograph(2)光刻技术的分辨率与分辨率增强技术
接上一篇介绍IC制造的基本过程,光刻的基本过程.这篇文章继续介绍光刻过程中的一些概念. 该系列文章的目录如下: [IC]Lithograph(0)半导体制造的基本过程 [IC]Lithograph(1 ...
- Makefile中怎么使用Shell if判断
/********************************************************************* * Makefile中怎么使用Shell if判断 * 说 ...
- Python:版本升级(Mac OS X)
Mac OS X 10.8及以后的版本都预装了Python 2.7,但是在Mac上(Unix-like OS)上修改Python的版本并不如Windows方便.这篇文章的目标是要将Mac自带的Pyth ...
- 10day1
但愿复赛的时候旁边坐的不是学军镇海杭二绍一的众神犇. 阅览室 模拟 [问题描述] 一个阅览室每天都要接待大批读者.阅览室开门时间是 0,关门时间是 T.每位读者的到达时间都 不一样,并且想要阅读的 ...
- appium-java,连接真机启动微信
1.启动appium 2.设置信息 3.设置IP和端口 4.appium连接手机,微信中打开debugx5.qq.com,信息->TBS settings->是否打开TBS内核Insper ...
- 每天一个linux命令:【转载】ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写,缺省下ls用来打印出当前目录的清单,如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- debug调试工具不显示的解决办法
清空在runtime目录下的debug目录下的文件即可.