替换为"\"将字符串转换为"\\"在WinForm C ++/CLI下 [英] Replace "\" of a string into "\\" under WinForm C++/CLI

查看:94
本文介绍了替换为"\"将字符串转换为"\\"在WinForm C ++/CLI下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我最近有问题.我的编是在C ++/CLI WinForm App下.我注意到这里使用文件路径时,它们应类似于:

Hi mates,

I have a problem recently. My prog is under C++/CLI WinForm App. I noticed that here when file path are used, they should be like:

"C:\\folder1\\underfolder\\file.JPG"


不像C#和VB:


unlike C# and VB:

C#:   @"C:\folder1\underfolder\file.JPG"

VB:   "C:\folder1\underfolder\file.JPG"



区别在于"\"和"\\"之间.
这意味着,当您使用"C:\ folder1 \ ..."来调用或写入文件时,在C ++/CLI中是不可接受的.
我使用folderbrowserdialog来获取folderpath,它以
的形式获取路径 "C:\ folder \ ...":



The difference is between "\" and "\\".
That means, when you use "C:\folder1\..." to call or write a file, it''s not acceptable in C++/CLI.
I used folderbrowserdialog to get the folderpath, which gets paths in form of
"C:\folder\...":

folderBrowserDialog1->ShowDialog();
String^ path = folderBrowserDialog1->SelectedPath;
\\ path = path->Replace("\", "\\"); 
textBox1->Text = path;



然后,我想将"\"替换为路径的"\\".我使用的方法是String.Replace().但是有问题.必须以两种方式使用Replace():
1. Replace(''a'',``b'');
2. Replace("docment","document");

但是将"\"替换为"\\"是行不通的.我真的很困惑.
你能告诉我如何解决吗?也许还有其他方法?还是我错了?
在c ++/cli中,路径必须与"\\"一起使用是否是一个普遍的问题?

在此先感谢您,万圣节快乐!



And then I want to replace the "\" into "\\" of the paths. The method I used is String.Replace(). But there is problem. The Replace() must be used in two ways:
1. Replace(''a'', ''b'');
2. Replace("docment", "document");

But the replace of "\" to "\\" is not going to work. I am really confused.
Could you tell me how to solve it? Maybe there is other ways? Or was I wrong?
Is it a general problem in c++/cli that the paths must be used with "\\"?

Thanks in advance and happy Halloween!

推荐答案

通过重复反斜杠来转义:Replace("\\", "\\\\");

请参阅 http://msdn.microsoft.com/en-us/library/h21280bw.aspx [ ^ ]
Escape the backslashes by repeating them: Replace("\\", "\\\\");

See http://msdn.microsoft.com/en-us/library/h21280bw.aspx[^]


这篇关于替换为"\"将字符串转换为"\\"在WinForm C ++/CLI下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆