Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » Replacing a substring in a string
Replacing a substring in a string [message #479987] Tue, 19 March 2013 02:32 Go to next message
iRANian is currently offline  iRANian
Messages: 4299
Registered: April 2011
Karma: 0
General (4 Stars)
I need to be able to replace a substring in a string with my own string. For example:

"Herp derpEderpD"

I'd like to replace every occurrence of "derp" with "winter", so the final string looks like:

"Herp winterEwinterD"

How would I go about doing this? I see std::string allows me to do this, but I haven't figured out how to do this with C without doing tedious and dangerous pointer arithmetic and the C libraries shitty string functions. I also couldn't find a way to do this with StringClass, but I might have missed something.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: Replacing a substring in a string [message #479994 is a reply to message #479987] Tue, 19 March 2013 13:46 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
There isn't a built-in method of doing this in C++ (never mind C), but it's not that hard. 5 seconds on Google gives a workable example;

http://stackoverflow.com/questions/1452501/string-replace-in-c



A more efficient solution would be to count how many instances of <search> there are, allocate a new buffer with the required memory size (1 + originalLength + (( length(replace)-(length(match) ) * nMatches)) and then memcpy the unmodified bits of string and as many instances of <replace> as required to build the new string.

Avoids doing unnecessary and costly memory allocations that would be involved in doing it iteratively as in the above example.


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Tue, 19 March 2013 13:53]

Report message to a moderator

Re: Replacing a substring in a string [message #479995 is a reply to message #479987] Tue, 19 March 2013 13:54 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4299
Registered: April 2011
Karma: 0
General (4 Stars)
iRANian wrote on Tue, 19 March 2013 02:32

How would I go about doing this? I see std::string allows me to do this, but I haven't figured out how to do this with C without doing tedious and dangerous pointer arithmetic and the C libraries shitty string functions. I also couldn't find a way to do this with StringClass, but I might have missed something.



Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: Replacing a substring in a string [message #479996 is a reply to message #479987] Tue, 19 March 2013 19:07 Go to previous messageGo to next message
Gen_Blacky is currently offline  Gen_Blacky
Messages: 3250
Registered: September 2006
Karma: 1
General (3 Stars)
string.replace is fine.

Quote:

I haven't figured out how to do this with C without doing tedious and dangerous pointer arithmetic and the C libraries shitty string functions.


Go find some better libraries.


http://s18.postimage.org/jc6qbn4k9/bricks3.png
Re: Replacing a substring in a string [message #479997 is a reply to message #479987] Wed, 20 March 2013 00:56 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
std::string::replace replaces a set of characters between indexes N and M with a different set of characters. Whilst it can be used as a substring replacement it takes a bit of finangling to make it work and it still needs an iterative approach so it will potentially make lots of unnecessary intermediate buffer reallocations if the new string is >= the length of the string being replaced.

http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Wed, 20 March 2013 00:56]

Report message to a moderator

Re: Replacing a substring in a string [message #479998 is a reply to message #479987] Wed, 20 March 2013 01:24 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4299
Registered: April 2011
Karma: 0
General (4 Stars)
Yeah that's what I'm afraid about.

@Blacky: I mean the standard C library. For some reason there's no version of strtok() that takes a substring as delimiter, just a char array of delimiters which are evaluated one by one.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: Replacing a substring in a string [message #479999 is a reply to message #479987] Wed, 20 March 2013 12:28 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Use strstr to locate each instance of the string to be replaced.

http://steamsignature.com/card/1/76561197975867233.png
Re: Replacing a substring in a string [message #480007 is a reply to message #479987] Wed, 20 March 2013 17:29 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4299
Registered: April 2011
Karma: 0
General (4 Stars)
Yes and what way is recommended to split the string in two (with the substring as delimiter and not part of either string)?

Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: Replacing a substring in a string [message #480010 is a reply to message #479987] Thu, 21 March 2013 06:14 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Depends on a few things. If you just want pointers to both strings and don't mind them staying in the same memory block as each other you can write over the first character of the strstr result with a null character and advance its position by <length of search string> to get the pointer to the second string, using the original pointer as the pointer to the first string. Repeat until there are no more matches as necessary, either acting on each bit of string one at a time or accumulating the pointers in a vector or other storage container (pre-allocate it to a decent size first to avoid reallocations mid-processing as much as possible).

That's just one way of doing it off the top of my head. It's destructive to the source string (you could copy it first if this is a problem) but it allows you to split a string into multiple substrings. This is equivilent to how strtok works.


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Thu, 21 March 2013 06:15]

Report message to a moderator

Re: Replacing a substring in a string [message #480013 is a reply to message #479987] Thu, 21 March 2013 10:29 Go to previous message
iRANian is currently offline  iRANian
Messages: 4299
Registered: April 2011
Karma: 0
General (4 Stars)
I think I could do it with the StringClass truncate left and right functions after obtaining the position of the start of the matched substring.

I just need to know now how to convert the pointer returned by strstr() into a position into the base string, I assume it should be simple pointer arithmetic?


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Previous Topic: Renhelp.net
Next Topic: C&C_Carriers *wip*
Goto Forum:
  


Current Time: Tue May 14 23:35:37 MST 2024

Total time taken to generate the page: 0.00759 seconds