๐Ÿ“š String

๐Ÿ“š String(๋ฌธ์ž์—ด)

  • ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐํƒ€์ž…
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์  ํฌ๊ธฐ ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌ๊ฐ€๋Šฅ

๐Ÿ“š ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

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

int main() {
    string str = "Hello, world!";  // ๋ฌธ์ž์—ด ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
    cout << str << endl;  // ์ถœ๋ ฅ: "Hello, world!"
    return 0;
}
  • ํ—ค๋” ํŒŒ์ผ ์กด์žฌ
  • using namespace std; ๋ฅผ ํ†ตํ•ด std ์ƒ๋žตํ•จ
  • โ€ โ€œ ํฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ

๐Ÿ”Ž ์ฃผ์š” ๊ธฐ๋Šฅ

  • ๋ฌธ์ž์—ด์˜ ๊ธธ์ด : size(), length() ๋‘˜ ๋‹ค ๋™์ผํ•˜๊ฒŒ ์ž‘๋™
string str = "Hello, world!";
cout << "Length: " << str.length() << endl;  // ๊ธธ์ด ์ถœ๋ ฅ: 13
cout << "Size: " << str.size() << endl;     // ํฌ๊ธฐ ์ถœ๋ ฅ: 13
  • ๋ฌธ์ž ์ ‘๊ทผ : ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉ์‚ฌ์—ฌ ์ ‘๊ทผ ๊ฐ€๋Šฅ
string str = "Hello";
cout << str[0] << endl;  // 'H' ์ถœ๋ ฅ
cout << str[4] << endl;  // 'o' ์ถœ๋ ฅ
  • ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ : โ€œ+โ€ ๋ฅผ ํ†ตํ•ด ๊ฒฐํ•ฉ ๊ฐ€๋Šฅ
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2;  // ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ
cout << result << endl;  // "Hello World" ์ถœ๋ ฅ
  • ๋ฌธ์ž์—ด ๋น„๊ต : โ€œ=โ€ ์„ ํ†ตํ•ด ํŒ๋ณ„ ๊ฐ€๋Šฅ
string str1 = "apple";
string str2 = "banana";
if (str1 == str2) {
    cout << "Equal" << endl;
} else {
    cout << "Not Equal" << endl;  // "Not Equal" ์ถœ๋ ฅ
}
  • ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ(substring) : substr() ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ์‹œ์ž‘ ์œ„์น˜์™€ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ถ”์ถœ
string str = "Hello, world!";
string sub = str.substr(7, 5);  // "world"
cout << sub << endl;  // "world" ์ถœ๋ ฅ

substr(start, length) ์˜ ํ˜•ํƒœ๋กœ, start๋Š” ์‹œ์ž‘ ์ธ๋ฑ์Šค, length๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์ด๋‹ค.

  • ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰(find) : find()ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด ๋‚ด์—์„œ ํŠน์ • ๋ฌธ์ž๋‚˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค
string str = "Hello, world!";
size_t pos = str.find("world");  // "world"์˜ ์œ„์น˜ ์ฐพ๊ธฐ
if (pos != string::npos) {
    cout << "'world' found at index " << pos << endl;  // 7 ์ถœ๋ ฅ
}

find()ํ•จ์ˆ˜๋Š” ์ฐพ์œผ๋ฉด ์ฒ˜์Œ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ฐพ์ง€ ๋ชปํ•˜๋ฉด npos๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(hash table์—์„œ๋Š” npos ๋Œ€์‹  end()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.)

  • ๋ฌธ์ž์—ด ์‚ญ์ œ(erase) : erase() ํ•จ์ˆ˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์‚ญ์ œ ๊ธฐ๋Šฅ
string str = "Hello, world!";
str.erase(5, 7);  // 5๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 7๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์‚ญ์ œ
cout << str << endl;  // "Hello" ์ถœ๋ ฅ

erase(start, length) ์˜ ํ˜•ํƒœ๋กœ start๋Š” ์‹œ์ž‘ ์ธ๋ฑ์Šค, length๋Š” start๋ถ€ํ„ฐ ์‚ญ์ œํ•  ๊ธธ์ด์ด๋‹ค.

  • ๋ฌธ์ž์—ด ์‚ฝ์ž…(insert) : insert() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ํŠน์ • ์œ„์น˜์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•œ๋‹ค.
string str = "Hello";
str.insert(5, ", world!");  // 5๋ฒˆ ์ธ๋ฑ์Šค์— ", world!" ์‚ฝ์ž…
cout << str << endl;  // "Hello, world!" ์ถœ๋ ฅ

insert(start, โ€œstringโ€) ์˜ ํ˜•ํƒœ๋กœ, start๋Š” ์‚ฝ์ž…ํ•  ์‹œ์ž‘ ์œ„์น˜๋กœ string์„ ์‚ฝ์ž…ํ•œ๋‹ค.

  • ๋ฌธ์ž์—ด ์น˜ํ™˜(replace) : replace() ํ•จ์ˆ˜๋Š” ํŠน์ • ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๊ต์ฒดํ•˜๋Š” ๊ธฐ๋Šฅ
string str = "Hello, world!";
str.replace(7, 5, "C++");  // "world"๋ฅผ "C++"๋กœ ๊ต์ฒด
cout << str << endl;  // "Hello, C++!" ์ถœ๋ ฅ

replace(start, length, โ€œstringโ€) ์˜ ํ˜•ํƒœ๋กœ, start๋Š” ๊ต์ฒดํ•  ์‹œ์ž‘ ์ธ๋ฑ์Šค, length๋Š” start๋ถ€ํ„ฐ ๊ต์ฒดํ•  ๊ธธ์ด, string์„ ๊ทธ ์œ„์น˜์— ๋„ฃ์Œ


๐Ÿ’ก ๋ฌธ์ž์—ด ๊ด€๋ จํ•จ์ˆ˜

  • c_str() : std::string์„ C ์Šคํƒ€์ผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
string str = "Hello";
const char* cstr = str.c_str();
cout << cstr << endl;  // "Hello" ์ถœ๋ ฅ
  • clear() : ๋ฌธ์ž์—ด์„ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ดˆ๊ธฐํ™”
string str = "Hello";
str.clear();
cout << str << endl;  // ๋นˆ ๋ฌธ์ž์—ด ์ถœ๋ ฅ
  • resize() : ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝ
string str = "Hello";
str.resize(3);  // "Hel"
cout << str << endl;
  • swap() : ๋‘ ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ์„œ๋กœ ๊ตํ™˜
string str1 = "Hello";
string str2 = "World";
str1.swap(str2);
cout << str1 << " " << str2 << endl;  // "World Hello"

๐Ÿ”‘ C++11 ์ดํ›„์˜ ๋ฌธ์ž์—ด ๊ธฐ๋Šฅ

  • std::to_string() : ์ˆซ์ž ํƒ€์ž…์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ
  • std::stoi(), std::stol(), std::stof() : ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ

๐Ÿ“Œ ์ •๋ฆฌ

  • std::string์€ ๋™์  ํฌ๊ธฐ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
  • ๋ฌธ์ž์—ด์„ ๋น„๊ต, ๊ฒฐํ•ฉ, ๊ฒ€์ƒ‰, ์ž๋ฅด๊ธฐ, ์‚ฝ์ž…, ์‚ญ์ œ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ž์—ด ์กฐ์ž‘ ๊ฐ€๋Šฅ

๐Ÿ’ก ์ถ”๊ฐ€

isdigit() ํ•จ์ˆ˜(03-08 ์ถ”๊ฐ€)

  • isdigit()๋Š” ๋ฌธ์ž c๊ฐ€ 0~9 ์‚ฌ์ด์˜ ์ˆซ์ž์ผ ๋•Œ๋งŒ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ๋‹ค๋ฅธ ๋ฌธ์ž๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, โ€˜aโ€™, โ€˜ โ€˜, โ€˜!โ€™ ๋“ฑ์€ ๋ชจ๋‘ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.