Methods built into the string data type
The string data type has many methods built into it. The ToUpper() and ToLower() methods for example will change a string to upper case and lower case letters respectively.
string s = "Hello";
string upper = s.ToUpper();
Console.WriteLine(upper);
string lower = s.ToLower();
Console.WriteLine(lower);
The Length property of a string will tell you how many characters there are in a string.
string s = "Hello";
Console.WriteLine(s.Length);
The SubString() method is used to get a specific part of a string. It takes 2 parameters which are the position to start at and the number of characters to copy. The following example extracts the word "Good" from "Good Morning" by getting 4 characters starting at position 0.
string s = "Good Morning";
string t = s.Substring(0, 4);
Console.WriteLine(t);
The string type has many other methods. You can find out about them by reading about the string data type in the C# documentation.
Converting to and from strings
Every object in C# has a ToString() method which will convert that object to a string. If you use the ToString() method of an int you will get the value of that int as a string. Some other objects that can't be converted directly to a string will return a value that best represents the object as a string.
int i = 5;
string s = i.ToString();
Console.WriteLine(s);
Whenever you ask a user to enter a number it will be read as a string. You will probably want to convert the number to an int or double type. The int and double types both have a Parse() method for converting a string to the correct data type. You must however use the int and double classes when calling the Parse() method and not call it from the variable name.
string s = Console.ReadLine();
int i = int.Parse(s);
double d = double.Parse(s);
Escaping characters
In c# the \ character is used to escape special characters. If you want to put an enter character in a string then you have to use \n because putting an enter in your code is obviously not practical. There are many other special characters such as \t for tab. The following example will print Hello and then put in an enter character and will put Good Morning on the next line with a tab between the 2 words.
string s = "Hello\nGood\tMorning";
Console.WriteLine(s);
Because the \ is used in this way it must also be escaped. This is done by simply using 2 of them next to each other. You will be using the \ character a lot because it is used as a directory separator when working with files.
string s = "C:\\Test";
Console.WriteLine(s);
If you don't want to have to escape characters all the time then you can use the @ sign in front of a string to make it not escape any characters.
string s = @"C:\Test";
Console.WriteLine(s);
Strings as arrays of characters
A string is actually just an array of characters which means you can use square brackets like you would with an array to access any character in a string. Here is an example of how to get the letter 'e' from the string "Hello".
string s = "Hello";
char c = s[1];
Console.WriteLine(c);
0 comments:
Post a Comment