博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Cpp primer] Library string Type
阅读量:6685 次
发布时间:2019-06-25

本文共 2463 字,大约阅读时间需要 8 分钟。

In order to use string type, we need to include the following code

#include
using std::string;

  

1. Defining and initializing strings

string s1; //Default initialization; s1 is the empty stringstring 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 nullstring 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; //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 togetherIt won't add a whitespace automatically.s1 = "666", s2 = "777"s3 = s1+s2 = "666777"*/string s1 = "mdzz";s1 = s1 + "haha"; //1s1 = "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
//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 digitisalpha(c); //True: c is a letteriscntrl(c); //True: c is a control characterisdigit(c); //True: c is a digitisgraph(c); //True: c is not a space but is printableislower(c); //True: c is a lowercase letterisupper(c);isprint(c); //True: c is a printable characterispunct(c); //True: c is a punctuation characterisspace(c); //True: c is a whitespaceisxdigit(c); //True: c is a hexadecimal digittolower(c);toupper(c);

转载于:https://www.cnblogs.com/KennyRom/p/6422115.html

你可能感兴趣的文章
struct2
查看>>
纯CSS3实现的一些酷炫效果
查看>>
faster-rcnn(testing): ubuntu14.04+caffe+cuda7.5+cudnn5.1.3+opencv3.0+matlabR2014a环境搭建记录
查看>>
ORACLE日期格式
查看>>
TestNG
查看>>
asp.net 源码坊4-5源码发布
查看>>
R语言基础3
查看>>
深度优先之货物搬运路径
查看>>
C# 基本术语概念
查看>>
Python爬虫学习笔记之点触验证码的识别
查看>>
苹果承认137名中国员工致残
查看>>
面向对象
查看>>
[1601]3n+1数链问题 sdutOJ
查看>>
jdbcTemplate 后台接口中的分页
查看>>
Excel 之查找与替换
查看>>
Mysql实现rownum
查看>>
python:使用OO和工厂模式解决问题
查看>>
C++学习-2
查看>>
SQL中查询数据表字段名称的查询语句
查看>>
关于masonry
查看>>