Qstring replace is not working fine

时光总嘲笑我的痴心妄想 提交于 2019-12-11 08:17:22

问题


i want to replace temp.replace

(QString("/"), QString("\"));

Here i am getting error error C2001: newline in constant
error C2275: 'QString' : illegal use of this type as an expression

How can i replace "/" with "\"


thakns 4 ur answer. But till now i didnt sort my issue. Please check my code

// Convert to a wchar_t*

size_t origsize = strlen(toChar) + 1;

const size_t newsize = 100;

size_t convertedChars = 0;

wchar_t wcstring[newsize];

mbstowcs_s(&convertedChars, wcstring, origsize, toChar, _TRUNCATE);

wcscat_s(wcstring, L"\\*.*\0");

wcout << wcstring << endl; // C:\Documents and Settings\softnotions\Desktop\Release\*.*



SHFILEOPSTRUCT sf;

memset(&sf,0,sizeof(sf));

sf.hwnd = 0;

sf.wFunc = FO_COPY;

 //sf.pFrom =wcstring;  /* when giving wcstring i am not getting answer */

  sf.pFrom = L"C:\\Documents and Settings\\softnotions\\Desktop\\Release\\*.*\0";

   wcout << sf.pFrom  <<endl;   // C:\Documents and      Settings\softnotions\Desktop\Release\*.*

Both wcstring and sf.pFrom are same then y not gettng answer when assigning sf.pFrom =wcstring;


回答1:


By escaping it with another \:

"\\"

This is a general C/C++ gotcha about how escaping works inside character strings. Since \ is used for escaping and thus has a special meaning inside a string, it should be escaped itself.


The error thrown by your compiler is that it sees the second string constant going until the end of the line, since \" precludes it from ending the string.




回答2:


The help below is for Qt4 library!

If you need to normalize file absolute path you may use for you needs toNativeSeparators function. Here is the Qt Assistant information about it:

QString QDir::toNativeSeparators ( const QString & pathName ) [static] Returns pathName with the '/' separators converted to separators that are appropriate for the underlying operating system.

On Windows, toNativeSeparators("c:/winnt/system32") returns "c:\winnt\system32".

The returned string may be the same as the argument on some operating systems, for example on Unix.

This function was introduced in Qt 4.2.

See also fromNativeSeparators() and separator().



来源:https://stackoverflow.com/questions/2537391/qstring-replace-is-not-working-fine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!