Way of Reversing strings in C#

vishalneeraj-24503
Posted by vishalneeraj-24503 under C# category on | Points: 40 | Views : 926
We can Reverse a String Array or List collection in many ways:-

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
list.Add("Vishal");
list.Add("Rajesh");
list.Add("Pooja");
list.Add("Vipin");

//1st Way using Reverse method
list.Reverse();

foreach (string value in list)
{
Response.Write("<br/>" + value);
}

//2nd Way
string[] str_names = new string[4] { "Vishal", "Rajesh", "Pooja", "Vipin" };

if (str_names != null)
{
if (str_names.Length > 0)
{
for (int index = str_names.Length - 1; index >= 0; index--)
{
Response.Write("<br/>" + str_names[index]);
}
}
}

//3rd Way
string names = "Vishal,Rajesh,Pooja,Vipin";
string[] comma_sep = names.Split(',');

if (comma_sep != null)
{
if (comma_sep.Length > 0)
{
for (int index = comma_sep.Length - 1; index >= 0; index--)
{
Response.Write("<br/>" + comma_sep[index]);
}
}
}
}
}


Output would be:-
Vipin
Pooja
Rajesh
Vishal

Comments or Responses

Login to post response