Home > Article > Backend Development > String Array in C#
To understand String Array in C#, let us first understand what is an array and string.
Array: A collection of the same type of variables stored sequentially and each variable can be accessed using its index number. Indexing an array starts with zero.
For example an array of five integers
String Array: It is an array of strings. Like a string array of employee names:String: Array of characters.
There are two ways to declare a string array:
1. Declaration with size
By using the String class object:
String[] variable_name = new String[size];
By using a string keyword:
string[] <em>variable_name </em>= new string[<em>size</em>];
2. Declaration without size
String[] variable_name;
string[] variable_name;
String array can be initialized using the new keyword. We cannot initialize string array without specifying it’s the size. There are two ways to initialize a string array.
1. At the time of declaration:
string[] <em>variable_name </em>= new string[<em>size</em>];
2. After declaration:
string [] <em>variable_name</em>;
<em>variable_name </em>= new string[<em>size</em>];
Values to string array can be assigned at the time of initialization or by using index number.
Example:
string[] stringer = new stringArr[3]{"value1","value2","value3"};
OR
string[] stringArr = new stringArr[3];
stringArr[0] = "value1";
stringArr[1] = "value2";
stringArr[2] = "value3";
Some of the examples are given below:
Code:
using System; public class StringArray { public static void Main() { // Array Declaration and Initialization string[] stringArr = new string[3] {"value1", "value2", "value3"}; // Accessing elements using index Console.WriteLine(stringArr[0]); Console.WriteLine(stringArr[1]); Console.WriteLine(stringArr[2]); } }
Output:
Code:
using System; public class StringArray { public static void Main() { // Array Declaration and Initialization string[] stringArr= new string[3] {"element1", "element2", "element3"}; // Accessing elements using for loop for(int i=0;i<stringArr.Length;i++) { Console.WriteLine(stringArr[i]); } } }
Output:
Example:
Code:
using System; public class StringSearch { public static void Main() { try { // Creating and initializing string array of flower names String[] flowerArr = new String[]{"Lily", "Jasmine", "Rose", "Sunflower"}; // Print values of the string array Console.WriteLine("Flower names:"); for (int i = 0; i < flowerArr.Length; i++) { Console.WriteLine("{0}", flowerArr[i]); } Console.WriteLine(); //Search for flower name that starts with 'R' string result = Array.Find(flowerArr, name => name.StartsWith("R", StringComparison.CurrentCultureIgnoreCase)); //Print result Console.Write("Flower name starting with 'R': {0}", result); } catch (Exception e) { Console.Write("{0}", e.Message); } } }
Output:
Sorting in a string array: We can sort a string array in many ways. Here we will sort it using Array.Sort()
Example:
Code:
using System; public class StringSort { public static void Main() { try { // declaring and initializing string array String[] stringArr = new String[] {"Cat", "Creature", "Culture", "Cow"}; // Sorting in ascending order. Array.Sort(stringArr); // reverse array to sort it in descending order Array.Reverse(stringArr); // print sorted array foreach(string val in stringArr) { Console.Write(val + " "); } } catch(Exception ex) { Console.Write(ex.Message); } } }
Output:
Converting string to string array: We can easily convert a string to a string array and vice versa as shown in the below examples:
Example:
Code:
using System; public class StringToStringArray { public static void Main() { try { string str = "Hello world"; //convert string to string array string[] strArr = new string[]{ str }; //print string array foreach(string s in strArr) { Console.Write(s); } } catch(Exception ex) { Console.Write(ex.Message); } } }
Output:
The output displayed is not a string but a string array. Example converting string array to string:
using System; public class StringArrayToString { public static void Main() { try{ } string[] strArr = new string[2]; strArr[0] = "Good"; strArr[1] = "Morning!"; //converting string array to string string str = string.Join("", strArr); //print string Console.WriteLine(str); catch(Exception ex) { Console.Write(ex.Message); } } }
Output:
From the above examples, we can say that a string array is very much similar to a list of string. But here are two major differences between them:
We can convert a string array to list as shown below:
using System; using System.Collections.Generic; using System. Linq; public class StringArrayToList { public static void Main() { string[] strArray = new string[]{ "string", "array", "to", "list"}; //converting string array to list List<string> list = strArray.ToList(); //print list foreach (string item in the list) { Console.WriteLine(item); } } }
Output:
C# also supports multidimensional string array, the simplest form of a multidimensional string array is 2D string array.
Example:
Code:
using System; public class TwoDimensionalArray { public static void Main() { string[,] strArr = new string[,] { {"Twenty", "Forty"}, {"Thirty", "Fifty"} }; for (int i = 0; i <= strArr.GetUpperBound(0); i++) { string s1 = strArr[i, 0]; string s2 = strArr[i, 1]; Console.WriteLine("{0}, {1}", s1, s2); } } }
Output:
The above is the detailed content of String Array in C#. For more information, please follow other related articles on the PHP Chinese website!