循环不枚举新添加的数组元素
loop not enumerating newly added array elements
这是我的问题:
我有一个句子,我使用 Message.Text.Split(" ")
拆分它以获得一组单词,但是有可能某些单词用 '
而不是 space 分隔(例如 all'una
).
如果发生这种情况,数组元素将是 all'una
而不是必须分隔元素 all
和 una
。所以我为得到这个结果所做的是:
'loop start (tried for, for each and while using words.GetEnumerator())
'do stuff here
If word.Contains("'") Then
Dim apos() As String = word.Split("'")
For Each subword As String In apos
words.Add(it)
Next
End If
'do other stuff here
'loop end
问题是单词数组(或列表)正确地改变了它的大小,但循环忽略了新元素。
我在互联网上看了这篇文章:
Modifying the Collection. The enumerator object returned by GetEnumerator normally doesn't let you change the collection by adding, deleting, replacing, or reordering any elements. If you change the collection after you have initiated a For Each...Next loop, the enumerator object becomes invalid, and the next attempt to access an element causes an InvalidOperationException exception.
However, this blocking of modification isn't determined by Visual Basic, but rather by the implementation of the IEnumerable interface. It is possible to implement IEnumerable in a way that allows for modification during iteration. If you are considering doing such dynamic modification, make sure that you understand the characteristics of the IEnumerable implementation on the collection you are using.
这是我唯一的解决方案吗?
String.Split
接受令牌字符数组。
Message.Text.Split({" "c, "'"c})
您不仅要添加新词,而且还要删除原始词。否则,您将在集合中同时拥有 all'una
、all
和 una
。
您可以为结果使用不同的集合,并将未更改的项目或拆分项目的结果复制到其中:
Dim result As New List(Of String)
For Each word As String in words
If word.Contains("'")
result.AddRange(word.Split("'"C))
Else
result.Add(word)
End If
Next
这是我的问题:
我有一个句子,我使用 Message.Text.Split(" ")
拆分它以获得一组单词,但是有可能某些单词用 '
而不是 space 分隔(例如 all'una
).
如果发生这种情况,数组元素将是 all'una
而不是必须分隔元素 all
和 una
。所以我为得到这个结果所做的是:
'loop start (tried for, for each and while using words.GetEnumerator())
'do stuff here
If word.Contains("'") Then
Dim apos() As String = word.Split("'")
For Each subword As String In apos
words.Add(it)
Next
End If
'do other stuff here
'loop end
问题是单词数组(或列表)正确地改变了它的大小,但循环忽略了新元素。 我在互联网上看了这篇文章:
Modifying the Collection. The enumerator object returned by GetEnumerator normally doesn't let you change the collection by adding, deleting, replacing, or reordering any elements. If you change the collection after you have initiated a For Each...Next loop, the enumerator object becomes invalid, and the next attempt to access an element causes an InvalidOperationException exception. However, this blocking of modification isn't determined by Visual Basic, but rather by the implementation of the IEnumerable interface. It is possible to implement IEnumerable in a way that allows for modification during iteration. If you are considering doing such dynamic modification, make sure that you understand the characteristics of the IEnumerable implementation on the collection you are using.
这是我唯一的解决方案吗?
String.Split
接受令牌字符数组。
Message.Text.Split({" "c, "'"c})
您不仅要添加新词,而且还要删除原始词。否则,您将在集合中同时拥有 all'una
、all
和 una
。
您可以为结果使用不同的集合,并将未更改的项目或拆分项目的结果复制到其中:
Dim result As New List(Of String)
For Each word As String in words
If word.Contains("'")
result.AddRange(word.Split("'"C))
Else
result.Add(word)
End If
Next