Strings

Intro

We use the datatype string to store text.

A good way to think of a string is to see it as the array of chars it is.

Chars

Char is another datatype, but not for text, rather only for a single symbol.

To store a symbol the char uses an integer number which is than mapped to a symbol.

Representation of a string:
string text = "Hallo";
// text: { 72, 61, 108, 108, 111}

User Input

We already used the WriteLine() method from the Console class to print text on the console.
The counerpart for getting text input from the console is the ReadLine() method, also from the class Console.

string input = Console.ReadLine();
Console.WriteLine(input);

As long as we only want to get text, this works fine, but what about numerical values?

Console.Write("Please enter number: ");
string input = Console.ReadLine();
int number = input; // Error

This code does not work, because we can not store a variable of type string as integer type.
But maybe we can force it..

Console.Write("Please enter number: ");
string input = Console.ReadLine();
int number = Convert.ToInt32(input);
Console.WriteLine(input);

This works fine, but let's call it cheating and try to work that out for ourself:

//Console.Write("Please enter number: ");
//string input = Console.ReadLine();
string input = "23"; // For development..
// By using the string as array we can access the chars
char firstSymbol = input[0];
char secondSymbol = input[1];
Console.WriteLine(firstSymbol);
Console.WriteLine(firstSymbol * 10);

Output:

2
500

Why 500 and not 20?

Because in Line 8, as soon as we start to use a char like an integer (in this case multiplying by 10) it is converted to an integer.

If u look up the char '2' in the ASCII table you will find the numeric value 50. I hope that makes sense.
So to make it work we could subtract 48 first.
string input = "23";
// By using the string as array we can access the chars
char firstSymbol = input[0];
char secondSymbol = input[1];
Console.WriteLine(firstSymbol);
Console.WriteLine( ( firstSymbol-48 ) * 10);

Output:

2
20

Works fine, now let us put that in a loop, which runs through strings of any length.

For calculating we use the Pow() method from the Math class:
Pow method

string input = "438";
// Consider the input as: 
// 4*100 + 3*10 + 8*1 = 
// 4*10^2 + 3*10^1 + 8*10^0
int num = 0;
for (int i = 0; i < input.Length; i++) { // First iteration:               
    int digit = (input[i] - 48); // 52 - 48 = 4
    int power = input.Length - i - 1; // 3 - 0 - 1 = 2
    int dec = (int)Math.Pow(10, power); // 10^2 = 100
    int value = digit * dec; // 4 * 100 = 400
    num += value; // 0 + 400 = 400
}
Console.WriteLine(num);

Shorter, but hard to read:

string input = "438";
int num = 0;
for (int i = 0; i < input.Length; i++) { 
    int value = (input[i] - 48) * ( (int)Math.Pow( 10, (input.Length - i - 1) ) );
}
Console.WriteLine(num);

String Methods

Strings offer a lot of methods, which can be called by using the . Operator

. Operator
Example
Console.Write("What?"); // Access Write(string value) method from Console class
string text = "Why?";
text.Contains('a'); // Access Contains(char value) method from the String class

ToUpper, ToLower

string text = "Hello?";
text.ToUpper();
Console.WriteLine(text);

Output:

Hello?
Why is the string unchanged?
String.ToUpper Method ()
Example

Because the original string is not changed. We have to store the string, that is returned by the method call.

string text = "Hello?";
string uppercase = text.ToUpper();
Console.WriteLine(uppercase); 

Output:

HELLO?

IndexOf

IndexOf():
Returns the index of the first occurrence of a specified Unicode character or string within this instance.

Example:
string text = "this is a test sentence or it is not a test";
int position = text.IndexOf("test");
Console.WriteLine(position);

Output:

10 // Index starts by zero

Contains

Contains():
Returns true or false indicating whether a specified substring occurs within this string.

Example:
string text = "this is a test sentence or it is not a test";
bool isContaining = text.Contains("test");
Console.WriteLine(isContaining);

Ouput:

True

Substring

Substring():
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

Example:
string text = "this is a test sentence or it is not a test";
string sub = text.Substring(10);
Console.WriteLine(sub);

Ouput:

test sentence or it is not a test

Trim

Trim():
Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed.

Example:
string text = "this is a test sentence or it is not a test";
char[] chars = { 't', 's' };
string trimmed = text.Trim( chars );
Console.WriteLine(trimmed);

Ouput:

his is a test sentence or it is not a te

Alternative Code:

string text = "this is a test sentence or it is not a test";
//char[] chars = { 't', 's' };
string trimmed = text.Trim( new char[] { 't', 's'} );
Console.WriteLine(trimmed);

Split

Split():
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

Example:
string text = "this is.a test.sentence, or it,, is not a test";
string[] trimmed = text.Split( new char[] { '.', ','} );
PrintArray(trimmed);

Ouput:

{
  0 => "this is"
  1 => "a test"
  2 => "sentence"
  3 => " or it"
  4 => ""
  5 => " is not a test"
}

Replace

Split():
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

Example:
string text = "this is.a test.sentence, or it,, is not a test";
string text02 = text.Replace('t', 'T');
Console.WriteLine(text02);

Ouput:

This is.a TesT.senTence, or iT,, is noT a TesT