Hi
Consider the string "Please, go Mr. Gofer and good bye"
I want to split the string at the specific word 'Go'
(not Go in 'Gofer' or in 'good')

in my case
'Go' could be at begin of sentence or at end or somewhere inside sentence.
How can I achieve this.

expected array result should be:
Please,
Mr. Gofer and good bye

I used

  Dim ScriptSplitter As String() = scriptfile.Split(New String() {"GO"}, StringSplitOptions.None)

and wrong arraylist I get is

Please,
Mr.
fer and
od bye

Edited 1 Year Ago by gbhs

This sounds like a homework question so I won't post the code but I can suggest that you use a regular expression to match "go" between two non-alphabetic characters.

HiJim
Forgot to mention that Im a newbie.
Fact is
I have scripted a small SQL database and it contains a series of GO's . I want to split each at word Go then execute but my query also contain words like GOOD,GOAT etc.
I get an exception "Incorrect syntax near the word 'th'". I have a GOTO statement next to this 'th'. I understand VB2010 attempts to split at GOTO and execute(which is wrong of course)

Here's what I have

 Sub Create_DBSystem()
        Dim scriptfile As String = txtRichText.Text
        Dim ScriptSplitter As String() = scriptfile.Split(New String() {"GO"}, StringSplitOptions.None)
        Using Cn As New SqlConnection(Path)
            Cmd.Connection = Cn
            Cn.Open()
            For Each Str As String In ScriptSplitter
                Using Cmd = Cn.CreateCommand
                    Cmd.CommandText = Str
                    Cmd.ExecuteNonQuery()
                End Using
            Next
            Cn.Close()
        End Using
        Refresh_Data()

end sub

Not yet very familiar with regexpressions
Thanks

    End Sub

Instead of scriptfile.Split(New String() {"GO"}, StringSplitOptions.None),
did you try scriptfile.Split(New String() {" GO "}, StringSplitOptions.None)?

From your example I see that you want to get rid of the word go or Go. The best way to achieve this would be the string replace method. Just replace go or Go with "". You need to pay attention to: " Go "or "Go "or " go " etc. so you don't replace go within other words.
http://www.dotnetperls.com/replace-vbnet

Edited 1 Year Ago by Minimalist

What you want to do is identify all occurrences of the word "go" (ignoring case) while ignoring any occurences of the string "go" that are embedded in a word. A regular expression will do that.

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim rex As New Regex("\bgo\b", RegexOptions.IgnoreCase)
        Dim str As String = "Please, go Mr. Gofer and good bye"

        For Each m As Match In rex.Matches(str)
            Debug.WriteLine(m.Index)
        Next

    End Sub

End Class

In a regular expression, the meta-character "\b" matches a word boundary so "\bgo\b" matches the word "go". The above loop will identify the starting position of each "go". In this case you want to replace two chars (go) with two chard (vbCrLf) so once you know the starting position of the word it should be a simple matter to write the code to do the substitution. Because you are replacing two chars with two chars, replacing the first occurrence will not affect the starting index of any remaining occurrences.

Comments
Nice.

Thanks Jim
I will do some practice in using Regex. Its quite handy.
In the mean time I got a solution to my specific problem here

I decided to add the following dll references in my VB project in order to execute my database scripts

Microsoft.SqlServer.Smo
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc

Thank to all for the help.

To finish...

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim rex As New Regex("\bgo\b", RegexOptions.IgnoreCase)
    Dim str As String = "Please, go Mr. Gofer and good bye"

    For Each m As Match In rex.Matches(str)
        str = ReplaceSubString(str, "xxx", m.Index)
    Next

    MsgBox(str)

End Sub

'replace a substring with another substring
'of the same length

Private Function ReplaceSubString(str As String, rep As String, ind As Integer) As String

    Return str.Substring(0, ind) & rep & str.Substring(ind + rep.Length)

End Function
This question has already been answered. Start a new discussion instead.