从中提取变量值;分开的字符串

时间:2015-07-28 02:01:35

标签: asp.net regex vb.net string

我的ASP.NET(VB.NET)应用程序接受一串由;字符分隔的变量,如下所示:q=castle;themes=3,4,11;price=1,450;

从此字符串中提取特定变量的最佳性能是什么,例如:如果我想拥有变量themes的值,则必须返回字符串3,4,11

我该怎么做?也许通过正则表达式,如果是这样的话?还是自定义功能?

2 个答案:

答案 0 :(得分:0)

这是从字符串中提取值的一种相对简单的方法。

Dim str As String = "q=castle;themes=3,4,11;price=1,450;"
Dim themes As String = ""
For Each item As String In str.Split({";"c})
    Dim parts() As String = item.Split("="c)
    If parts.Length = 2 AndAlso parts(0).Trim = "themes" Then
        themes = parts(1).Trim
    End If
Next

答案 1 :(得分:0)

尝试使用Dictionary和Linq

    Dim inputString As String = "q=castle;themes=3,4,11;price=1,450;"

    Dim resultDictionary = inputString.Split(";"C).ToList().Where(Function(x) x.Split("="C).Length > 1).[Select](Function(x) New With { _
    Key .x = x.Split("="C)(0), _
    Key .y = x.Split("="C)(1) _
}).ToDictionary(Function(key) key.x, Function(value) value.y)


    Dim themesValue = resultDictionary("themes")