Two most important and underrated methods used on string datatype in C#

Vishal Pathak
Abhima C# Programming
2 min readSep 16, 2022

--

You have seen multiple string methods and most of them are very common ones but today I will give you two uncommon string methods which are rarely used but are very important.

Lord Vishnu Statue in Indonesia

Consider a scenario where you have received a data in plain string which has values separated by some character like comma(,) , * or anything. To read the data in that scenario you will need below two string methods.

  • String.Split()
  • String.Join()

String.Split():- As discussed in above scenario in case if we have a data in string format and the values inside the data are separated with some string. If we want to do some operation with data then we can use split method on that data. This method will split the data by given character and put them in an array.
Example:-

string states=”Maharashtra,UttarPradesh,Tamilnadu,Gujarat”;

var statesInArray=states.Split(“,”);

//statesinArray=[“Maharashtra”,“UttarPradesh”,“Tamilnadu”,“Gujarat”];

As in the above code you can see how easily we can convert plain string to the array by using Split method.

In Split method we have an optional parameter StringSplitOptions. StringSplitOptions parameter is enum which can have three possible values None, RemoveEmptyEntries, TrimEntries.

None : It will result the same data as Split(string) does.

RemoveEmptyEntries: It will remove blank values before forming the array as below.

var cities=”Mumbai, ,Banglore”;
var cityArray=cities.Split(“,”,StringSplitOptions.RemoveEmptyEntries);
//cityArray=[“Mumbai”,”Banglore”]

TrimEntries(Available in Asp.net 5 or above):- This parameter will remove the leading and trailing spaces after split the values by separator string as give below.

var cities=”Mumbai,Delhi,Banglore ”;
var cityArray=cities.Split(“,”,StringSplitOptions.TrimEntries);
//cityArray=[“Mumbai”,”Delhi”,”Banglore”]

String.Join():- Join method joins the elements from the collection with the joining string provided in the parameter and forms the string.

Let’s say you used split method to convert plain string data to an array and after doing all the operations now you again want to convert it back to the plain string. Here in this scenario you can use Join method.

Example:-

string states = “Maharashtra,UttarPradesh,Tamilnadu,Gujarat”;

var statesInArray = states.Split(“,”);

//statesinArray=[“Maharashtra”,”UttarPradesh”,”Tamilnadu”,”Gujarat”];

foreach (var item in statesInArray)

{

Console.WriteLine(item);

}

var statesAfterOps = string.Join(“,”, statesInArray);

//statesAfterOps=”Maharashtra,UttarPradesh,Tamilnadu,Gujarat”

As you can see above code we have converted back statesAfterOps array value to plain string separated by comma.

Thank you for reading please comment your suggestions, share the article, follow me and Abhima C# Programming publication.

Bhagavad Gita Verse of the Day

न तद्भासयते सूर्यो न शशाङ्को न पावक: |
यद्गत्वा न निवर्तन्ते तद्धाम परमं मम || 6||

na tad bhāsayate sūryo na śhaśhāṅko na pāvakaḥ
yad gatvā na nivartante tad dhāma paramaṁ mama

BG 15.6: Neither the sun nor the moon, nor fire can illumine that Supreme Abode of Mine. Having gone There, one does not return to this material world again.

--

--

Vishal Pathak
Abhima C# Programming

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud