VB'的等价物是什么;s替换C#中的(str1、str2、str3)?
在VB和C#中,我知道当我们替换字符串时,我们使用:VB'的等价物是什么;s替换C#中的(str1、str2、str3)?,c#,vb.net,file,C#,Vb.net,File,在VB和C#中,我知道当我们替换字符串时,我们使用:FILE.replace,并且我们不会将语句赋给变量。例如,File.Replace(text1,text2,text1) 然而,我有一个VB代码,我想使用它并将其转换成C代码,我对如何使用Replace感到非常困惑 VB代码工作正常 下面是VB代码: Dim pdfTemplate As String = Path.Combine(Application.StartupPath, "PDFs\2017-I9.pdf") pdf
FILE.replace
,并且我们不会将语句赋给变量。例如,File.Replace(text1,text2,text1)代码>
然而,我有一个VB代码,我想使用它并将其转换成C代码,我对如何使用Replace
感到非常困惑
VB代码工作正常
下面是VB代码:
Dim pdfTemplate As String = Path.Combine(Application.StartupPath, "PDFs\2017-I9.pdf")
pdfTemplate = Replace(pdfTemplate, "bin\Debug\", "")
Dim fields = New Dictionary(Of String, String)() From {
{"textFieldLastNameGlobal", Me.tbLast.Text},
{"textFieldFirstNameGlobal", Me.tbFirst.Text},
{"textFieldMiddleInitialGlobal", Mid(Me.tbMiddle.Text, 1, 1)},
{"textFieldOtherNames", Me.tbOtherName.Text}
}
行pdfTemplate=Replace(pdfTemplate,“bin\Debug\”,“”)让我感到困惑,我无法将其转换为C
顺便说一下,VB代码是在这里编写和发布的,但我忘了是谁。尽管我没有提到作者,但我还是想表扬他/她
下面是我用C编写类似代码的尝试#
pdfTemplate=HostingEnvironment.MapPath(“~/Content/PDFs/2017-I9.pdf”);
pdfTemplate=File.Replace(pdfTemplate,“bin\\Debug\\”,“”);
var fields=newdictionary()
{
{“textFieldLastNameGlobal”,EL.LastName},
{“textFieldMiddleInitialGlobal”,EL.MiddleName},
{“textFieldFirstNameGlobal”,EL.FirstName},
{“textFieldOtherNames”,EL.OtherLastName}
};
我在pdfTemplate=File.Replace(pdfTemplate,“bin\Debug\,”)上出错相当于VB替换(str1,str2,str3)
的C代码是str1.Replace(str2,str3)
所以这一行:
pdfTemplate = Replace(pdfTemplate, "bin\Debug\", "")
应该是这样的:
pdfTemplate = pdfTemplate.Replace("bin\Debug\", "")
虽然这也不正确,因为在C#中,字符串中的\
字符转义下一个字符,在这种情况下,C#编译器会抱怨无法识别的转义序列,以及常量字符串中的换行符,因为首先它不理解\D
的意思,然后是\“
转义引号,这意味着它不会结束字符串
您可以将反斜杠加倍,或者在字符串前面放置一个@
,这样以下任一项都可以工作:
pdfTemplate = pdfTemplate.Replace(@"bin\Debug\", "");
pdfTemplate = pdfTemplate.Replace("bin\\Debug\\", "");
请注意,我还在语句末尾添加了分号
警告之词:对于路径操作,通常最好使用.NET中的类,因为该类旨在正确处理路径异常
例如,如果您碰巧有一个名为Trashbin\Debug\File
的文件,那么您将得到TrashFile
,这是不正确的
您可以这样做:
string templateFolder = Path.GetFullPath(Application.StartupPath);
if (templateFolder.EndsWith(@"\\bin\\debug", StringComparison.InvariantCultureIgnoreCase))
templateFolder = Path.GetFullPath(Path.Combine(templateFolder, "..", ".."));
string templateFile = Path.Combine(templateFolder, "PDFs", "2017-I9.pdf");
如果需要在Windows以外的其他操作系统上运行代码,则应使用Path
类来查找Debug
和bin
,并且其他操作系统可能使用正斜杠或冒号作为路径分隔符
请注意,它执行完全不同的操作,它将替换文件,而不是字符串的内容。pdfTemplate=pdfTemplate.Replace(@“bin\Debug\”,“”);注意字符串前面的@以转义\character@Steve,效果非常好。谢谢大家!@他正在使用File.Replace,这与用另一个字符串替换字符串的一部分无关part@Steve哎呀!我的错!缺少文件。
部分:)并知道VB中的替换是字符串替换,而文件.Replace
是文件替换,您需要C#中的字符串.Replace
。您在问题中关于您所知道的是导致此问题的原因的陈述,您知道在C#和VB.NET中您都使用File.Replace
替换文本,这根本不是真的。最后一点警告是,请注意string.Replace(…)
区分大小写,这意味着,如果字符串包含Bin\Debug
或Bin\Debug
,这两者在文本上相同,但在大小写上不相同,则字符串替换将失败。
string templateFolder = Path.GetFullPath(Application.StartupPath);
if (templateFolder.EndsWith(@"\\bin\\debug", StringComparison.InvariantCultureIgnoreCase))
templateFolder = Path.GetFullPath(Path.Combine(templateFolder, "..", ".."));
string templateFile = Path.Combine(templateFolder, "PDFs", "2017-I9.pdf");