解析已知格式字符串的最有效方法

时间:2022-10-29 20:57:10

I have a string which is in the following format for example:

我有一个字符串,例如以下格式:

[ "{0}", "{1}", "{2}" ]

So I know there are always 3 parameters (of variable length) in that string format.

所以我知道该字符串格式总共有3个参数(可变长度)。

What is the most efficient way of parsing the string? (And maybe the shortest code along with it so I have to run some tests)

解析字符串的最有效方法是什么? (也许是最短的代码,所以我必须运行一些测试)

Thanks.

3 个解决方案

#1


0  

1st solution: I think this is simplest - Cut [ and ] (1st and last character) - Split by "," -> trim() -> get 3 parts - Remove redundant characters (like "{ and }") If you're sure that your expected strings don't contain " { } -> You can Remove them before split.

第一个解决方案:我认为这是最简单的 - 剪切[和](第一个和最后一个字符) - 按“,” - >修剪() - >获取3个部分 - 删除多余的字符(如“{和}”)如果你'确保您的预期字符串不包含“{} - >您可以在拆分之前删除它们。

2nd solution: Use Regex

第二个解决方案:使用正则表达式

#2


0  

It can be done in many ways, using Regex class or string methods.

它可以使用Regex类或字符串方法以多种方式完成。

Here is how it can be done using Regex.Match:

以下是使用Regex.Match完成的方法:

        string s = @"[ ""some test"", ""another test string"", ""hi there!"" ]";
        string[] vars = Regex.Matches(s, @"""([^""]*)""")
            .Cast<Match>()
            .Select(m => m.Groups[1].Value)
            .ToArray();

Another way, using Regex.Split:

另一种方法,使用Regex.Split:

        vars = Regex.Split(s.Remove(s.Length - 3, 3).Remove(0, 3), @""",\s""");

Here is one way of doing it with string methods:

以下是使用字符串方法执行此操作的一种方法:

        vars = s.Substring(s.IndexOf("\"") + 1, s.LastIndexOf("\"") - 3)
                    .Split(new string[] {@""", """}, StringSplitOptions.None);

#3


0  

Example: Extract --> How Are You?

示例:提取 - >你好吗?

string stuff = @"""{How}"", ""{Are}"", ""{You?}""";
string[] answer = (stuff.Replace(@"""{", String.Empty).Replace(@"}""", String.Empty)).Split(',');

Now the variable answer holds the three words!

现在变量答案包含三个字!

#1


0  

1st solution: I think this is simplest - Cut [ and ] (1st and last character) - Split by "," -> trim() -> get 3 parts - Remove redundant characters (like "{ and }") If you're sure that your expected strings don't contain " { } -> You can Remove them before split.

第一个解决方案:我认为这是最简单的 - 剪切[和](第一个和最后一个字符) - 按“,” - >修剪() - >获取3个部分 - 删除多余的字符(如“{和}”)如果你'确保您的预期字符串不包含“{} - >您可以在拆分之前删除它们。

2nd solution: Use Regex

第二个解决方案:使用正则表达式

#2


0  

It can be done in many ways, using Regex class or string methods.

它可以使用Regex类或字符串方法以多种方式完成。

Here is how it can be done using Regex.Match:

以下是使用Regex.Match完成的方法:

        string s = @"[ ""some test"", ""another test string"", ""hi there!"" ]";
        string[] vars = Regex.Matches(s, @"""([^""]*)""")
            .Cast<Match>()
            .Select(m => m.Groups[1].Value)
            .ToArray();

Another way, using Regex.Split:

另一种方法,使用Regex.Split:

        vars = Regex.Split(s.Remove(s.Length - 3, 3).Remove(0, 3), @""",\s""");

Here is one way of doing it with string methods:

以下是使用字符串方法执行此操作的一种方法:

        vars = s.Substring(s.IndexOf("\"") + 1, s.LastIndexOf("\"") - 3)
                    .Split(new string[] {@""", """}, StringSplitOptions.None);

#3


0  

Example: Extract --> How Are You?

示例:提取 - >你好吗?

string stuff = @"""{How}"", ""{Are}"", ""{You?}""";
string[] answer = (stuff.Replace(@"""{", String.Empty).Replace(@"}""", String.Empty)).Split(',');

Now the variable answer holds the three words!

现在变量答案包含三个字!