Skip to content

[C++] How to Use "string" of STL

Last Updated on 2021-11-02 by Clay

string is a string container provided by C++ Standard Template Library (STL). Compared with char, you can do more complex string operations.

A simple example is, if you want to store a string in a char array, you need to set the max length of it, and you must be expand the array size when you using more than default length.

But if you use string container to do it, you can add new string directly without considering the length limitation.

In below, I will introduce some function in string container.

  • basic operation
  • length()
  • clear()
  • empty()
  • assign()
  • append()
  • find()

basic operation

If you want to print the characters of string.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string s = "Today is a nice day.";

    // Print all sentence
    cout << s << endl;

    // Print every character
    for (auto c: s) {
        cout << c << endl;
    }
    
    return 0;
}



Output:

Today is a nice day.
T
o
d
a
y
 
i
s
 
a
 
n
i
c
e
 
d
a
y
.

As you can see, we can print string container directly or print every characters. In addition to use the for-range, you can also use index to print the corresponding character.

For example, if you want to print the index 0 character T:

cout << s[0] << endl;



Output:

T

length()

length() is a function to get the string length.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string s = "Today is a nice day.";

    // length()
    cout << s.length() << endl;
    
    return 0;
}



Output:

20

clear()

clear() can clear all the content in string.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string s = "Today is a nice day.";

    // Before
    cout << s << endl;

    // clear()
    s.clear();

    // After
    cout << s << endl;
    
    return 0;
}



Output:

Today is a nice day.

The second line is blank.


empty()

empty() can check a string is null or not. If the string is null, return 1; if not, return 0.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string s = "Today is a nice day.";

    // Before
    cout << s << s.empty() << endl;

    // clear()
    s.clear();

    // After
    cout << s << s.empty() << endl;
    
    return 0;
}



Output:

Today is a nice day.0
1

assign()

assign() is a function used to assign new string.

str_01.assign(str_02, START_INDEX, NUM)

str_02 is the string we want to assign.
START_INDEX is the copy start point index.
NUM is the number of characters we want to assign to str_01.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string str_01;
    string str_02 = "Today is a nice day.";
    
    // assign()
    str_01.assign(str_02, 6, 100);

    // Print
    cout << str_01 << endl;

    return 0;
}



Output:

is a nice day.

In fact, str_02 doesn't have 100 characters, so all characters in str_02 are assigned.


append()

append() is similar to assign(), but the added string is add at the end.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string str_01 = "I want to go to play.";
    string str_02 = "Today is a nice day.";
    
    // append()
    str_01 += "\n";
    str_01.append(str_02);

    // Print
    cout << str_01 << endl;

    return 0;
}



Output:

I want to go to play.
Today is a nice day.

As you can see, you can actually use + to concatenate strings.


References


Read More

Tags:

Leave a Reply