문자열을 가지고 HelloHello
elloHello
을 갖는 첫 번째 문자를 어떻게 추출 할 수 있습니까? 내가 .at()
과 string[n]
생각했습니다문자열에서 문자 추출
는 있지만
elloHello
문자열을 가지고 HelloHello
elloHello
을 갖는 첫 번째 문자를 어떻게 추출 할 수 있습니까? 내가 .at()
과 string[n]
생각했습니다문자열에서 문자 추출
는 있지만
elloHello
#include <iostream>
#include <string>
int main(int,char**)
{
std::string x = "HelloHello";
x.erase(x.begin());
std::cout << x << "\n";
return 0;
}
인쇄 값을 반환하고, 문자열에서 삭제되지 않습니다 참조 : http://www.cplusplus.com/reference/string/string/substr/
이 연산자는 무엇입니까 ::? –
@SaraKholusi : 그것은 범위 분석 연산자입니다. –
SUBSTR()
를 사용해보십시오
std::string str ("HelloHello");
str.erase (0,1); // Removes 1 characters starting at 0.
// ... or
str.erase(str.begin());
하위 문자열을 사용해야합니다. 첫 번째 매개 변수는 시작 위치를 나타냅니다. 두 번째 매개 변수 string::npos
은 새 문자열에 지정된 시작 위치에서부터 문자열 끝까지의 모든 문자가 포함되기를 원한다는 것을 의미합니다.
std::string shorterString = hellohello.substr(1, std::string::npos);
당신의 캐릭터는 어떤 유형입니까? char * 또는 std :: string? – emsr