【问题标题】:vb.net : split stringvb.net:拆分字符串
【发布时间】:2011-10-22 22:50:06
【问题描述】:

我有一个包含如下内容的字符串:

你好,这是一个{测试,示例}

你好,这是一个{test, example}其他文本

你好,这是一个 {test, example} 一些其他文本 { test1, example1 } 等等等等等等

这就是我想要的:

创建一个包含所有普通文本和{}之间的所有文本的列表

例如:

  • 你好,这是一个
  • 测试,示例

  • 你好,这是一个
  • 测试,示例
  • 其他一些文字

依此类推(就像字符串的第一个示例一样。

我怎样才能做到最好?

【问题讨论】:

  • 您希望所有普通文本和 '} 之间的所有文本在两个单独的列表中吗?
  • 不,它们必须在同一个列表中。

标签: vb.net


【解决方案1】:

试试这个,它使用 RegEx 而不是 Split 函数 -

Dim value As String = "hello this is an {test, example} some other text { test1, example1 } etc etc etc"
Dim words As List(Of String) = new List(Of String) 

' Get a collection of matches.
Dim matches As MatchCollection = Regex.Matches(value, "[^\{\}]+")

For Each match As Match In matches
    words.Add(match.Captures(0).Value.Trim)
Next

words 列表现在应该包含所需的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多