프로그래밍/C++
string 사용법 정리
우끼우끼몽키
2023. 11. 13. 22:57
- operator[], at(), front(), back()
- size(), length(), capacity(), resize(), shrink_to_fit(), reserve(), clear(), erase(), empty()
- c_str(), substr(), replace(), compare(), copy(), find(), insert(), push_back(), pop_back()
- begin(), end()
- swap(), operator+
- string substr(size_t index = 0, size_t len = npos) const;
- 문자열을 잘라서 반환한다.
- s2 = s1.substr(); // 처음부터 끝까지
- s2 = s1.substr(3); // 3부터 끝까지..
- s2 = s1.substr(0,2); // 0부터 2개
- string& replace(size_t index, size_t len, const CharT* cstr) const;
- 문자열을 대체한다.
- s.replace(0, 2, "ap"); // 0부터 2개를 교체한다.
- size_type find(const CharT* s, size_type pos, size_type count) const;
- 문자열을 찾고, 인덱스를 반환한다. 찾지 못한경우 string::npos (-1)를 반환한다.
- index = s.find("ap"); //처음부터 찾는다.
- index = s.find("ap", 3) // 3부터 찾는다.
- basic_string& erase(size_type index = 0, size_type count = npos);
- 원하는 범위의 문자들을 삭제한다.
- s.erase(0, 5) // 0부터 5개 삭제한다.
- s.erase(std::find(s.begin(), s.end(), ' ')); // ' '를 삭제한다.
- basic_string& insert(size_type index = 0, const CharT* s, size_type count);
- s.insert(0, "ap"); 0 위치에 삽입