vb.net split function

I need to split some input string based on a word, not a character. Is there any way to do this in vb?

Dim MyString = "This is a test <NEW RECORD> for which I need an answer"
dim result = MyString.Split("<NEW RECORD>",StringSplitOptions.RemoveEmptyEntries)

This doesn't work.
Also, I may have multiple records with this word separating them <NEW RECORD>

rutledgjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
there are several ways to to this :)

http://www.dotnetperls.com/split-vbnet
0
plusone3055Commented:
from
http://www.dotnetperls.com/split-vbnet

Split based on words
Often you need to extract the words from a String or sentence in VB.NET. The code here needs to handle punctuation and non-word characters differently than the String Split method. Here we use Regex.Split to parse the words.

Program that splits words [VB.NET]

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
      ' Declare iteration variable
      Dim s As String

      ' Loop through words in string
      Dim arr As String() = SplitWords("That is a cute cat, man!")

      ' Display each word. Note that punctuation is handled correctly.
      For Each s In arr
          Console.WriteLine(s)
      Next
      Console.ReadLine()
    End Sub

    ''' <summary>
    ''' Split the words in string on non-word characters.
    ''' This means commas and periods are handled correctly.
    ''' </summary>
    Private Function SplitWords(ByVal s As String) As String()
      '
      ' Call Regex.Split function from the imported namespace.
      ' Return the result array.
      '
      Return Regex.Split(s, "\W+")
    End Function

End Module

Output

That
is
a
cute
cat
man
0
käµfm³d 👽Commented:
While I agree that a regex-based split can be used here, the pattern you would actually want to use would be your string, not "\w+", based on the phrasing of your question. In plusone3055's example, change the line:

Return Regex.Split(s, "\W+")

Open in new window


to:

Return Regex.Split(s, "<NEW RECORD>")

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

rutledgjAuthor Commented:
These examples still seem to just be splitting based on a space or punctuation.  I have a text file that contains a bunch of records, each record starts off with the <NEW RECORD> tag. So I need to read in all the text after each new record tag into an array.  
0
käµfm³d 👽Commented:
...even with the change I proposed above?
0
rutledgjAuthor Commented:
I tried this:

Dim SplitData() As String
 SplitData = Regex.Split(TextIn, "<NEW RECORD>")

It didn't split it. It put the entire contents into SplitData(0)
0
käµfm³d 👽Commented:
This is what I get:
untitled.PNG
0
rutledgjAuthor Commented:
Here is a sample input if it helps
sampledoc.txt
0
käµfm³d 👽Commented:
P.S.

Remember that regex is case-sensitive by default. "<NEW RECORD>" is not the same as "<new record>". You can change this behavior if necessary.
0
käµfm³d 👽Commented:
Try this modification:

Dim result() As String = Regex.Split(MyString, "(?i)<NEW RECORD>")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rutledgjAuthor Commented:
What does (?i) do? It worked.
0
käµfm³d 👽Commented:
What does (?i) do? It worked.
Makes the pattern following it case-insensitive. You can alternatively do:

Dim result() As String = Regex.Split(MyString, "<NEW RECORD>", RegexOptions.IgnoreCase)

Open in new window


but my preference is the more succinct version. Use the version that makes the most sense to you  = )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.