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的更多相关文章

  1. [Cpp primer] Library vector Type

    #include<vector> using std::vector; //Vector is a container. //It has a collection of same typ ...

  2. Library string Type

    The string type supports variable-length character strings.The library takes cares of managing memor ...

  3. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  4. string Type

    Notes from C++ Primer Operations Operations of string support lots of operations of sequential conta ...

  5. 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 ...

  6. setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)

    对于setCharacterEncoding(java.lang.String),这个方法是javax.servlet.ServletRequest和javax.servlet.ServletResp ...

  7. Library vector Type

    vector的定义 vector是C++标准库里最常用的容器,它之所以被称为容器,是因为它可以存放多个对象,所有在用一个容器中的对象都应该具有相同的类型. vector也是一个类模板,这也是它能存放多 ...

  8. cpp 实现简易String类

    需求 实现一个String类 自己写的String headers/String.h #ifndef __MYSTRING__ #define __MYSTRING__ #include <st ...

  9. Excel Sheet Column Title (STRING - TYPE CONVERTION)

    QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet ...

随机推荐

  1. Android版本的"Wannacry"文件加密病毒样本分析(附带锁机)

    一.前言 之前一个Wannacry病毒样本在PC端肆意了很久,就是RSA加密文件,勒索钱财.不给钱就删除.但是现在移动设备如此之多,就有一些不法分子想把这个病毒扩散到移动设备了,这几天一个哥们给了一个 ...

  2. linux远程win7教程

    http://jingyan.baidu.com/article/c275f6bacd2227e33c756754.html 1 在ubuntu下搜索Remmina(超级方便,应该也可以控制linux ...

  3. vuex(三)actions

    actions:既然mutations只能处理同步函数,我大js全靠‘异步回调’吃饭,怎么能没有异步,于是actions出现了...   actions和mutations的区别     1.Acti ...

  4. Break point and VC bound

    Restriction of Break Point e.g: k=2 说明在所有的dichotomy中,任意两个点不能被shatter(shatter就是能够出现所有种排列组合),即不能出现这两个点 ...

  5. BZOJ5301: [Cqoi2018]异或序列(莫队)

    5301: [Cqoi2018]异或序列 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 400  Solved: 291[Submit][Status ...

  6. linux自学(五)之开始centos学习,Xshell远程连接

    上一篇:linux自学(四)之开始centos学习,网络配置 前面操作都是在电脑中的虚拟机上操作的,比较麻烦,需要来回切换.下面我将使用远程连接工具Xshell进行操作,Xshell直接百度下载即可. ...

  7. (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序

    每次使用 Visual Studio 的模板创建一个 UWP 程序,我们会在项目中发现大量的项目文件.配置.应用启动流程代码和界面代码.然而这些文件在 UWP 程序中到底是如何工作起来的? 我从零开始 ...

  8. String.format(2)

    转载:https://blog.csdn.net/feng_870906/article/details/6870788 String.format是在JDK1.5中新增的静态方法,功能强.它主要功能 ...

  9. mix deps HEX_HTTP_CONCURRENCY=1 HEX_HTTP_TIMEOUT=120 timeout

    mix  deps.get  timeout 问题: If this happens consistently, adjust your concurrency and timeout setting ...

  10. 使用neon 开发nodejs addon

    备注:开发使用的是mac 系统,需要安装rust nodejs .python2.7 Xcode 1. 安装neon npm install -g neon-cli   2. 创建简单项目 neon ...