1 <pre name="code" class="cpp"> 2 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 8 int main() 9 { 10 string::size_type pos=0; 11 string test="fsffsfd\\fdsfsfd\\fdsfsd"; 12 cout<<test<<endl; 13 14 while((pos=test.find('\\',pos))!=string::npos) 15 { 16 test.insert(pos,"\\");//插入 17 pos=pos+2; 18 } 19 cout<<test<<endl; 20 21 return 0; 22 }
注:
C++中,將字符'\'用內部的轉義字符‘\’來表示,所以在內部的'\'不能單獨存在,如果存在內部的字符串中,則將其視為相連字符的轉義字符,所以在內部要慎用‘\’字符。
相反,外部傳進來的字符'\',在C++內部自動轉換為對應的轉義字符'\\',開發者不需要考慮,知道怎么回事就行了。
轉自:http://blog.csdn.net/huixingshao/article/details/46910761
參考:https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx#basic_string::find_first_of