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”这样的 ...
随机推荐
- PL/SQL数据库,Oracle登录
用户名:TESTZYPX_9999 数据库:10.75.142.242:1521/orcl
- C#窗体程序【用户控件-窗体】委托事件
这里的自定义控件是由普通控件组合而成的.希望事件响应代码推迟到使用自定义控件的窗体里写.步骤一:新建一个用户控件,放两个按钮,Tag分别是btn1,btn2.这两个按钮的共用单击事件处理代码如下: u ...
- phpcms二次开发中无法获取SESSION的值
今天在在phpcms开发留言板用到验证码,提交数据,后台无法$_SESSION['code']无法获取验证码值,也无法打印var_dump($_SESSION)值,我们只需要在文件头部添加如下代码: ...
- 教你开发jQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文: ...
- FZU 2105Digits Count(线段树 + 成段更新)
Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operation 1: AN ...
- github 相关英语
github 相关英语 repository n. 仓库 A repository contains all the files for your project, including the rev ...
- ios 快速审核
https://developer.apple.com/contact/app-store/?topic=expedite
- Tlist删除技巧
二. 从TList开始分析-- 为了写一个更好的性能ISAPI Filter,我需要更快速地从TList中删除部分连续的Item.比如这样的一段代码: var p : pChar = 'abcd ...
- 移动端 Web 开发前端知识整理
文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...
- React 组件性能优化探索实践
转自:http://www.tuicool.com/articles/Ar6Zruq React本身就非常关注性能,其提供的虚拟DOM搭配上Diff算法,实现对DOM操作最小粒度的改变也是非常的高效. ...