vb.net:从“a123”到“b123”提取字符串

时间:2009-10-28 18:15:02

标签: vb.net

我有一个看起来像这样的字符串:

sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi sdofi osdi foi sdofd oi b123 sdfoijsfdoifsdiosfdoifsoifsdofjssfdoji

如何从a123到b123提取所有内容?

2 个答案:

答案 0 :(得分:6)

如果您的模式将像a123一样简单,那么简单的字符串操作就足够了:

Dim s As String = "sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi sdofi osdi foi sdofd oi b123 sdfoijsfdoifsdiosfdoifsoifsdofjssfdoji"

Dim startIndex As Integer = s.IndexOf("a123") + "a123".Length
Dim endIndex As Integer = s.IndexOf("b123")

Dim result = s.Substring(startIndex, endIndex - startIndex)

答案 1 :(得分:1)

您可以使用文本解析功能查找令牌的开始和结束位置。

例如:

Dim s As String = "sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi sdofi osdi foi sdofd oi b123 sdfoijsfdoifsdiosfdoifsoifsdofjssfdoji"
Dim startToken As String = "a123"
Dim endToken As String = "b123"
Dim startTokenPosition As Integer = s.IndexOf(startToken)
Dim endTokenPosition As Integer = s.IndexOf(endToken)
'Validate both startToken and endToken were found...
Dim mysubstring As String = s.Substring(startTokenPosition + startToken.Length, endTokenPosition - startTokenPosition - startToken.Length)