옐그's 코딩라이프

[c++] substr() 부분문자열 본문

c++/개념정리

[c++] substr() 부분문자열

옐그멍이 2022. 7. 25. 14:01
더보기

substr(위치, 길이);

  • string 클래스에 있음
  • 문자열의 일부를 추출할 수 있음
  • 길이가 아무리 길어도 문자열의 마지막까지만 반환함
  • 길이 입력 생략시 문자열의 마지막까지 반환
#include <iostream>
#include <string>
using namespace std;

int main(){
    string x = "yello2wgreen";
    cout << x.substr(0,3) << '\n'; //출력 yel
    cout << x.substr(7); //출력 green
}
728x90