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. Ant—怎样Windows操作系统中搭建Apache Ant环境

    介绍一下怎样在Windows操作系统中搭建Apache Ant环境: 一.下载Apache Ant压缩文件:http://download.csdn.net/detail/wangshuxuncom/ ...

  2. HttpSession 和URLRewriting

    在上面使用Cookie技术存储会话信息的时候发现Cookie存储的数据有限,而且每次需要客户端浏览器携带数据,导致网络的负载过大.因此如果需要存储相对大量的数据,那么可以直接将数据存储在服务器端,这样 ...

  3. 转MQTT压力测试之Tsung的使用

    转自:http://www.cnblogs.com/lingyejun/p/7941271.html nTsung测试工具的基本测试命令为 Tsung -f  ~/.tsung/mqtt.xml -l ...

  4. 扩展GeoServer数据源

    今天我们来讲讲怎么扩展GeoServer(简称GS)的数据源.大家都知道,GS支持多种数据源,而且都提供了友好的界面供操作.下面我们就来简单介绍一下,如何把自定义的数据源增加到GS中,让我们可以在统一 ...

  5. Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言

    在曾经的一封邮件中,我指出在众多改变中,最明显的一个就是:在java领地上的JVM上使用其它流行的语言的发展变得越来越快.一些老的和新的创建的基于JVM的语言---JRuby 和 Jython ,Ja ...

  6. html学习一(html简史及doctype)

    html3部分 doctype(html) dtd head body 一.深入浅出HTML与XHTML的区别 HTML(HyperText Markup Language,超文本标记语言)最早的HT ...

  7. Spring Cloud 模块简介

    Spring Cloud Netflix对微服务的支持还有: Hystrix: 断路器和资源隔离 Feign: 声明式HTTP REST请求客户端 Ribbon: 与Eureka结合实现软负载均衡 Z ...

  8. chrome 浏览器插件开发

    一.chrome 浏览器插件开发是什么: 1 从技术上说插件只是一个存在于本地的一个网站.所以呢在插件开发的过程中用到的技术无非是 javascript .html .css . 二.把当前活动页面的 ...

  9. IIS目录禁止执行权限

    IIS6: IIS7:

  10. Webkit初始化以及载入URL过程中各种对象的建立时序以及DOM树的建立详情分析

            众所周知,Webkit须要创建DOM树. 为此它须要创建WebView, Chrome,Page,Frame, Document. Document Parser, DOM Tree ...