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. 【Oracle】PL/SQL Developer 快捷键、使用技巧

    1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它好用多了. 2.设置关键字自动大写:Tools-& ...

  2. Linode之使用UE实现SSH连接

    在Linode上建立了节点后,选择对应的操作系统(我选用的是Ubuntu 12.04 LTS),然后启动就可以.详细能够參见该文(https://library.linode.com/getting- ...

  3. 【转帖】为什么不要把ZooKeeper用于服务发现

    http://www.infoq.com/cn/news/2014/12/zookeeper-service-finding ZooKeeper是Apache基金会下的一个开源的.高可用的分布式应用协 ...

  4. 常见URL字符及URL编码值

    大家上网的时候一定会看到很多这类情况有的网页地址都是%22%32%11%23%21等这种机器语言恐怕只有机器能马上辨认吧现在我把大概知道的总结一下 字符                         ...

  5. cat /proc/iomem

    在proc目录下有iomem和ioports文件,其主要描述了系统的io内存和io端口资源分布. 对于外设的访问,最终都是通过读写设备上的寄存器实现的,寄存器不外乎:控制寄存器.状态寄存器和数据寄存器 ...

  6. 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)

    非变动性算法代码分析与示例: 一.for_each  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14   // TEMPLATE FUNCTION for_eac ...

  7. spi flash偶尔出现写入错误的情况

    spi flash W25Q128会偶尔出现写入错误的情况,会发现读出的值和写入的值不一致,需加入2次读出比较判断. W25QXX_Read(&temp_date_count,0x000000 ...

  8. Vue2.0+Webpack项目环境构建到发布

    前言:为什么要用webpack搭建项目呢,因为这个工具可以把目前浏览器不全部支持的ES6语法,通过打包工具生成所有浏览器都支持的单个JS文件. 参考: https://blog.csdn.net/u0 ...

  9. Atitit.软件按钮与仪表盘(13)--全文索引操作--db数据库子系统mssql2008

    Atitit.软件按钮与仪表盘(13)--全文索引操作--db数据库子系统mssql2008 全文索引操作 4.全文索引和like语句比较 1 5.倒排索引 inverted index 1 2.SQ ...

  10. 【Android】15.5 例15-3—Notification的各种属性演示

    分类:C#.Android.VS2015: 创建日期:2016-02-29 一.简介 利用这个例子,可测试通知的各种属性以及这些不同属性选项呈现的效果. 另外,在这个例子中,还演示了如何读写SD中的图 ...