Useful functions of string in C++

String append

string::append

append经常被+代替,他们作用相似。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main ()
{
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more";

// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3,6,3); // "10 "
str.append("dots are cool",5); // "dots "
str.append("here: "); // "here: "
str.append(10u,'.'); // ".........."
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
str.append<int>(5,0x2E); // "....."

std::cout << str << '\n';
return 0;
}
// Output: Writing 10 dots here: .......... and then 5 more.....

Upcase and lowercase

std::tolower

把大写字母转化为小写字母,每次只能对一个char进行转换,如果要对string进行转换,可以按照以下代码,来自这里

1
2
3
4
5
6
7
#include <algorithm>
#include <string>

std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

// For converting to lower case, you need to replace ::tolower with ::toupper .

std::toupper

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>       // std::cout
#include <string> // std::string
#include <locale> // std::locale, std::toupper

int main ()
{
std::locale loc;
std::string str="Test String.\n";

for (std::string::size_type i=0; i<str.length(); ++i)
std::cout << std::toupper(str[i], loc);

return 0;
}

transform

也可以参考这篇博客

Code sample:

1
transform(rs[i].begin(), rs[i].end(), rs[i].begin(), ::tolower);

String split

cstring::strtok

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* strtok example */ 
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="a,b,c,d*e";
const char * split = ",";
char * p;
p = strtok (str,split);
while(p!=NULL) {
printf ("%s\n",p);
p = strtok(NULL,split);
}

getchar();
return 0;
}

本例中,实现对字符串a,b,c,d\*e用逗号(,)来作界定符对字符串进行分割。输出结果将如下所示:

a
b
c
d*e

因为delimiters支持多个分割符, 我们将本示例中的语句行const char * split = ",";改成 const char * split = ",*"; //用逗号(,)和星号(*)对字符串进行分割。这样输出结果将如下所示:

a
b
c
d
e

split

c++中没有对string直接split的函数,string::strtok只能对char *进行split,如果要对string进行split可以使用下面的代码,同样支持多个分隔符:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}


vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}

规定自己就算死记硬背也要把上面的代码记住,做到快速手写出来

除了上面的方法,还可以使用boost库(需要安装boost库,或者直接把include的cpp直接复制过来):

1
2
3
#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("\t "));

string::find

Code sample:

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
#include <iostream>       // std::cout
#include <string> // std::string

int main ()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");

// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';

found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';

found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';

found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';

// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';

return 0;
}
//Notice how parameter pos is used to search for a second instance of the same search string.

Output:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.

transformation between string and other data types

string and int

可以查看这篇博客

Other functions

string::erase

erase函数有三种用法:

  • erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
  • erase(position);删除position处的一个字符(position是个string类型的迭代器)
  • erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

Code sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// string::erase
#include <iostream>
#include <string>

int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9); // ^
std::cout << str << '\n';
// "This is a sentence."
str.erase (str.begin()+5, str.end()-9); // ^^^^^
std::cout << str << '\n';
// "This sentence."
return 0;
}

trim

  1. 自己定义

    Code sample:

    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
    #include <iostream>
    #include <string>

    std::string& trim(std::string &);

    int main()
    {
    std::string s = " Hello World!! ";
    std::cout << s << " size:" << s.size() << std::endl;
    std::cout << trim(s) << " size:" << trim(s).size() << std::endl;

    return 0;
    }

    std::string& trim(std::string &s)
    {
    if (s.empty())
    {
    return s;
    }

    s.erase(0,s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);
    return s;
    }
  2. 使用boost

    可以参考这篇博客,很强大的boost……。