#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;
}
#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;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1("");
cout << str1 << endl;
cout << std::boolalpha;
cout << str1.empty() << endl;
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
int main() {
//프로그래므이 지역 설정
setlocale(LC_ALL, ""); // 모든 지역
//유니코드 문자열 초기화
wstring korString = L"안녕하세요.";
//유니코드 문자열 출력
wcout << korString << endl;
return 0;
}