Donate

Split A String With Multiple Space As Delimiter Or Separator Using Regular Expression Or String.Split In C# and VB.NET

Given that you have a string that you want to split and the separator is two or more spaces. You can achieve using split() or Regex.
VB.NET
1
2
3
4
5
6
7
8
Dim str As String = "Rule Name:                            WhosThere-176.44.28.203"
Dim whosThereVar As String
Dim whosThereString As String

'using split
whosThereVar = str.Split(New String() {"  "}, StringSplitOptions.RemoveEmptyEntries)(1).Trim()
'using regex
whosThereString = System.Text.RegularExpressions.Regex.Split(str, "\s{2,}")(1)
C#
1
2
3
4
5
string str = "Rule Name:                            WhosThere-176.44.28.203";
//using split
string whosThereVar = str.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
//using regex
string whosThereString = System.Text.RegularExpressions.Regex.Split(str, @"\s{2,}")[1];

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.