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的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. --关于null在oracle数据库中是否参与计算,进行验证,

    --关于null在oracle数据库中是否参与计算,进行验证,with td as (select null id,1 name from dual ),td1 as ( select null id ...

  2. 精灵方向移动问题[math.floor]

    local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...

  3. Android基础总结(三)

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  4. php截取中文无乱码

    在PHP中需要对字符串进行截取,如果没有装mb扩展(mb_substr函数),对中文截取就需要进行相应的处理.下面是对字符串 "世s界s的功s\\\夫萨的mn是非得失sdf dsf dsf ...

  5. mysql 创建存储过程报错

    在创建存储过程前把结束符定义为 delimiter // 然后再创建就不会报错

  6. nandflash的读写(2440)

    说明: 根据物理结构上的区别 , NandFlash主要分为如下两类:1)•SLC (Single Level Cell): 单层式存储2)•MLC (Multi Level Cell): 多层式存储 ...

  7. 创建Mat对象的几种方法

    1.Mat的构造函数 Mat M(行数,列数,数据类型,通道数) eg:M(2,2, CV_8UC3, Scalar(0,0,255)). 2.利用Mat的Create()函数.Mat M; M.cr ...

  8. IO例子

    1.用字节读取一个文件,替换换行符,并打印 String fileName = "src/learnIO/Stream.java"; FileInputStream in = ne ...

  9. Unity3D 动态改变地形

    直接获取TerrainData进行修改即可 using System.Collections; using UnityEngine; using UnityEditor; public class D ...

  10. shell 计算

    1.整数计算 n=$((/)) echo $n 输出 2(注意必须是两个括号) exper / / 注意这里不支持括号,a%b,a/b后取余 output: 1 2.小数计算 bc <<& ...