I recently found the need to replace a std::string
's contents with a substring of itself. The most logical function to call here I think is the following, from http://www.cplusplus.com/reference/string/string/assign/:
substring (2) string& assign (const string& str, size_t subpos, size_t sublen);
Copies the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is string::npos).str
Another string object, whose value is either copied or moved.subpos
Position of the first character in str that is copied to the object as a substring. If this is greater than str's length, it throws out_of_range. Note: The first character in str is denoted by a value of 0 (not 1).sublen
Length of the substring to be copied (if the string is shorter, as many characters as possible are copied). A value of string::npos indicates all characters until the end of str.
However, I'm not certain if this is permissible, or if it can corrupt the string data. I know that memcpy()
, for example, does not allow (or at least does not guarantee non-corruption in the case of) overwriting an area of memory with (a portion of) itself (see memcpy() vs memmove()). But I don't know if the above method has the same limitation.
More generally, can you please comment if I should have been able to figure out the answer to this question myself? There's nothing in the documentation I linked to that makes it clear to me what the answer to this question is, except perhaps the qualifier "Another" in the description of the str
parameter ("Another string object"), which seems to imply it cannot be the this object, although I don't find that to be unequivocal. Is that a weakness in the documentation?
Copying one string to another - strcpystrcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied. The destination character array is the first parameter to strcpy .
The function will return a substring from the character of the pos index (inclusive) up to the character with the ( pos + len - 1) index. The original string will not be changed.
Replace substring with another substring C++ It replaces the portion of the string that begins at character pos and spans len characters. The structure of the replace function is like below: string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
You can't assign strings. But you can call functions to help achieve what you want. Also have a look at the strncat() and memcpy() functions.
No.
This operation is defined by [string::assign]/4:
basic_string& assign(const basic_string& str, size_type pos, size_type n = npos);
Effects: Determines the effective length
rlen
of the string to assign as the smaller ofn
andstr.size() - pos
and callsassign(str.data() + pos rlen)
.
(dat typo)
Then:
basic_string& assign(const charT* s, size_type n);
Effects: Replaces the string controlled by
*this
with a string of lengthn
whose elements are a copy of those pointed to bys
.
Nothing about this says anything about whether str.assign(str, 0)
is at all safe (in particular, we have no way of knowing when the copy of each character will occur!).
Therefore I strongly suggest you avoid doing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With