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

Regex Split String but Keep Delimiter in C#, VB.NET

Feb 1, 2017
Introduction:

Here we will learn how to split string include delimiters in c#, vb.net with example or split string but keep delimiters in c#, vb.net with example or regex split string but 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 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 delimiters “?@!”.

C# Code


string text = "suresh?rohini@praveen!sateesh";
string[] split = Regex.Split(text, @"(?<=[?@!])");

VB.NET Code


Dim text As [String] = "suresh?rohini@praveen!sateesh"
Dim split As String() = Regex.Split(text, "(?<=[?@!])")

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

Regex Split String but Keep Delimiter in C#, VB.NET

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.Text.RegularExpressions;

namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
string text = "suresh?rohini@praveen!sateesh";
string[] strarr = Regex.Split(text, @"(?<=[?@!])");
foreach (var item in strarr)
Console.WriteLine(item);
Console.ReadLine();
}
}
}

VB.NET Code


Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim text As String = "suresh?rohini@praveen!sateesh"
Dim strarr As String() = Regex.Split(text, "(?<=[?@!])")
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 splitting the string and including delimiter at the end of string.

This is how we can split the string into words including delimiters 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.