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. Eclipse下搭建SWT与Swing图形界面开发环境

    一.SWT与Swing介绍 SWT(StandardWidget Toolkit)则是由Eclipse项目组织开发的一套完整的图形界面开发包,虽然当初仅仅是IBM为了编写Eclipse的IDE环境才编 ...

  2. AngularX Http服务总结

    自己经常用的方式: 1.首先看httpModule Angular2的http访问经常采用两种方式: 共同点:导入相应的Module import {Http, RequestOptions, Res ...

  3. Lua table

    获取数组长度 在Lua中可以使用“#”和table.maxn两种方法来获取数组的长度 arr = {,,,} arr[] = 7 都仅统计数字key的长度: #是从1递增到nil的长度: table. ...

  4. PHP 生成类似 SqlServer NEWID() 全局唯一标识符

    最近在对接SqlServer中对方有一个字段要求 SqlServer 中 有一个 NEWID() 就是生成36位的 唯一标识符 ->  8CBD3198-297D-4037-A859-B27BB ...

  5. 《DSP using MATLAB》示例Example7.3

    由图上可以看出,与幅度谱对应的相位谱是分段线性函数,而与振幅谱对应的相位谱是真正线性函数. 幅度谱和振幅谱的区别也很明显.

  6. {vlFeat}{matlab}{VS2010}{编译配置}

    运行程序需要vlfeat与mex等在matlab与vs2010中配置,碰到了不少困难,下面给出解决方案 1.下载vlfeat,但是vlfeat目录中并没有编译好的mex文件,需要在vs2010中编译 ...

  7. Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律

    给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是  abs(原数小数部分相加-1*n), 多一个0 则 m ...

  8. php 必须了解提升的知识

    https://blog.csdn.net/m18513057343/article/details/78974292

  9. 在Google的GKE上创建支持Internal Load Balancer的Service

    在Google的Kubernetes Engine上发布service,可以采用除On-Promise相同的Cluster IP和NodePort两种方式外,还可以创建LoadBalaner的Serv ...

  10. cowboy的路由方式

    直接贴代码 route_helper.erl -module(route_helper). -export([get_routes/]). get_routes() -> [ {'_', [ % ...