Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Split String Include Delimiter at the End in C#, VB.NET

Feb 6, 2017
Introduction:

Here we will learn how to split string include delimiters at the end in c#, vb.net with example or split string without removing delimiters in c#, vb.net with example or split string keep delimiter at the end in c#, vb.net with example or split string into array of words but keep delimiters at the end of result in c#, vb.net with example. By using regular expression we can easily split the string and include the delimiters in word based on our requirements.

Description:


By using regular expressions we can easily split and keep the delimiters at the end of string. Following is the sample code snippet to split the string with delimiter “}”.

C# Code


string strmsg = "{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"}{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}";
string[] strarr = Regex.Split(strmsg, @"(?<=[}])").Where((s, i) => s != "").ToArray();

VB.NET Code


Dim strmsg As String = "{""userid"":1,""username"":""suresh"",""location"":""chennai""}{""userid"":2,""username"":""rohini"",""location"":""guntur""}"
Dim strarr As String() = Regex.Split(strmsg, "(?<=[}])").Where(Function(s, i) s <> "").ToArray()

When we execute above statements we will get result like as shown below.


If you want complete example to split string but to keep delimiter create new project and write the code like as shown below.

C# Code


using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
string strmsg = "{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"}{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}";
string[] strarr = Regex.Split(strmsg, @"(?<=[}])").Where((s, i) => s != "").ToArray();
foreach (var item in strarr)
Console.WriteLine(item);
Console.ReadLine();
}
}
}

VB.NET Code


Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim strmsg As String = "{""userid"":1,""username"":""suresh"",""location"":""chennai""}{""userid"":2,""username"":""rohini"",""location"":""guntur""}"
Dim strarr As String() = Regex.Split(strmsg, "(?<=[}])").Where(Function(s, i) s <> "").ToArray()
For Each item In strarr
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

If you observe above examples we are splitting string with special characters “}”.

Demo

Now run the application to see the result that will be like as shown below. Following is the result of split string and include delimiter at the end of string.

This is how we can split the string and include delimiter at the end in c#, vb.net. 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.