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〗Debian 7.1.0 Wheezy使用ltib报错的解决办法

    报错内容: scue@Link:/home/work/ltib$ ./ltib Processing platform: Phytec board with the NXP LPC32XX SoC = ...

  2. datagrid("getSelections")只获取一行

    页面加载方法如下 function loadSfXtjsList(sfXtjsListId, url, onClickFun) { $("#eastPanel").panel({ ...

  3. 总结js(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    http://hi.baidu.com/yashua839/blog/item/131fdb2fe547ef221f3089af.html一.Iframe 篇 //&&&&am ...

  4. 关于flex,好像有12个属性非常重要

    关于Flex,有12个属性非常重要 这几天在学习Flex布局,发现Flex真的好厉害! Flex是Flexible Box的缩写,意为"弹性布局",用来为盒模型提供最大的灵活性. ...

  5. atoi 和 itoa

    转自:http://www.cnblogs.com/cobbliu/archive/2012/08/25/2656176.html atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C ...

  6. Java数据结构和算法(三):常用排序算法与经典题型

    常用的八种排序算法 1.直接插入排序 我们经常会到这样一类排序问题:把新的数据插入到已经排好的数据列中.将第一个数和第二个数排序,然后构成一个有序序列将第三个数插入进去,构成一个新的有序序列.对第四个 ...

  7. 怎样在tsung中使用动态參数(二)

    上一篇博客说过,在配置getOrderId请求时,能够用动态变量(order_id)解析和捕获服务端返回的json对象.这个变量能够作为接下来的订单确认请求(Confirm)的输入參数.看一下Conf ...

  8. Unity和安卓互调

    Unity调安卓 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); And ...

  9. xUtils工具实现下载功能

    private String download_url="http://192.168.2.8:80/DownZip/*****.zip";//下载的路径 public  Stri ...

  10. dubbo相关

    1 面试题:Dubbo中zookeeper做注册中心,如果注册中心集群都挂掉,发布者和订阅者之间还能通信么? 可以的,启动dubbo时,消费者会从zk拉取注册的生产者的地址接口等数据,缓存在本地.每次 ...