如何用空格和引号连接c#中的两个字符串 [英] how to concatenate two strings in c# with space and quotation

查看:75
本文介绍了如何用空格和引号连接c#中的两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串





string strEXEPath =" C:\\Files \\FrameWindows.exe ;

string strHTMLFile = @OutputFolder +" \\" + htmlName +" .html;



它应该像



C:\ Files \\ \\ FrameWindows.exeC:\Output \UK_Skype Technologies SA_Skype 5.10_5.10.116.html



提前致谢

i have two strings


string strEXEPath = "C:\\Files\\FrameWindows.exe;
string strHTMLFile = @OutputFolder + "\\" + htmlName + ".html;

it should come like

C:\Files\FrameWindows.exe "C:\Output\UK_Skype Technologies S.A._Skype 5.10_5.10.116.html"

Thanks in advance

推荐答案

string strEXEPath = "C:\\Files\\FrameWindows.exe";
            string @OutputFolder=@"C:\Output\UK_Skype Technologies S.A._Skype";
            string htmlName="5.10_5.10.116";
            string strHTMLFile = strEXEPath + " \"" + @OutputFolder + "\\" + htmlName + ".html" + "\"";


我会使用String.Format。

例如:
I would use String.Format.
For example :
var strHTMLFile = System.IO.Path.Combine(@OutputFolder, htmlName, ".html");
var resultString = string.Format("\"{0}\" \"{1}\"", strEXEPath, strHTMLFile);
Console.WriteLine(resultString);

给出结果

"C:\Files\FrameWindows.exe" "C:\Output\UK_Skype Technologies S.A._Skype 5.10_5.10.116\.html"



请注意,此处不需要包含可执行文件的双引号,但保留它们是无害的,它可以保护代码免受空格的影响在可执行文件路径的文件夹名称中。



另请注意使用 Path.Combine [ ^ ]创建路径


另一个想法是使用string.join:



Another idea would be to use string.join:

string strEXEPath = @"C:\Files\FrameWindows.exe";
string strHTMLFile = "\"C:\\Output\\UK_Skype Technologies S.A._Skype 5.10_5.10.116.html\"";

string result = string.Join(" ", strEXEPath, strHTMLFile);


这篇关于如何用空格和引号连接c#中的两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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