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. Okhttp之连接池ConnectionPool简单分析(一)

    开篇声明:由于本篇博文用到的一些观点或者结论在之前的博文中都已经分析过,所以本篇博文直接拿来用,建议读此博文的Monkey们按照下面的顺序读一下博主以下博文,以便于对此篇博文的理解: <Okht ...

  2. openssl编译出错解决

    tar -jxvf trafficserver-3.0.2.tar.bz2 ./configure --prefix=/usr/install/trafficserver --with-user=ca ...

  3. UIview的一些属性

    一.UIView(视图\控件)1.在屏幕上能看得见.摸得着的东西,都是UIView对象2.任何UIView都可以充当其他UIView的容器3.关于UIView的一些疑问1> 谁来管理UIView ...

  4. rabbitMQ高可用

    镜像模式 镜像模式和普通模式的区别就是,队列的数据都镜像了一份到所有的节点上.这样任何一个节点失效,不会影响整个集群的使用. 在实现上,mirror queue内部有一套选举算法,会选出一个maste ...

  5. c语言 变量的存储类别以及对应的内存分配?

    <h4><strong>1.变量的存储类别</strong></h4>从变量值存在的角度来分,可以分为静态存储方式和动态存储方式.所谓静态存储方式指在程 ...

  6. c++中子类转父类,父类转子类

    #include <iostream> using namespace std; class Father { public: virtual void show() { cout< ...

  7. arm_linux QT+v4l 显示视频

    1.参考(原创)基于ZedBoard的Webcam设计(三):视频的采集和动态显示 下载代码实测可用. 2.重新下载了csdn的代码,缺widget.h文件,后重新生成widget工程(自动产生wid ...

  8. ecmall页面空白解决方案(转)

    页面空白解决方案: ------------------------------------------------------------------------------------------ ...

  9. git还原某个特定的文件到之前的版本

    场景: 对于某个git控制下的文件进行了修改,但是改的不满意,想退回到改之前的版本.假定该文件为 src/main/main.c 解决方法: 第一步: 在命令行中输入 git log src/main ...

  10. 笔记本设置wifi热点并抓包

    现在很多人喜欢蹭wifi热点,这里演示一下怎么利用笔记本设置wifi热点来钓鱼.本机是win10操作系统. 一.设置笔记wifi热点:右键点击右下角网络图标 -> 打开“网络和Internet设 ...