프로그래밍/C++
[C++] String 문자 비교 및 Substr 예제
HSDY
2023. 2. 2. 12:13
반응형
코드
# include <iostream>
# include <string>
using namespace std;
int main()
{
string s1;
cout << s1.size() << endl;
s1 = "Hello"; // Operator = (); 메모리 할당, 값을 할당
cout << s1.size() << endl;
s1 = s1 + "world"; // Operator + () 문자열 연결
cout << s1 << endl;
cout << s1.size() << endl;
string s2 = "Helloworld";
if (s1 == s2)
{
cout << "같다" << endl;
}
else
cout << "다르다" << endl;
if (s1 != s2)
cout << "다르다" << endl;
else
cout << "같다" << endl;
string path = "c:\\temp\\test\\test.txt";
cout << path << endl;
int n = path.find_last_of("\\");
string filename = path.substr(n + 1);
cout << filename << endl;
cout << path.substr(3, 5);// index 3번째에서 5개 문자를 출력
}
결과