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. 〖Linux〗(2013.08.02)使用ctag+cscope查看Android源代码

    1. 安装ctags和cscope sudo apt-get install -y exuberant-ctags cscope 2. vimrc中的配置 """&quo ...

  2. C# XMLOperate

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  3. ui-router详解(二)ngRoute工具区别

    我们了解 angular.js 是一种富客户端单页面应用,所以要在一个页面呈现不同的视图,路由起到了至关重要的作用. angular.js 为我们封装好了一个路由工具 ngRoute ,它是一种靠ur ...

  4. apache虚拟主机设置泛域名的方法

    在apache虚拟主机中设置泛域名解析,主要是用到ServerAlias 的配置. 1.支持多域名 例如,让mail.jbxue.org.smtp.jbxue.org.pop3.jbxue.org 都 ...

  5. Java多线程简析——Synchronized(同步锁)、Lock以及线程池

    Java多线程 Java中,可运行的程序都是有一个或多个进程组成.进程则是由多个线程组成的.最简单的一个进程,会包括mian线程以及GC线程. 线程的状态 线程状态由以下一张网上图片来说明: 在图中, ...

  6. tracert路由跟踪工具使用方法

    1. 路由跟踪在线Tracert工具说明 Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据报访问目标所采取的路径.Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP ...

  7. JS 利用正则表达式替换字符串

    JS 利用正则表达式替换字符串 博客分类: JavaScript 学习资料 Java代码 收藏代码 JS 利用正则表达式替换字符串 var data = "123123,213,12312, ...

  8. Nginx 使用中文URL,中文目录路径

    Nginx 使用中文URL,中文目录路径 分类: linux2012-05-03 11:04 2672人阅读 评论(0) 收藏 举报 nginxurl服务器translationcentosserve ...

  9. python中sorted方法和列表的sort方法使用详解

    一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...

  10. angular初体验

    所有需要ng管理的代码必须被包裹在一个有ng-app指令的元素中ng-app是ng的入口,表示当前元素的所有指令都会被angular管理(对每一个指令进行分析和操作) 利用angular实现双向绑定: ...