有没有办法从自定义字符串中创建一个参数数组? [英] there's way to make an argument array from custom string?

查看:42
本文介绍了有没有办法从自定义字符串中创建一个参数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很抱歉我的英语不好。



有没有办法解析字符串中的参数?

我将解释我的问题以展示一个例子。



First of all, I'm very sorry for my bad English.

Is there's any way to parse arguments from string?
I'll explain my question to showing an example.

'String Input: This is "TEST STRING"
Array[0] = "This"
Array[1] = "is"
Array[2] = "TEST STRING"

'String Input: Another Test String, Separate Strings with "space character"
Array[0] = "Another"
Array[1] = "Test"
Array[2] = "String,"
Array[3] = "Separate"
Array[4] = "Strings"
Array[5] = "with"
Array[6] = "space character"

'String Input: "This " is "also another" "test " "string  " "!"
Array[0] = "This "
Array[1] = "is"
Array[2] = "also another"
Array[3] = "test "
Array[4] = "string  "
array[5] = "!"





如果您知道这一点,请告诉我

我很抱歉我的英语不好。谢谢!



If you know this, please let me know
I'm sorry again for my bad english. Thanks!

推荐答案

Dim myString As String = "Another Test String, Separate Strings with ""space character"""

'Everything here should be on the same line.
Dim result = myString.Split(""""c).[Select](Function(element, index) If(index Mod 2 = 0, element.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries), New String() {element})).SelectMany(Function(element) element).ToList()





如果您仍想保留报价,请使用此选项。

你必须导入System.Text.RegularExpressions





Or use this if you still want to keep the quotes.
You will have to Import System.Text.RegularExpressions

'Everthing goes on the same line.
Dim parts = Regex.Matches(myString, "[\""].+?[\""]|[^ ]+").Cast(Of Match)().[Select](Function(m) m.Value).ToList()


下面是相同的示例。



Below is sample for the same.

string input = "one \"two two\" three \"four four\" five six";
          var result = input.Split('"')
                   .Select((element, index) => index % 2 == 0  // If even index
                                         ? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)  // Split the item
                                         : new string[] { element })  // Keep the entire item
                   .SelectMany(element => element).ToList();





您还可以查看以下链接



拆分字符串有空格的,除非它们被括在引号中? [ ^ ]


这篇关于有没有办法从自定义字符串中创建一个参数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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