c/c++ string
string类的定义、操作。
#include<iostream>
#include<string>
using namespace std;
int main()
{
// 4 declare
string s1; //default
string s2( s1 );
string s3( "hello" );
string s4( 9, 's' );
//read and write
string s_1, s_2;
cin>>s_1>>s_2; //igore blank in head, find blank in tail will end;
cout<<s_1<<endl;
//读入未知数目的文本
string word;
while( cin>>word ) //read until end-of -file
{
cout<<word<<endl;
if( word == "exit" )
{
cout<<"exit success"<<endl;
break;
}
}
//用getline读取整行文本,getline不会保存换行符
string line;
while( getline( cin, line ) )
{
cout<<line<<endl;
if( line == "exit" )
{
cout<<"exit success"<<endl;
break;
}
}
//string.size() and string.empty
string str_size;
cout<<"test string size"<<endl;
cin>>str_size;
if( str_size.empty() )
cout<<"string is empty"<<endl;
else
cout<<"string is not empty"<<endl;
//配套类型string::size_type实现与及其无关;int 型变量容易出错,不便移植;unsigned 可用于string::size_type可用的地方。
//关系操作符==、+=、<、<=、>、>=,使用字典顺序排序
string big = "big", small = "small", substr = "hello",phrase = "hello world"; //substr 小于 phrase
//string赋值,有效率上的问题
string str1, str2 = "value assignment come with efficent problem";
str1 = str2;
//string对象相加
str2 = "c plus plus";
str1 = "hell";
str2 += str1;
//和字符串字面值的连接,必须用string类型作为左值
str1 = str2 + "hello world" + "!!!";
//下标操作string单个字符
for( string::size_type ix = 0; ix != str1.size(); ix++ )
{
cout<< str1[ix] <<endl;
str1[ix] = '*';
}
//下标值计算
str1[2*1] = 'a';
cout<<str1<<endl<<"下标计算"<<endl;
//string中单个字符处理
cout<<"单个字符的处理"<<endl;
if( ispunct(str1[2]) )
cout<<"str1[2] 是一个标点"<<endl;
string s;
cout<<s[0]<<endl; //越界
return 0;
}
操作单个字符时使用cctype定义的函数,满足条件则为true:
isalnum(c) 是字母或数字
isalpha(c) 是字母
iscntrl(c) 是控制字符
isdigit(c) 是数字
isgraph(c) 不是空格,可打印
islower(c) 小写字母
isprint(c) 可打印字符
ispunct(c) 标点符号
isspace(c) 空白字符
isupper(c) 大写字母
isxdigit(c) 十六进制数
tolower(c) 是大写字母时返回小写字母,否则返回c
toupper(c) 是小写字母时返回大写字母,否则返回c
c/c++ string的更多相关文章
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
- BCL中String.Join的实现
在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...
随机推荐
- 四.Android adb命令(持续更新...)
1.安装:甭管从哪里下载下来的apk,放在指定的目录下,不一定非要是sdk的目录下:adb install "d:\hxcjaz.apk"(指定的一个目录)2.卸载:adb uni ...
- NPOI的操作
public async Task<MemoryStream> ExportExcel(IList<fuquestionbank> _list, string pId, str ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Noise Contrastive Estimation
Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...
- AnjularJS系列5 —— scopes、module、controller
第五篇, scopes.module.controller 这一篇,感觉,在前面几篇就使用过的属性,但,总觉得没有理解透彻,有待完善!~ 1.scopes A.定义:$scope是一个把view(一个 ...
- Jenkins 安装FAQ
1.对路径的访问被拒绝,如: 解决办法:以管理员模式进入DOS命令窗口,参考Jenkins安装中的第4步: 2.服务不能启动,提示: 解决办法:启动Windows Service(InstallU ...
- JS数组经典冒泡排序
将8,4,3,1,4,6,等数字按照从小到大的顺序依次输出: var arr=new Array(); arr.push(8); arr.push(4); arr.push(3); arr.push( ...
- MyEclipse部署web项目到Tomcat出现An internal error occurred during: "Launching on Tomcat 7.x"的问题
如果出现了上述的错误按照如下的3个步骤解决:1.首先关闭MyEclipse工作空间.2.然后删除工作空间下的文件."MyEclipse10\workspace.metadata.plugin ...
- Spring--多人开发
在spring中, 可以定义多个配置文件. 例子: ①首先定义一个班级类和一个学生类 1 package com.fuwh.spring; } } ...
- Unity StrangeIoC框架
Unity StrangeIoC框架 http://blog.csdn.net/y1196645376/article/details/52746251