stl스트링 라이브러리 find_first_not_of, find_first_of 사용해서 스트링 찾기

stl 스트링 라이브러리에서 제공하는 함수인


string str("hello, hi");    // 기본 문구


string token(",");            // 토큰


 string::size_type it1,it2;

 

 it1=str.find_first_not_of(token); //h를 가리킨다 0


 while(it1!=string::npos) //찾았으면  찾지 못하면 string::npos리턴

  it2=str.find_first_of(token,it1); //o를 가리킨다 4


  if(it2==string::npos)//,을 발견하지 못했을시 ...

  {

   it2=str.length();//단어 끝까지의 길이 리턴..

  }


  for(int i=static_cast<int>(it1);i<static_cast<int>(it2);i++) //str[0]~str[4]출력

  {

     cout<<str[i];

   }


    it1=str.find_first_not_of(token,it2); //새로 세팅 출력결과물 다음 곳으로 이동

 }


결과물 hello hi 위 함수를 반대로 하면 ,만 추출할수 도 있고 string에서 제공 안하는 trim 함수도 만들수 있다...

기타 등등...

 

이 글을 공유하기

댓글

Designed by JB FACTORY