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
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()
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 “}”.
No comments:
Post a Comment