Add Dash or Underscore in String Using C#.Net in .Net Core

How to add dash or hyphen or underscore in a string using c#.net in .net core. by using Replace() to add dash, Insert and Regex to add in a position.
In today's article I will show you multiple ways to add underscore or dash in a string using c#.net in you .NET core application. The example shown in this article you can use in your asp.net core mvc, console or in windows application using c#.net. 

Here first we will create a new .NET core console application using c#.net. After creating the console application, we will use the below mention codes to add dash or underscore in the string using C#.Net.

How to Add Dash After Specific Position in String?

Here we will show how we can add a dash or a specific character in string specie position. So here I will use Insert to add dash in a specific position of the string. Here we will define the position as a number value. 

string sampleString = "This is a example string to add dash using c#.net";
string changeString = sampleString.Insert(3, "-");
Console.WriteLine(changeString);
In above code I have taken a string variable to store the sample string. Now I have used Insert method to add the add the dash (-) at position 3. Here in Insert method, we need to pass the position and the character to push. Now let's run the code and check the output.

How to Add Dash After Specific Position in String

How to Add Dash After Each Word in String?

Here we will show you how you can replace or add a dash after each word in a string using c#.net. Here I have used Replace method to replace all the space in a string with dash.  Replace methos will accept two parameters first one which word you want to replace and second what you want to replace. 
string sampleString = "This is a example string to add dash using c#.net";
string changeString = sampleString.Replace(" ", "-");
Console.WriteLine(changeString);
In above code i have used Replace function to replace the space with the dash or hyphen. Now let's run the code and check the output.

How to Add Dash After Each Word in String

How to Add Dash or Hyphen in String Using Regex?

Here I will show you how you can add dash or hyphen in a string from a specific position. Here i have used Regex to add the dash or hyphen after a specific no of character. Please check the below code. 
string sampleString = "6B29FC40CA471067B31D00DD010662DA";
string refularExpression = "\\s*(.{2})";
var dataCollection = Regex.Split(sampleString, refularExpression).Where(m => m != "");
string changeString = String.Join("-", dataCollection);
Console.WriteLine(changeString);
In above code I have taken a string in a variable with no space or a special character. After taking the input string, I have taken the regular expression in a string. Here please check the highlighted part of the code. In this part of the code, I have added a number.  In our case I have added 2. you can replace the value as per your choice

After preparing the regular expression I have used Regex.Split() method by passing the input string and the regex expression to string the string in an array format. In array variable I have used where clause to remove or eliminate the blank items. Now let's run the code to check the output.

String to Array Using Regex In C#.Net

Here we are getting the array of string with max length of 2 characters of each item in array. If you want to split the string after 3 characters you need to replace the number value from regular expression "\\s*(.{3})". Now let's check the output.

How to Add Dash or Hyphen in String Using Regex

Post a Comment