Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

GPX

How can i remove part of a string ? (visual c++)

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m sure it''s really easy, but i''m looking for the best way of doing it (maybe a string function that i''ve missed or something) I want to remove a part of a string, i know it appears only once in the string : i.e : char str[100]="hello\r\nbob\r\nwassup\r\nlalalala"; now i want to remove the "bob\r\n", to end up with : hello\r\nwassup\r\nlalalala What''s the best way of doing this ? Thanks in advance ...

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by superdeveloper
Sorry I don''t have the answer, but this would make a good programming question...



Actually i know how to do that in another programming language, in pascal, using a stack , but i want a function to do that for me, in a more efficient way probably ..

about the std->string->replace, can u show a simple code example plz ? i''m noob to c++ :\

Share this post


Link to post
Share on other sites
Doing it the old-fashioned/C way:
char * remove( char * str, int from, int to )
{
int offset = to - from;
for( int n = from; n < to; ++n )
str[n] = str[n + offset];
str[to] = ''\0'';
return str;
}
Note that the string effectively gets mangled in-place: removing "bob\r\n" from "hello\r\nbob\r\nwassup\r\nlalalala" with this function yields "hello\r\nwassup\r\nlalala\0alala". The last five characters will not be printed by C string functions because of the insertion of a null terminal.

Share this post


Link to post
Share on other sites
quote:
about the std->string->replace, can u show a simple code example plz ? i''m noob to c++ :\
First off, it''s not str->string->replace (which indicates accessing members of pointers).
#include <iostream>
#include <string>
 
int main()
{
using namespace std;
string word, str = "hello\r\nbob\r\nwassup\r\nlalalala";
 
cout << "Enter the word/phrase to remove: ";
cin >> word;
int pos = str.find(word);
if( pos == string::npos )
{
cerr << "\''" << word << "\'' is not a substring of the text."
<< "\nExiting..." << endl;
return 0;
}
 
str.replace( pos, word.size(), "" );
cout << "The edited text is \''" << str << "\''. Bye!" << endl;
return 0;
}

Share this post


Link to post
Share on other sites
I not sure about a best way, but here is one way(C style).


char str[100]="hello\r\nbob\r\nwassup\r\nlalalala";
char* toremove= "bob";


int len = strlen(toremove);
char* pRemove = strstr(str,toremove);

if(NULL != pRemove)
strcpy(pRemove,(pRemove+len));

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
quote:
about the std->string->replace, can u show a simple code example plz ? i''m noob to c++ :\
First off, it''s not str->string->replace (which indicates accessing members of pointers).
#include <iostream>
#include <string>
 
int main()
{
using namespace std;
string word, str = "hello\r\nbob\r\nwassup\r\nlalalala";
 
cout << "Enter the word/phrase to remove: ";
cin >> word;
int pos = str.find(word);
if( pos == string::npos )
{
cerr << "\''" << word << "\'' is not a substring of the text."
<< "\nExiting..." << endl;
return 0;
}
 
str.replace( pos, word.size(), "" );
cout << "The edited text is \''" << str << "\''. Bye!" << endl;
return 0;
}



Thanks a lot !
I''ve learned a bit about the string thing after reading ur code, and it''s so cool compared to c char arrays :O
Ofcourse my original problem is solved
thanks all

Share this post


Link to post
Share on other sites
quote:
Original post by Jack Sotac
I not sure about a best way, but here is one way(C style).


char str[100]="hello\r\nbob\r\nwassup\r\nlalalala";
char* toremove= "bob";


int len = strlen(toremove);
char* pRemove = strstr(str,toremove);

if(NULL != pRemove)
strcpy(pRemove,(pRemove+len));




Thats not such a good idea - strcpy won''t nessicarily work - it requires that the memory does not overlap (like memcpy). You''d be better using memmove().

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!