C++에서 Left Right Mid Trim함수 사용하기이다.
#include <string>
#include <iostream>
using namespace std;
string Left(const string &Text, int TextLength) {
string ConvertText;
if (Text.size() < TextLength) {
TextLength = Text.size();
}
ConvertText = Text.substr(0, TextLength);
return ConvertText;
}
string Right(const string &Text, int TextLength) {
string ConvertText;
if (Text.size() < TextLength) {
TextLength = Text.size();
}
ConvertText = Text.substr(Text.size() - TextLength, TextLength);
return ConvertText;
}
string Mid(const string &Text, int Startint, int Endint) {
string ConvertText;
if (Startint < Text.size() || Endint < Text.size()) {
ConvertText = Text.substr(Startint, Endint);
return ConvertText;
}
else {
return Text;
}
}
string trim(string& str) {
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
return str;
}
// source url https://stackoverflow.com/questions/5343190/how-do-i-replace-all-instances-of-a-string-with-another-string
string ReplaceString(string subject, const string& search,
const string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
int main() {
// string spaces(20, ' '); repeat 20 spaces
// split & join
// check if it is a number string like -123.45
const char *longString = R""""(
This is
a very
long
string
)"""";
const string str2 = R"(
"Hello?
It's been a long time.
How are you doing?
I'm doing pretty good.
And you?"
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=killerq6434&logNo=90017206835
https://m.blog.naver.com/PostView.naver?blogId=killerq6434&logNo=90180563211&navType=tl
https://www.youtube.com/watch?v=QHalO6LU7w0
)";
string num1 = " 1 2 3 4";
string num2 = "012345678901234567890123456789012345678901";
string test = "Hello, C# is a great programming language.";
string l5 = Left(test, 5);
string r9 = Right(test, 9);
string m155 = Mid(test, 15, 5);
cout << "\n" << num1 << std::endl;
cout << num2 << std::endl;
cout << test << "\n" << std::endl;
cout << "Left 5 chars: \"" << l5 << "\"" << std::endl;
cout << "Right 9 chars: \"" << r9 << "\"" << std::endl;
cout << "Mid 5 chars from the position 15: \"" << m155 << "\"" << "\n\n";
string str = " 가나다라마바사";
cout << "Original: [" << str << "]" << endl;
trim(str);
cout << "Trimmed: [" << str << "]" << "\n\n";
string a="What a nice day it is? What a nice day it is? What a nice day it is?";
cout << "Original: [" << a << "\n";
cout << "Replaced: [" << ReplaceString(a, "day", "") << "\n\n";
cout << longString << "\n\n";
cout << str2 << "\n\n";
return 0;
}
'C++ 언어' 카테고리의 다른 글
| C++를 이용 JSON을 SRT 파일로 만들기 (1) | 2021.09.11 |
|---|---|
| C++에서 Vector를 사용하여 문자열 정력하기(중복테이타 제외) (0) | 2021.09.10 |
| C++에서 Vector를 사용하여 자료를 추가히고 출력하기 (0) | 2021.09.10 |
| C++에서 문자열을 정수로 변환하기 (0) | 2021.09.09 |
| C++에서 3자리 숫자 입력 받기 (0) | 2021.08.13 |
댓글