Find String help

May 2, 2013 at 11:22pm
I'm trying to use the find string to erase a '\n' from the end of a word.
I tried something along the lines of:
1
2
if (SecondSide.find('\n')!= string::npos)
     SecondSide.erase(SecondSide.find('\n'));

but it had no effect. Any suggestions on what I'm doing wrong?
Last edited on May 2, 2013 at 11:23pm
May 2, 2013 at 11:28pm
std::string.find() takes two arguments: http://www.cplusplus.com/reference/string/string/find/
May 2, 2013 at 11:47pm
I'm not exactly sure what you mean by two arguments.
The examples in that link use:
unsigned found = str.find(str2);
found=str.find("haystack");
found=str.find('.');

Aren't those using 1 argument?
May 2, 2013 at 11:52pm
I'm trying to use the final example they have on that link
1
2
 // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");

but instead of replace I want to use erase.
May 3, 2013 at 12:26am
For whatever reason I can use std::string.find() with just supplying a const literal or a string with a given value.

1
2
std::string srch {"The dog jumped over the moon"};
std::string.find{"dog"};


I'm assuming its' because the second argument(position) is default initialized to 0.

From what I understand Monkey you're trying to work with C-style string. Never really dabbled with that but the focus in this situation should be your use of erase().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string test {"The dog and the cat"};
    string srch {"dog"};

    unsigned int pos = test.find(srch);

    test.erase(pos, srch.size() + 1); //The second argument of erase tells how many chars to erase after pos.

    cout << test;

}


Also try "\n" instead of '\n'. Nevermind.
Last edited on May 3, 2013 at 12:29am
May 3, 2013 at 12:47am
So I tried
1
2
3
char EndLine = '\n';
    pos = SecondSide.find(EndLine);
    SecondSide.erase(pos, EndLine.length() + 1);


And got:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
So I removed EndLine.length() + 1 and just made it 2 and then 1, but kept getting the same error.
May 3, 2013 at 3:38am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string str = "ab*+cd*+ef*+gh*+ij*+kl*+" ;
    std::cout << str << '\n'  ;

    constexpr char asterisk = '*' ;

    // erase first asterisk
    auto pos = str.find(asterisk) ;
    if( pos != std::string::npos ) str.erase(pos,1) ;
    std::cout << str << '\n'  ;

    // erase last asterisk
    pos = str.rfind(asterisk) ;
    if( pos != std::string::npos ) str.erase(pos,1) ;
    std::cout << str << '\n'  ;

    // erase all asterisk
    while( ( pos = str.find(asterisk) ) != std::string::npos ) str.erase(pos,1) ;
    std::cout << str << '\n'  ;

    // erase all '+'
    str.erase( std::remove( str.begin(), str.end(), '+' ), str.end() ) ;
    std::cout << str << '\n'  ;
}


http://ideone.com/8UY8pv
May 3, 2013 at 4:07am
ut it had no effect. Any suggestions on what I'm doing wrong?


It did have an effect. The problem is that the string you were working with wasn't what you expected it to be.

The referenced thread: http://www.cplusplus.com/forum/beginner/100989/
Last edited on May 3, 2013 at 4:10am
Topic archived. No new replies allowed.