How to trim off whitespace ??

How to trim off white space and string except string which is expected??

str ="This is an example sentence."
str1 ="an".
I just want to print "an" in given string . Given string may change everytime just i want to match it with expected string . Below is my code which is giving output but i know its not right way. Guidence would be helpful to optimize my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main () {
  std::string str= "This is an example sentence.";
  std::string str1="an";
  std::size_t found = str.find(str1);
	cout<<found<<'\n';
	int j=found-6;
  string str2 = str.substr (found,(j));

  cout<<str2<<'\n'; // output: 12

  return 0;
}
Last edited on
What are you trying to do?

str2 will contain exactly the same as str1. So why the hassle to find str2?

What whitespaces do you want to trim?
std::string str= "This is an example sentence.";
std::string str1="an";
Comparing these two string , if str1 content i.e "an" present in str, then only print "an" from str earase ramaining string and whitespace from str. This what i intend to do in my code. Am i doing it in right way???
std::regex_replace() http://en.cppreference.com/w/cpp/regex/regex_replace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <regex>
#include <string>

std::string replace_word( std::string str, std::string word, std::string replacement = " " )
{
    // zero or more white space, word boundary, word, word boundary, zero or more white space
    std::regex word_re( "\\s*\\b" + word + "\\b\\s*" ) ;
    return std::regex_replace( str, word_re, replacement ) ;
}

int main()
{
   std::string str = "I know. You know I know. I know you know I know. "
                     "We know Henry knows, and Henry knows we know. "
                     "We're a knowledgeable family." ;

   std::cout << str << '\n'
             << replace_word( str, "know" ) << '\n'
             << replace_word( str, "know(s?)" ) << '\n' // know or knows
             << replace_word( str, "know(\\w*)" ) << '\n' ; // know followed by zero or more alphanumeric characters
}

I know. You know I know. I know you know I know. We know Henry knows, and Henry knows we know. We're a knowledgeable family.
I . You I . I you I . We Henry knows, and Henry knows we . We're a knowledgeable family.
I . You I . I you I . We Henry , and Henry we . We're a knowledgeable family.
I . You I . I you I . We Henry , and Henry we . We're a family.

http://coliru.stacked-crooked.com/a/ded671ce7efc0a4e
Say,
string str="Hello world";
string str1="wo";

I want to remove white space and other string except "wo" from "Hello world".
1
2
if(str.find(str1) != std::string::npos)
  str = str1;
?
Finally i got it >>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string myStr = "http://www.cplusplus.com/forum/ost.cgi.1" ;
std::string s ="ost.cgi";
std::string::size_type len = myStr.find(s);
if(len != std::string::npos)
{
      std::string str = myStr.erase(0, len).erase(s.length());
cout<<str<<endl;
}

else
{
cout<<"not matching"<<endl;
}
return 0;
}
It is actually the same.

std::string str = myStr.erase(0, len).erase(s.length()); = str = s;

or do you see a difference?
@coder777: Yeah both are same. I agree with you. Thank you for your reply.
Topic archived. No new replies allowed.