extends | section | title | description |
---|---|---|---|
_layouts.documentation |
content |
C# |
C# Dev Snippets |
Method to shuffle an array of strings in place.
private void Shuffle(string[] e)
{
var rnd = new Random();
string temp;
for (int i = e.Length - 1; i > 0; i--)
{
int j = rnd.Next(0, i);
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
Strings are used for storing text.
string greeting = "Hello";
A string in C# is actually an object, which contain properties and methods that can perform certain operations on strings. For example, the length of a string can be found with the Length property:
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);
There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of the string converted to uppercase or lowercase:
string txt = "Hello World";
Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD"
Console.WriteLine(txt.ToLower()); // Outputs "hello world"
The + operator can be used between strings to combine them. This is called concatenation:
string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);
You can also use the string.Concat() method to concatenate two strings:
string firstName = "John ";
string lastName = "Doe";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);