快捷搜索:  汽车  科技

c语言替换指定字符串:C自定义字符串全部替换函数

c语言替换指定字符串:C自定义字符串全部替换函数string& replace (size_t pos size_t len const char* s);c-string (3)substring (2)string& replace (size_t pos size_t len const string& str size_t subpos size_t sublen);

我们来看string库的成员函数replace:

string (1)

string& replace (size_t pos size_t len const string& str);

string& replace (iterator i1 iterator i2 const string& str);

substring (2)

string& replace (size_t pos size_t len const string& str

size_t subpos size_t sublen);

c-string (3)

string& replace (size_t pos size_t len const char* s);

string& replace (iterator i1 iterator i2 const char* s);

buffer (4)

string& replace (size_t pos size_t len const char* s size_t n);

string& replace (iterator i1 iterator i2 const char* s size_t n);

fill (5)

string& replace (size_t pos size_t len size_t n char c);

string& replace (iterator i1 iterator i2 size_t n char c);

range (6)

template <class InputIterator>

string& replace (iterator i1 iterator i2

InputIterator first InputIterator last);

Replace portion of string

Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the range between [i1 i2)) by new contents:
pos:指需要被替换的目标string需要被替换字符的起始位置。Position of the first character to be replaced. If this is greater than the string length it throws out_of_range.

len:指需要被替换的目标string需要被替换字符的数量。Number of characters to replace (if the string is shorter as many characters as possible are replaced). A value of string::npos indicates all characters until the end of the string.

std::string::npos static const size_t npos = -1; Maximum value for size_t

(1) string Copies str.

(2) substring 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).

(3) c-string Copies the null-terminated character sequence (C-string) pointed by s.

(4) buffer Copies the first n characters from the array of characters pointed by s.

(5) fill Replaces the portion of the string by n consecutive copies of character c.

(6) range Copies the sequence of characters in the range [first last) in the same order.

(7) initializer list Copies each of the characters in il in the same order.

demo:

#include <iostream> #include <string> int main () { std::string base="this is a test string."; std::string str2="n example"; std::string str3="sample phrase"; std::string str4="useful."; // replace signatures used in the same order as described above: // Using positions: 0123456789*123456789*12345 std::string str=base; // "this is a test string." str.replace(9 5 str2); // "this is an example string." (1) str.replace(19 6 str3 7 6); // "this is an example phrase." (2) str.replace(8 10 "just a"); // "this is just a phrase." (3) str.replace(8 6 "a shorty" 7); // "this is a short phrase." (4) str.replace(22 1 3 '!'); // "this is a short phrase!!!" (5) // Using iterators: 0123456789*123456789* str.replace(str.begin() str.end()-3 str3); // "sample phrase!!!" (1) str.replace(str.begin() str.begin() 6 "replace"); // "replace phrase!!!" (3) str.replace(str.begin() 8 str.begin() 14 "is coolness" 7); // "replace is cool!!!" (4) str.replace(str.begin() 12 str.end()-4 4 'o'); // "replace is cooool!!!" (5) str.replace(str.begin() 11 str.end() str4.begin() str4.end());// "replace is useful." (6) std::cout << str << '\n'; return 0; }

我们知道,ms office word、Excel的替换操作,在选择需要做替换操作的内容文本后,可以全部替换或逐个替换:

c语言替换指定字符串:C自定义字符串全部替换函数(1)

string replace显然没有提供如下接口的类似操作:

void replaceAll(string &src const string &olds const string &news)

显然要自定义replaceAll函数需要使用string find()不断更新string replace()的pos。

了解一下string find成员函数:

string (1)

size_t find (const string& str size_t pos = 0) const;

c-string (2)

size_t find (const char* s size_t pos = 0) const;

buffer (3)

size_t find (const char* s size_t pos size_t n) const;

character (4)

size_t find (char c size_t pos = 0) const;

Find content in string

Searches the string for the first occurrence of the sequence specified by its arguments.
When pos is specified the search only includes characters at or after position pos ignoring any possible occurrences that include characters before pos.
Notice that unlike member find_first_of whenever more than one character is being searched for it is not enough that just one of these characters match but the entire sequence must match.

pos:开始查找的位置。Position of the first character in the string to be considered in the search.
If this is greater than the string length the function never finds matches.
Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.

demo:

#include <iostream> // std::cout #include <string> // std::string int main () { std::string str ("There are two needles in this haystack with needles."); std::string str2 ("needle"); // different member versions of find in the same order as above: std::size_t found = str.find(str2); if(found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; found=str.find("needles are small" found 1 6);// "needles are small"的前6个字符 if(found!=std::string::npos) std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack"); if(found!=std::string::npos) std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.'); if(found!=std::string::npos) std::cout << "Period found at: " << found << '\n'; // let's replace the first needle: str.replace(str.find(str2) str2.length() "preposition"); std::cout << str << '\n'; return 0; } /*output: first 'needle' found at: 14 second 'needle' found at: 44 'haystack' also found at: 30 Period found at: 51 There are two prepositions in this haystack with needles. */

replaceAll()实现:

#include <iostream> using namespace std; void replaceAll(string &src const string &olds const string &news) { unsigned int pos = src.find(olds); while(pos != string::npos) { src.replace(pos olds.length() news); pos = src.find(olds pos 1); } } int main() { string src = "When pos is specified the search only includes characters at or "; src = "after position pos ignoring any possible occurrences that include characters before pos."; string olds = "pos"; string news = "POS"; replaceAll(src olds news); cout<<src.c_str()<<endl; getchar(); return 0; }

ref:

http://www.cplusplus.com/reference/string/string/find/

http://www.cplusplus.com/reference/string/string/

猜您喜欢: