I have a string with quotes around the path as follows:
我在路径周围有一个带引号的字符串,如下所示:
"C:\Program Files (x86)\Windows Media Player\wmplayer.exe" arg1 arg2
“C:\ Program Files(x86)\ Windows Media Player \ wmplayer.exe”arg1 arg2
If I use Text.Split(new Char[] { ' ' }, 2);
then I get the first space.
如果我使用Text.Split(new Char [] {''},2);然后我得到了第一个空间。
How to get the path and args ?
如何获得路径和args?
3 个解决方案
#1
Try splitting on the double quotes (Text.Split(new Char[] { '/"' }, 3);) then taking the last string in that array and splitting again on the space.
尝试拆分双引号(Text.Split(new Char [] {'/“'},3);)然后取出该数组中的最后一个字符串并再次在空格上拆分。
string[] pathAndArgs = Text.Split(new Char[] { '/"' }, 3);
string[] args = pathAndArgs[2].Split(new Char[] { ' ' }, 2);
I may have a syntax error in there, but you get what I mean.
我可能有一个语法错误,但你明白我的意思。
#2
Use a regular expression like: ("".*?"")|(\S+)
使用正则表达式,如:(“”。*?“”)|(\ S +)
So your code would be something like:
所以你的代码将是这样的:
Regex r = new Regex(@"("".*?"")|(\S+)");
MatchCollection mc = r.Matches(input);
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Value);
}
#3
Do text.split and work your way back from the end of the array.
做text.split并从数组的末尾开始工作。
var input = "C:\\blah\\win.exe args1 args2";
var array = input.split(' ');
var arg1 = array[array.length -2];
var arg2 = array[array.length -1];
#1
Try splitting on the double quotes (Text.Split(new Char[] { '/"' }, 3);) then taking the last string in that array and splitting again on the space.
尝试拆分双引号(Text.Split(new Char [] {'/“'},3);)然后取出该数组中的最后一个字符串并再次在空格上拆分。
string[] pathAndArgs = Text.Split(new Char[] { '/"' }, 3);
string[] args = pathAndArgs[2].Split(new Char[] { ' ' }, 2);
I may have a syntax error in there, but you get what I mean.
我可能有一个语法错误,但你明白我的意思。
#2
Use a regular expression like: ("".*?"")|(\S+)
使用正则表达式,如:(“”。*?“”)|(\ S +)
So your code would be something like:
所以你的代码将是这样的:
Regex r = new Regex(@"("".*?"")|(\S+)");
MatchCollection mc = r.Matches(input);
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Value);
}
#3
Do text.split and work your way back from the end of the array.
做text.split并从数组的末尾开始工作。
var input = "C:\\blah\\win.exe args1 args2";
var array = input.split(' ');
var arg1 = array[array.length -2];
var arg2 = array[array.length -1];