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:
In previous articles I explained json serialization and deserialization in asp.net, jquery split string into array with comma delimiter, sql substring function to get particular part of string with
example, regex to replace all special characters with space in c#, sql server split function to split string with example, sql function return table as parameter with example and
many articles related to in JSON,
asp.net,
mvc, c#,
vb.net.
Now I will explain how to split the string and keep the delimiters at the end in
c#,
vb.net
with example.
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.
|
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.