#include <iostream>
#include <string>
using namespace std;
int main()
{
// 문자열 생성
string str1;
string str2 = "string";
// 문자열 입력 및 출력
cin >> str1;
cout << "첫번째 문자열: " << str1 << endl;
cout << "두번째 문자열: " << str2 << endl;
return 0;
}
✔ 문자열 비교 (<, >, ==)
string str1, str2;
cin >> str1 >> str2;
if (str1 == str2)
cout << "동일한 문자열";
else if (str < str2)
cout << "첫번째 문자열이 사전순으로 빠름";
else
cout << "두번째 문자열이 사전순으로 빠름";
✔ 문자열 연결 (+)
string str1 = "debug";
string str2 = "ing";
cout << str1 << "g" << str2; // "debugging"
✔ 문자열 원소 접근 (element access)
function | description |
---|---|
str[index] | 문자열에서 index에 해당하는 문자 반환 |
str.at(index) | 문자열에서 index에 해당하는 문자 반환 |
str.front() | 문자열의 가장 앞 문자 반환 |
str.back() | 문자열의 가장 뒤 문자 반환 |
✔ 문자열 크기 (capacity)
function | description |
---|---|
str.length() | 문자열의 길이 반환 |
str.size() | 문자열의 길이 반환 (문자열의 크기) |
str.empty() | 빈 문자열인지 확인 |
✔ 문자열 수정 (modifier)
function | description |
---|---|
str.append(target) | 문자열 뒤에 target 문자열을 이어 붙임 |
str.append(target, index, n) | 문자열 뒤에 target 문자열의 index 위치에서 n개의 문자를 이어 붙임 |
str.insert(index, target) | 문자열의 index 위치에 target 문자열을 삽입 |
str.erase(index) | 문자열의 index 위치부터 끝까지의 문자열을 제거 |
str.erase(index, n) | 문자열의 index 위치부터 n개의 문자를 제거 |
str.replace(index, n, target) | 문자열의 index 위치부터 n개의 문자를 target 문자열로 교체 |
✔ 문자열 연산 (operation)
function | description |
---|---|
str.substr(index) | 문자열에서 index 위치부터 끝까지의 문자열을 반환 |
str.substr(index, n) | 문자열에서 index 위치부터 n 길이의 문자열을 반환 |
str.find(target) | 문자열에서 target 문자열을 찾음. 있으면 찾은 위치를, 없으면 string::npos를 반환 |
str.find(target, index) | 문자열에서 target 문자열을 index의 위치에서부터 찾음. |
str.rfind(target, index) | 문자열에서 target 문자열을 index의 위치에서부터 거꾸로 찾음. |
str.find_first_of(targets, index) | 문자열의 index 위치에서부터 targets에 속한 문자들 중 가장 먼저 발견된 문자의 위치를 반환 |
str.find_first_not_of(targets, index) | 문자열의 index 위치에서부터 targets에 속하지 않은 문자들 중 가장 먼저 발견된 문자의 위치를 반환 |
✔ 그 외에 유용한 함수들..
<string>
function | description |
---|---|
stoi(str) | 문자열을 int 타입으로 변환하여 반환 |
to_string(value) | 값 value를 문자열로 변환하여 반환 (value는 int, double, char 등의 타입) |
<cctype>
function | description |
---|---|
isdigit(c) | 문자가 숫자 형태의 문자이면 1, 그 외에는 0을 반환 |
isalpha(c) | 문자가 알파벳(소문자, 대문자)이면 1, 그 외에는 0을 반환 |
isalnum(c) | 문자가 알파벳 또는 숫자 형태의 문자이면 1, 그 외에는 0을 반환 |
isupper(c) | 문자가 대문자이면 1, 그 외에는 0을 반환 |
islower(c) | 문자가 소문자이면 1, 그 외에는 0을 반환 |
toupper(c) | 문자를 대문자로 변환하여 반환 |
tolower(c) | 문자를 소문자로 변환하여 반환 |
✔ 문자열을 반복해서 찾는 경우
int cnt = 0;
string text, target;
cin >> text >> target;
int idx = -1;
while ((idx = text.find(target, idx + 1)) != string::npos)
{
cnt++;
}
cout << cnt << "개의 문자열 발견";
✔ 괄호 안에 있는 문자열 추출
string str = "삼성전자(005930) 카카오(035720) 현대차(005380)";
string code;
int fpos = str.find("(");
int lpos = str.find(")", fpos);
while (fpos != string::npos && lpos != string::npos)
{
code = str.substr(fpos + 1, lpos - fpos - 1);
cout << code << " ";
fpos = str.find("(", lpos);
lpos = str.find(")", fpos);
}
✔ 구분자 (delimiter)로 문자열 분리
string text, sentence;
string delimiter = ".?!";
getline(cin, text);
int fpos = text.find_first_not_of(delimiter);
int lpos = text.find_first_of(delimiter);
while (fpos != string::npos || lpos != string::npos)
{
sentence = text.substr(fpos, lpos - fpos);
cout << sentence << endl;
fpos = text.find_first_not_of(delimiter, lpos);
lpos = text.find_first_of(delimiter, fpos);
}