string 라이브러리

윤이령·2026년 4월 23일

string 라이브러리

#include <iostream>
#include <string>

using namespace std;

int main() {
	string str1("Hello");
	cout << str1[0] << endl;
    cout << str1[1] << endl;
    cout << str1[2] << endl;
    cout << str1[3] << endl;
    cout << str1[4] << endl;

	return 0;
}

length, size

  • 뒤에 붙는 문자가 없다 -> C언어는 문자 끝에 문자열의 끝을 표시하는 \0문자가 붙음
#include <iostream>
#include <string>

using namespace std;

int main() {
	string str1("Hello");
	cout << str1 << endl;
	cout << str1.length() << endl; // 길이
	cout << str1.size() << endl; // 메모리 크기

	return 0;
}

empty

  • 문자열이 비어있으면 true, 아니면 false
#include <iostream>
#include <string>
using namespace std;

int main() {
	string str1("");
	cout << str1 << endl;

	cout << std::boolalpha;
	cout << str1.empty() << endl;

	return 0;
}

append

  • 문자열.append("추가할문자열", 문자열시작인덱스, 문자개수)
#include <iostream>
#include <string>
using namespace std;

int main() {
	string str1("hello");
	str1.append(" world");
	cout << str1 << endl; // hello world

	string str2("Hello");
	str2.append(" world!", 6, 1);
	cout << str2 << endl; // Hello!

	return 0;
}

find

  • 문자열.find(찾을문자열, 시작위치);
  • 문자열을 찾지 못하면 string::npos 라는 상수를 반환
#include <iostream>
#include <string>

using namespace std;

void check_found(string::size_type n) {
	if (n == string::npos) { // 문자열을 찾지 못했는지 검사
		cout << "not found" << endl;
	}
	else {
		cout << "found index : " << n << endl;
	}
}

int main() {
	string::size_type n; // 문자열의 크기를 나타내는 형식(부호없는 정수)
	string str = "This is an example of a standard string.";
	//문자열 시작 지점부터 example 탐색
	n = str.find("example");
	check_found(n); // 11

	//문자열 내 index위치 4부터 is 탐색
	n = str.find("is", 4);
	check_found(n); // 5

	return 0;
}

compare

  • 대상문자열.compare(비교할문자열);
#include <iostream>
#include <string>

using namespace std;

void compare_result(int result) {
	if (result == 0) {
		cout << result << " = 두 문자열이 같음" << endl;
	}
	else if (result > 0) {
		cout << result << " = 대상 문자열이 더 길거나 일치하지 않는 첫 번째 문자가 더 큼" << endl;
	}
	else if (result < 0) {
		cout << result << " = 대상 문자열이 더 짧거나 일치하지 않는 첫 번째 문자가 더 작음" << endl;
	}
}

int main() {
	string s1 = "Hello";
	string s2 = "Hello";
	int result = s1.compare(s2); // 0
	compare_result(result);

	s1 = "Hello";
	s2 = "Hello World";
	result = s1.compare(s2); // -1
	compare_result(result);

	s1 = "cat";
	s2 = "dog";
	result = s1.compare(s2); // -1 : c가 d보다 알파벳 순서가 더 작음
	compare_result(result);

	s1 = "Hello World";
	s2 = "Hello";
	result = s1.compare(s2); // 1
	compare_result(result);

	s1 = "cat";
	s2 = "apple";
	result = s1.compare(s2); // 1 : c가 a보다 알파벳 순서가 더 큼
	compare_result(result);
}
  • 관계 연산자 사용
#include <iostream>
#include <string>
using namespace std;

int main() {
	string s1 = "Hello";
	string s2 = "Hello";
	if (s1 == s2) {
		cout << "두 문자열 일치" << endl;
	}

	s1 = "Hello";
	s2 = "World";
	if (s1 != s2) {
		cout << "두 문자열 불일치" << endl;
	}
	return 0;
}

replace

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

int main() {
	string text = "The C++ programming language is one of the hardest languages.";
	string target = "hardest";
	string replacement = "most powerful";

	size_t pos = text.find(target); // 처음 등장하는 위치 찾기
	
	if (pos != string::npos) {
		//위치, 변경대상문자열 개수, 변경할 문자열
		text.replace(pos, target.length(), replacement);
		cout << "고체 후 문장 : " << text << endl;
	}
	else {
		cout << target << "을 찾을 수 없음" << endl;
	}

	return 0;
}

wstring

  • 와이드 문자열
  • 유니코드를 더 직관적으로 표현할 수있음
  • 유니코드 문자열 처리에 편리한 함수도 제공
    ``cpp
    #include
    #include
    using namespace std;

int main() {
//프로그래므이 지역 설정
setlocale(LC_ALL, ""); // 모든 지역
//유니코드 문자열 초기화
wstring korString = L"안녕하세요.";
//유니코드 문자열 출력
wcout << korString << endl;

return 0;

}

0개의 댓글