A two-dimensional rectangular string array
using System;
class MainClass
{
public static void Main()
{
const int rows = 8;
const int columns = 8;
string[,] stringArray = new string[rows, columns];
stringArray[0, 0] = "R";
stringArray[1, 0] = "P";
stringArray[2, 3] = "K";
stringArray[3, 5] = "B";
stringArray[4, 4] = "A";
stringArray[5, 3] = "E";
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
Console.WriteLine("stringArray[" + row + ", " + column + "] = " +
stringArray[row, column]);
}
}
}
}
stringArray[0, 0] = R
stringArray[0, 1] =
stringArray[0, 2] =
stringArray[0, 3] =
stringArray[0, 4] =
stringArray[0, 5] =
stringArray[0, 6] =
stringArray[0, 7] =
stringArray[1, 0] = P
stringArray[1, 1] =
stringArray[1, 2] =
stringArray[1, 3] =
stringArray[1, 4] =
stringArray[1, 5] =
stringArray[1, 6] =
stringArray[1, 7] =
stringArray[2, 0] =
stringArray[2, 1] =
stringArray[2, 2] =
stringArray[2, 3] = K
stringArray[2, 4] =
stringArray[2, 5] =
stringArray[2, 6] =
stringArray[2, 7] =
stringArray[3, 0] =
stringArray[3, 1] =
stringArray[3, 2] =
stringArray[3, 3] =
stringArray[3, 4] =
stringArray[3, 5] = B
stringArray[3, 6] =
stringArray[3, 7] =
stringArray[4, 0] =
stringArray[4, 1] =
stringArray[4, 2] =
stringArray[4, 3] =
stringArray[4, 4] = A
stringArray[4, 5] =
stringArray[4, 6] =
stringArray[4, 7] =
stringArray[5, 0] =
stringArray[5, 1] =
stringArray[5, 2] =
stringArray[5, 3] = E
stringArray[5, 4] =
stringArray[5, 5] =
stringArray[5, 6] =
stringArray[5, 7] =
stringArray[6, 0] =
stringArray[6, 1] =
stringArray[6, 2] =
stringArray[6, 3] =
stringArray[6, 4] =
stringArray[6, 5] =
stringArray[6, 6] =
stringArray[6, 7] =
stringArray[7, 0] =
stringArray[7, 1] =
stringArray[7, 2] =
stringArray[7, 3] =
stringArray[7, 4] =
stringArray[7, 5] =
stringArray[7, 6] =
stringArray[7, 7] =
Change element in a string array
using System;
class MainClass {
public static void Main() {
string[] str = { "This", "is", "a", "test." };
Console.WriteLine("Original array: ");
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
Console.WriteLine("\n");
// change a string
str[1] = "was";
str[3] = "test, too!";
Console.WriteLine("Modified array: ");
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
}
}
Original array:
This is a test.
Modified array:
This was a test, too!
Initialize string arrays
using System;
class MainClass
{
public static void Main()
{
string[] stringArray = {"Hello", "World"};
foreach (string myString in stringArray)
{
Console.WriteLine("myString = " + myString);
}
}
}
myString = Hello
myString = World
Use the Sort() method to sort the elements in a string array
using System;
class MainClass
{
public static void Main()
{
string[] stringArray = {"t", "i", "a", "test", "abc123", "abc345"};
Array.Sort(stringArray);
Console.WriteLine("Sorted stringArray:");
for (int i = 0; i < stringArray.Length; i++)
{
Console.WriteLine("stringArray[" + i + "] = " + stringArray[i]);
}
}
}
Sorted stringArray:
stringArray[0] = a
stringArray[1] = abc123
stringArray[2] = abc345
stringArray[3] = i
stringArray[4] = t
stringArray[5] = test