none
std::wstring replace 问题 RRS feed

  • 问题

  • std::wstring a = "\"abcd\"";

    我想替换 \" 为空,怎样写?

    这样写 a.replace(L"\"",L""), 无法通过编译,提示参数错误,

    2014年5月31日 6:09

答案

  • // try this please :
    
    void ReplaceString(std::wstring& str, const std::wstring& src, const std::wstring& des)
    {
    	int pos = -1;
    	int curPos = 0;
    	while (-1 != (pos = str.find(src, curPos)))
    	{
    		str.replace(pos, src.size(), des);
    		curPos = pos + des.size();
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::wstring a = L"\"abcd\"";
    	ReplaceString(a, L"\"", L"");
    	wcout << a.c_str() << endl;
    	return 0;
    }
    
    


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2014年6月2日 8:10
    版主

全部回复

  • 你好,我把问题移到C++论坛,因为问题本身跟平台没有什么关系,是C++语言的使用,多谢理解。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月2日 6:11
  • // try this please :
    
    void ReplaceString(std::wstring& str, const std::wstring& src, const std::wstring& des)
    {
    	int pos = -1;
    	int curPos = 0;
    	while (-1 != (pos = str.find(src, curPos)))
    	{
    		str.replace(pos, src.size(), des);
    		curPos = pos + des.size();
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::wstring a = L"\"abcd\"";
    	ReplaceString(a, L"\"", L"");
    	wcout << a.c_str() << endl;
    	return 0;
    }
    
    


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2014年6月2日 8:10
    版主