Can anyone help with following? Suppose I have a QString with a filepath stored of a certain file, I want to replace the /
(slashes) from it with \\
(double backslashes) I tried:
mystring.replace("/","\\");
But it only puts a single \
instead of \\
String before replacement: D:/myfiles/abc.zip
String after replacement: D:\myfiles\abc.zip
Expected string: D:\\myfiles\\abc.zip
You need to use:
mystring.replace("/","\\\\");
The compiler uses \
as an escape character in strings (for things like \t
, \n
or \r
) so that \\
is actually turned into \
. If you need two backslashes, you need to start with four.
If you want to convert paths to Windows format, you could simply use QDir::toNativeSeparators():
qDebug() << QDir::toNativeSeparators("c:/windows/path"); // Prints "c:\windows\path"
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