gpt4 book ai didi

vb.net - 从字符串中检索重复出现的字符

转载 作者:行者123 更新时间:2023-12-02 08:05:44 26 4
gpt4 key购买 nike

我需要从长字符串行中获取某个出现多次的字符。该字符串如下所示:

<Press T><Press Left><Press A><Press C><Press P><Press U><Press G><Press P><Press NumPad7><Press NumPad7><Press A>

我需要循环遍历每组 <>'s并获取每次出现单词 Press 之后的信息。所以在这种情况下我需要信息 T, Left, A, C, P, etc

最佳答案

我认为这种纯String方法是最有效的,但它要求格式严格:

Dim text = "<Press T><Press Left><Press A><Press C><Press P><Press U><Press G><Press P><Press NumPad7><Press NumPad7><Press A>"
Dim allKeys As New List(Of String)
Dim pattern = "Press "
Dim index = text.IndexOf(pattern)
While index >= 0
index += pattern.Length
Dim endIndex = text.IndexOf(">", index)
If endIndex >= 0 Then
Dim nextKey = text.Substring(index, endIndex - index)
allKeys.Add(nextKey)
index = text.IndexOf(pattern, endIndex + 1)
Else
Exit While
End If
End While

Console.Write(String.Join(", ", allKeys))

输出:T、左、A、C、P、U、G、P、NumPad7、NumPad7、A

<小时/>

这是Regex返回所有匹配项,您会在第二组中找到按下的“键”:

pattern = "<Press ([^>]+)>"
Dim regex = New Regex( pattern, RegexOptions.Compiled And RegexOptions.IgnoreCase)
For Each match As Match In regex.Matches(text)
Console.WriteLine(match.Groups(1))
Next

关于vb.net - 从字符串中检索重复出现的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17449083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com