C++字符串技术 string类

   string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;

  可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;

  一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。

  大多数的编译器已经实现了C++标准的std::string类~

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
/* stlstring.cpp
    C++字符串技术  string类
    string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;
    可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;
    一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。
    大多数的编译器已经实现了C++标准的std::string类~
*/

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    //构造字符串,给字符串赋值
    string myString1("Michael Joessy!");
    string myString2(, 'q');
    string myString3 = "Michael Jordan~";
    string myString4(myString1);
    string myString5 = myString3;

cout << myString1 << endl;
    cout << myString2 << endl;
    cout << myString3 << endl;
    cout << myString4 << endl;
    cout << myString5 << endl;

//字符串读入和读出
    //string::operator>>  string::operator<<    string::getline
    string str;
    //getline(cin, str, ' ');
    cout << str << endl;

//字符串判断函数
    /*字符串判断为空函数*/
    string empString("");
    if (empString.empty())
    {
        cout << "empString is empty!" << endl;
    }
    /*字符串长度函数*/
    string testString = "1234567890";
    cout << testString << endl;
    cout << "testString length is " << testString.length() << endl;
    cout << "testString size is " << testString.size() << endl;
    testString.resize();
    cout << testString << endl;
    cout << "testString length is " << testString.length() << endl;
    cout << "testString size is " << testString.size() << endl;
    /*提示:length()和size()的功能是一样的,size()是基于STL概念提出来的,而length()还是原来C语言的东西,
            为了更好地符合STL的编程概念,建议使用size()。
    */

/*字符串指针函数*/
    string str1("023");
    cout << "str1 = " << str1 << endl;
    cout << "str1 = " << str1.c_str() << endl;      //c_str()返回字符串的指针

//增加字符串成员
    string strAppend("Michael ");
    cout << strAppend << endl;
    char szTest[] = "Jackson";
    strAppend.append(szTest);
    cout << strAppend << endl;
    string strInsert = " great";
    strAppend.append(strInsert, );
    cout << strAppend << endl;
    strAppend.append(strInsert.begin(), strInsert.end());
    cout << strAppend << endl;

strAppend += " person";
    cout << strAppend << endl;

strAppend.push_back('!');
    cout << strAppend << endl;

//字符串比较函数
    /*关系运算符 == > >= < <=*/
    string strt1 = "Michael";
    string strt2 = "Michael";
    if (strt1 == strt2)
    {
        cout << "strt1 == strt2" << endl;
    }
    string strt3 = "Michael Joessy";
    if (strt3 >= strt1)
    {
        cout << "strt3 >= strt1" << endl;
    }
    /*compare*/
    )
    {
        cout << "strt1 == strt2" << endl;
    }
    )
    {
        cout << "strt3 != strt1" << endl;
    }

//字符串查找
    string strfind("abcdefg");
    ;
    nPos = strfind.find('c');
    cout << "The char c pos is " << nPos << endl;
    char szArray[] = "def";
    nPos = strfind.find(szArray);
    cout << "The szArray[] pos is " << nPos << endl;
    char szArrayNone[] = "drf";
    nPos = strfind.find(szArrayNone);
    cout << "The szArrayNone[] pos is " << nPos << endl;

nPos = strfind.rfind('c');
    cout << "The char c pos is " << nPos << endl;
    nPos = strfind.rfind(szArray);
    cout << "The szArray[] pos is " << nPos << endl;
    nPos = strfind.rfind(szArrayNone);
    cout << "The szArrayNone[] pos is " << nPos << endl;

string strCha = "aaabbbcccdddeeefffggghhh";
    nPos = strCha.find_first_of('b');
    cout << "The find_first_of b pos is " << nPos << endl;
    nPos = strCha.find_first_not_of('b');
    cout << "The find_first_not_of b pos is " << nPos << endl;
    nPos = strCha.find_last_of('b');
    cout << "The find_last_of b pos is " << nPos << endl;
    nPos = strCha.find_last_not_of('b');
    cout << "The find_last_not_of b pos is " << nPos << endl;

//字符串下标和字串函数
    string strSub = "Hello World!";
    cout << strSub << endl;
    ];
    );
    strSub.at() = '~';
    cout << strSub << endl;
    strSub.assign("Hello Man!");
    cout << strSub << endl;

cout << strSub.substr() << endl;

//字符串其它函数
    string strOrg = "Hello Hero!";
    cout << strOrg << endl;
    strOrg.insert(, "At ");
    cout << strOrg << endl;
    cout << strOrg.capacity() << endl;
    cout << strOrg.size() << endl;
    strOrg.replace(, "Ha");
    cout << strOrg << endl;

cin.get();
    ;
}

C++ string类学习总结的更多相关文章

  1. java中String类学习

    java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...

  2. java中String类学习笔记

    1.String的两种实例化方式 String str="hello";//直接赋值的方式: String str=new String("hello");// ...

  3. 关于String类学习的一些笔记(本文参考来自程序员考拉的文章)

    String 类继承自 Object 超类,实现的接口有:Serializable.CharSequence.Comparable<String> 接口,具体如下图: 一.常用的Strin ...

  4. 《C++ Primer Plus》16.1 string类 学习笔记

    16.1.1 构造字符串程序清单16.1使用了string的7个构造函数.程序清单16.1 str1.cpp---------------------------------------------- ...

  5. Java第二次作业——数组和String类

    Java第二次作业--数组和String类 学习总结 1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法.举例说明equals方法和==的区别 ...

  6. JAVA学习第二十九课(经常使用对象API)- String类

    多线程告一段落,開始经常使用对象API的涉及,背也要背下来!.! 日后开发,遇见最多的对象是文字,也就是字符串 String类 字符串是一个特殊对象 字符串一旦初始化就不能够被改变 一.特点 publ ...

  7. 20175212童皓桢 在IDEA中以TDD的方式对String类和Arrays类进行学习

    20175212童皓桢 在IDEA中以TDD的方式对String类和Arrays类进行学习 要求 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 ...

  8. 20155312张竞予 20170510实践一:在IDEA中以TDD的方式对String类和Arrays类进行学习

    实践题目 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 sort binarySea ...

  9. 20155326 第12周课堂实践总结(二)String类和Arrays类的学习

    20155326 第12周课堂实践总结(二)String类和Arrays类的学习 实践二 Arrays和String单元测试 实践题目 在IDEA中以TDD的方式对String类和Arrays类进行学 ...

随机推荐

  1. a标签添加点击事件

      a标签添加点击事件 CreateTime--2017年8月8日09:11:34 Author:Marydon 一.基础用法 方式一:(不推荐使用) <a href="javascr ...

  2. 〖Linux〗不重复启动某应用程序的脚本

    cmd="/home/scue/bin/ipclient $ipclient" exist=$(ps aux | grep -v 'grep' | grep "$cmd& ...

  3. Mybatis-Generator自动生成代码

    在使用mybatis开发的过程中,通常我们会给数据库的每张表编写对应的model.dao.mapping,虽然很简单,但是工作量很大,所以通常会使用代码生成器Mybatis-Generator帮我们自 ...

  4. cxf发布 webservice服务

    导包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar commons-lang-2.6.ja ...

  5. Linux-软件包管理-rpm命令管理-安装-卸载

    mount 确认光盘是否挂载 mount /dev/cdrom /mnt/cdrom 将设备名称/dev/cdrom安装到/mnt/cdrom挂载点下面 mount 查看光盘是否已经挂载 (ro表示只 ...

  6. maven打包到本地仓库里面

    mvn install:install-file -Dfile=D:/你的包名  -DgroupId=com.sdk4j -DartifactId=sdk4j -Dversion=1.0 -Dpack ...

  7. javascript异步代码的回调地狱以及JQuery.deferred提供的promise解决方式

    我们先来看一下编写AJAX编码常常遇到的几个问题: 1.因为AJAX是异步的,全部依赖AJAX返回结果的代码必需写在AJAX回调函数中.这就不可避免地形成了嵌套.ajax等异步操作越多,嵌套层次就会越 ...

  8. stm32 堆和栈(stm32 Heap & Stack)

    关于堆和栈已经是程序员的一个月经话题,大部分有是基于os层来聊的. 那么,在赤裸裸的单片机下的堆和栈是什么样的分布呢?以下是网摘: 刚接手STM32时,你只编写一个 int main() { whil ...

  9. JStorm环境搭建

    开始JStorm学习之前需要搭建集群环境,这里演示搭建单机JStorm环境,仅供学习使用,生产环境部署大同小异,但建议参考JStorm社区及相关说明文档. 一.前提 JStorm核心代码均用Java实 ...

  10. 关于 AngularJS 的数据绑定

    单向绑定(ng-bind) 和 双向绑定(ng-model) 的区别 ng-bind 单向数据绑定($scope -> view),用于数据显示,简写形式是 {{}}. 1 <span n ...