?
Solved

vb.net split function

Posted on 2011-10-05
12
Medium Priority
?
274 Views
Last Modified: 2012-06-21
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>

0
Comment
Question by:rutledgj
  • 6
  • 4
  • 2
12 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 36917883
there are several ways to to this :)

http://www.dotnetperls.com/split-vbnet
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 36917890
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36917931
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rutledgj
ID: 36917939
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36917975
...even with the change I proposed above?
0
 

Author Comment

by:rutledgj
ID: 36917994
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36918014
This is what I get:
untitled.PNG
0
 

Author Comment

by:rutledgj
ID: 36918020
Here is a sample input if it helps
sampledoc.txt
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36918024
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 36918030
Try this modification:

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

Open in new window

0
 

Author Closing Comment

by:rutledgj
ID: 36918048
What does (?i) do? It worked.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36918063
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question