Parse a sentence in VB. Net

I am trying to parse a sentence wherein the words are seperated by spaces.  The words are then stripped  of commas (",") or semicolons (";") and written to a database.  This code to split the words works in VB.net 2002 but not VB.Net 2003 or 2005.  Iam using streamReader/StreamWriter to read/write from a text file.

Variables:
currentSentence as String
splitSentence() as String
i as Integer

Code:

Do Until sr.Peek = -1
   currentSentence = sr.readLine
   For i = 1 To currentSentence.Length -1  ' Ignore first word 
      splitSentence = currentSentence.Split(" ")*
      'Some other code to write sentence goes here
   Next i
 Loop 

* the Split method is splitting the characters and not the words in VB.Net 2003 and 2005

Any suggestions

Thanks Trebormac


Answer this question

Parse a sentence in VB. Net

  • citegestion

     Trebormac wrote:
    the Split method is splitting the characters and not the words in VB.Net 2003 and 2005
    Perhaps your code to write out the sentence is confusing you somehow, or perhaps your sentence has more spaces in it than you expect

    The following code works as you'd expect in Visual Studio 2003, spliting words not characters.


    Dim s1 As String
    s1 = "ABC DEF GHI, JKL "

    Dim s2() As String

    s2 = s1.Split(" ")

    For Each s As String In s2
       Console.WriteLine(s)
    Next

     



    The output looks like this...
       
       ABC
       DEF
       GHI,
       JKL


  • BZRK

    String.Split will break the input string into an array of strings. So I don't see why you need the For loop. You also need to specify that the split is a character using " "c. Try this and modify it to read from your StreamReader:

    Dim currentSentence As String = "This is a test!!!"
    Dim splitSentence() As String = currentSentence.Split(" "c)
    For Each s As String In splitSentence
      Console.WriteLine(s)
    Next



  • DannyvG

    Thanks!  This did the fix.
  • Parse a sentence in VB. Net