简体   繁体   English

遍历字符串的算法遇到麻烦

[英]Having trouble with an algorithm that loops through a string

I am using Visual Studio Community. 我正在使用Visual Studio社区。
Hi, I wrote a loop that loops through a string of lottery drawings.I would like to grab a full drawing and cut out the date, drawings and the Powerball numbers. 嗨,我写了一个循环遍历一系列彩票图纸的循环,我想抓完整张图纸并剪下日期,图纸和强力球号码。

Everything works well the first time through the loop, and I get 06/29/2002 04 33 35 36 45 Powerball: 22 , but the second time through the loop I get 07/03/2002 " 04 33 35 36 45 " Powerball: 22. 第一次循环运行一切正常,我得到06/29/2002 04 33 35 36 45 Powerball: 22 ,但是第二次循环获得07/03/2002“ 04 33 35 36 45”强力球: 22。

The second time through the loop it goes back to the numbers for the first drawing. 第二次遍历循环,返回到第一个图形的编号。 I know I could write 我知道我会写

Numbers(i) = Full_History.Substring(Start + 10, 16) , and Balls(i) = FullHistory.Substring(Start + 26, 14) , but that doesn't seem right. Numbers(i) = Full_History.Substring(Start + 10, 16)Balls(i) = FullHistory.Substring(Start + 26, 14) ,但这似乎不对。 I hope I explained this right. 我希望我能解释这个权利。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Dim Start As Integer = 0                                                'Create a variable for start
Dim Finish As Integer = 40                                              
For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
Full_Draw(i) = Full_History.Substring(Start, Finish)                'Store the full drawing
Dates(i) = Full_History.Substring(Start, 10)                        'Store the Date of the drawing
Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)                           'Store the 'ball' if necessary
Start += 40                                                         'Increment the start variable

If you have such specialized text you can use a regular expression to extract the data, like in this snippet: 如果您有这样的专用文本,则可以使用正则表达式提取数据,如以下代码段所示:

Dim rx As New System.Text.RegularExpressions.Regex("(\d\d\/\d\d\/\d{4})\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s+Powerball:\s(\d\d)")
Dim s = "06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22"
Dim matches = rx.Matches(s)
Dim sb As New System.Text.StringBuilder
For Each m As System.Text.RegularExpressions.Match In matches
    sb.AppendLine("Date: " & m.Groups(1).Value)
    sb.AppendLine("#1: " & m.Groups(2).Value)
    sb.AppendLine("#2: " & m.Groups(3).Value)
    sb.AppendLine("#3: " & m.Groups(4).Value)
    sb.AppendLine("#4: " & m.Groups(5).Value)
    sb.AppendLine("#5: " & m.Groups(6).Value)
    sb.AppendLine("Powerball: " & m.Groups(7).Value)
    sb.AppendLine()
Next
MessageBox.Show(sb.ToString)

Granted it is not the most elegent RegEx ever, but it should be easy enough to follow: 尽管它不是有史以来最高级的RegEx,但是它应该很容易遵循:

  • \\d stands for a single digit \\ d代表一个数字
  • / is the literal slash character /是文字斜杠字符
  • {4} is a quantifier, meaning 4 of the previous thing {4}是一个量词,表示前一事物的4
  • /s is whitespace / s是空格
  • /s+ means as much whitespace as you like / s +表示尽可能多的空格
  • () denote capturing groups, allowing the access to the values in the .Groups property of each match ()表示捕获组,允许访问每个匹配项的.Groups属性中的值

So you find all matches in a given text and can easily extract all subgroups by iterating over the matches. 因此,您可以在给定的文本中找到所有匹配项,并且可以通过对匹配项进行迭代来轻松提取所有子组。


For your general question: 对于您的一般问题:

Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)   

These lines will always crop the same part out of the source string (eg index 10 to 26). 这些行将始终在源字符串中裁剪相同的部分(例如,索引10到26)。 You need to scale the Substring start part with the Start variable as well, like 您还需要使用Start变量来缩放Substring的开始部分,例如

Numbers(i) = Full_History.Substring(Start + 10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(Start + 26, 14)   

Otherwise it is not suprising that each element ends up the same. 否则,不必惊讶每个元素的结局相同。 Or to avoid more confusion, first crop the whole thing out and then dissect this new string into its parts: 为了避免更多的混乱,请先将整个内容裁剪掉,然后将这个新字符串分解成各个部分:

For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
    Dim ThisDraw As String = Full_History.Substring(Start, Finish)
    Full_Draw(i) = ThisDraw
    'Notice the fixed indizes now
    Dates(i) = ThisDraw.Substring(0, 10)                        'Store the Date of the drawing
    Numbers(i) = ThisDraw.Substring(10, 16)                         'Store the numbers drawn
    Balls(i) = ThisDraw.Substring(26, 14)                           'Store the 'ball' if necessary
    Start += Finish
Next

thank you for your responses. 谢谢你的回复。 I fixed the issue by cutting out a single drawing in an array, and then cutting a substring (single Drawing) out of that, instead of trying to cut the Drawings, Dates, Numbers and Balls out of the "FullString" array straight from the stream. 我通过在数组中剪切单个图形,然后在其中剪切出一个子字符串(单个图形),而不是试图直接从“ FullString”数组中剪切图形,日期,数字和球,解决了该问题。流。

Thanks for all the help Jim 感谢Jim的所有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM