Split String VB.NET Examples

How to Split String in VB.NET? There are different ways to call Split function in VB.NET. Below solutions guide you VB.NET Split string/sentence with examples.

Solution 1:

Simple Split Example. Below example uses Split on String.
' Input string
Dim str As String = "This is a test string"

' Split string based on spaces
Dim words As String() = str.Split(New Char() {" "c})

' Use For Each loop over words and display them. See the result below in Output.
Dim word As String
For Each word In words
          Console.WriteLine(word)
Next


Solution 2:
Split Parts of a file path in VB.NET. Below example that splits file path.
' file system path we need to split
Dim str As String = "C:\Users\TestUser\Documents\Doc"

' Split the string on the backslash character (\)
Dim parts As String() = str.Split(New Char() {"\"c})

' Loop through result strings with For Each.
Dim part As String
For Each part In parts
       Console.WriteLine(part)
Next


Solution 3:
Split string or sentence base on words in VB.NET
There are cases where you need to extract the words from a string/sentence in VB.NET. Below solution can handle non-word characters differently than the String Split method. For the same we need to use Regex.Split to parse the words in a string.

Add below line on top of your page.
Imports System.Text.RegularExpressions

Below code will splits words in VB.NET
Dim str As String
Dim arr As String() = SplitWordsInString("Oh! this is a simple task.")

For Each str In arr
        Console.WriteLine(str)
Next
Console.ReadLine()

Private Function SplitWordsInString (ByVal str As String) As String()   
          Return Regex.Split(str, "\W+")
End Function

11 comments:

  1. Good Article, I used to store IDs in single field and searching made very fast. Again I say good

    ReplyDelete
  2. Hi,how to use split get the value after specific word?
    like content is
    Date: 23/4/2013
    Name: John
    Tel No: 2349832
    then i want to scan the document then if find date it will get me the value 23/4/2013.
    My code only works for word in sequence means if find ":" then it will show the value behind.

    Dim splitVals As String() = rng.Text.Split(":")
    txtDate.Text = splitVals(1)
    txtRefNo.Text = splitVals(2)
    txtCustName.Text = splitVals(3)

    ReplyDelete
  3. Hello YHK,

    You can use below logic.

    Form_load -
    DirectoryInfo dir1 = new DirectoryInfo(@"C:\Temp\");

    FileInfo[] files = dir1.GetFiles("*.docx");

    foreach (FileInfo file in files)
    {
    if (file.Length > 0)
    {
    string content = WriteStuffWordDocContent(@"C:\Temp\" + file);
    }
    }
    Function -

    private static string WriteStuffWordDocContent(string strDoc)
    {
    Stream stream = File.Open(strDoc, FileMode.Open);
    WordprocessingDocument wordprocessingDocument =
    WordprocessingDocument.Open(stream, true);
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;


    string content = body.InnerText;

    int first = content.IndexOf("Date:");
    first = first + 5;
    int last = content.LastIndexOf("Name:");
    string str2 = content.Substring(first, last - first);

    MessageBox.Show(str2);

    wordprocessingDocument.Close();
    return content;
    }

    Hope that helps.

    ReplyDelete
  4. Hi there, I have a database with more than 1000 members, I coded a program to send bulk sms. The program collect mobile numbers from the mobile section in the database and the join them with semicolon (;) as requested by the sms server. Now the problem is the url I use to send the SMS can not take more than 200 long characters so I must find a way to split the numbers in 200 bundles and submit the url respectively, how do I split the string from 1000 phones in to small groups of 200

    ReplyDelete
  5. well ,I have text file contain these string 666 66 55 333 55 for example and I have two arrays of string one of them contains the string in text file without repetitions and the other values from 1 to array1 size I need to replace each string in text file for example if array1 {666,66,333,55} array2 will be {1,2,3,4} text file should be like that 1 2 4 3 4 i try this code the problem with it is replace 66 by 1 and 666 become 16 ,so it consider 66 as part of 666 I need away to get over-this


    For j = 0 To stra.GetLength(0)

    Const ForReading = 1
    Const ForWriting = 2

    Dim objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objFile = objFSO.OpenTextFile(OpenFileDialog1.FileName, ForReading)

    Dim strText = objFile.ReadAll

    objFile.Close()
    Dim strNewText = Regex.Replace(strText, stra(j), stra2(j))

    objFile = objFSO.OpenTextFile(OpenFileDialog1.FileName, ForWriting)
    objFile.WriteLine(strNewText)
    objFile.Close()
    Next



    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete