我有一个看起来像这样的字符串:
sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi sdofi osdi foi sdofd oi b123 sdfoijsfdoifsdiosfdoifsoifsdofjssfdoji
如何从a123到b123提取所有内容?
答案 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)