[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 ...
随机推荐
- [置顶]
滴滴插件化VirtualAPK框架原理解析(二)之Service 管理
在前一篇博客滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理 中VirtualAPK是如何对Activity进行管理的,本篇博客,我们继续来学习这个框架,这次我们学习的是如何 ...
- Git详解之九 Git内部原理
以下内容转载自:http://www.open-open.com/lib/view/open1328070620202.html Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各 ...
- socket创建TCP服务端和客户端
看情况选择相对应的套接字*面向连接的传输--tcp协议--可靠的--流式套接字(SOCK_STREAM)*面向无连接的传输--udp协议--不可靠的--数据报套接字(SOCK_DGRAM) 在liun ...
- 博客迁移至“零一积流|it-refer.com”
虽然在博客园写了没几篇文章,考虑到个人发展,自己还是建立一个独立的站点:零一积流. 以后直接在自己网站上写东西了,此处只用做文章收藏.
- grub2 设置Windows为默认启动系统
1. 首先找到Windows的菜单menuentry.<blockquote># cat /boot/grub2/grub.cfg | grep Windows 结果: menuentry ...
- 可能是 BJOI2019 Day1 题解?
T1 给一个有空白字符的串 $S$,和若干模板串 $X_i$,初始 $Ans = 1$,每当一个模板串在 $S$ 中作为子串出现时,$Ans$ 会乘以 $X_i$ 的权值 $Val_i$,然后如果 $ ...
- LOJ6039. 「雅礼集训 2017 Day5」珠宝【决策单调性优化DP】【分治】【思维好题】
LINK 懒得搬题面 简要题意:n个物品,每个物品有一个价格和一个吸引力,问你对于\(i \in [1,k]\),花费i的价格能得到的最大吸引力 其中价格的范围很小,在\([1,300]\)范围内 思 ...
- 携程框架Apollo实现.NET Core微服务统一配置(测试环境-单机)
Apollo实现.NET Core微服务统一配置(测试环境-单机) https://www.cnblogs.com/guolianyu/p/10065999.html 一.前言 注:此篇只是为测试环境 ...
- [UOJ310][UNR #2]黎明前的巧克力
uoj description 给你\(n\)个数,求从中选出两个交集为空的非空集合异或和相等的方案数模\(998244353\). sol 其实也就是选出一个集合满足异或和为\(0\),然后把它分成 ...
- grpc 安装以及墙的解决方法
1. 默认官方文档 go get -u google.golang.org/grpc 因墙的问题,大部分安装是无法完成的 2. 解决方法 a. grpc mkdir -p $GOAPTH/src/go ...