在循环字符串

时间:2015-05-31 06:53:55

标签: vb.net

我正在使用Visual Studio社区 嗨,我写了一个循环,循环通过一系列的彩票图。我想抓一个完整的图纸,并删除日期,图纸和强力球数字。

第一次通过循环时一切都很顺利,我得到06/29/2002 04 33 35 36 45 Powerball: 22,但第二次通过循环得到07/03/2002“04 33 35 36 45”强力球:22。

第二次循环回到第一次绘图的数字。我知道我可以写

Numbers(i) = Full_History.Substring(Start + 10, 16),和 Balls(i) = FullHistory.Substring(Start + 26, 14),但这似乎不对。我希望我解释了这一点。任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:1)

如果您有这样的专业文字,可以使用regular expression提取数据,就像在此片段中一样:

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)

当然,它不是最优秀的RegEx,但它应该很容易遵循:

  • \ d代表单个数字
  • /是字面斜杠字符
  • {4}是一个量词,意思是前一个事物中的4个
  • / s是空白
  • / s +表示您喜欢的空白
  • ()表示捕获组,允许访问每个匹配项的.Groups属性中的值

因此,您可以找到给定文本中的所有匹配项,并可以通过迭代匹配来轻松提取所有子组。

对于您的一般问题:

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

这些行将始终从源字符串中裁剪出相同的部分(例如索引10到26)。您还需要使用Start变量缩放Substring start部分,例如

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

否则,每个元素的结尾都是相同的并不令人惊讶。或者为了避免更多的混淆,首先将整个事物裁剪掉,然后将这个新字符串分解为其部分:

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

答案 1 :(得分:0)

感谢您的回复。我通过在数组中剪切单个图形,然后从中剪切子图形(单个图形)来解决问题,而不是试图从“FullString”数组中直接从图形中切割图形,日期,数字和球。流。

感谢所有帮助 吉姆