1

here is my problem: I have a sentence and I split it using Message.Text.Split(" ") to get an array of words, however there is the possibility that some words are separated with ' instead of space (Ex. all'una). if this happens that array element will be all'una instead of having to separated elements all and una. So what I did for get that result is:

'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

problems is that words array (or list) change it's size correctly, but the loop ignore new elements. I looked in Internet and read this:

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.

Is this the only solution I have?

  • Hi, you don't actually need the `For Each`, this will work too: `words.AddRange(word.Split("'"c).ToList))`. But I'm not really sure what you mean with _"the loop ignore new elements"_? – Saragis Aug 06 '15 at 10:35
  • thanks for the suggestion. I'll try to be more clear: suppose the input sentence is _"oggi all'una"_ it will be splitted in `oggi` and `all'una`, instead it should be splitted into `oggi`, `all` and `una`. I thought to split the `all'una` element and append the 2 resulting elements to the words array. I do this correctly with mine or your suggested code, but the loop don't see the 2 added elements to words and the loop ends without enumerating them. – Alex Cortinovis Aug 06 '15 at 10:50
  • What kind of collection are you using? The `Split` method returns an array, but you can't add items to an array. – Guffa Aug 06 '15 at 11:14
  • actually I can, with this extension I found and used: http://stackoverflow.com/questions/18097756/fastest-way-to-add-an-item-to-an-array – Alex Cortinovis Aug 06 '15 at 20:59

2 Answers2

2

String.Split accepts an array of token characters.

Message.Text.Split({" "c, "'"c})
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
0

Not only would you want to add the new words, but you would want to remove the original word also. Otherwise you would have both all'una, all and una in the collection.

You can use a different collection for the result, and either copy unchanged items or the result of splitted items to it:

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
Guffa
  • 687,336
  • 108
  • 737
  • 1,005