4.1 Introduce the strings, its usages and functions
C# supports a predefined reference type known as string. We can use string to declare string type objects. Remember, when we declare a string using string type, we are in fact declaring the object to be of type System.String, one of the built-in types provided by the .NET Framework. A string type is therefore a System.String type as well.
Usage of string
Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as “hello world”. A string can contain any sequence of characters, visible or invisible, and characters may be repeated. The main usage of string is to store a sequence of characters together in a variable.
4.2 Demonstrate the creation of a string object
We can create immutable strings using string or String objects in a number of ways.
- Assigning string literals
- Copying from one object to another
- Concatenating two objects
- Reading from the keyboard
- Using ToString method.
Code:
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
// create string
string str1 = “C# Programming”;
string str2 = “Microsoft”;
// print string
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.ReadLine();
}
}
}
Output:
C# Programming
Microsoft
4.3 Demonstrate the methods of string class and deduce its usages
4.4 Introduce to string functions and examine the usage of functions
String objects are immutable, meaning that we cannot modify the characters contained in them. However, since the string is an alias for the predefined System.String class in the Common Language Runtime (CLR), there are many built-in operations available that work with strings. All operations produce a modified version of the string rather than modifying the string on which the method is called.
Code:
using System;
class HelloWorld
{
static void Main()
{
string str = “Hello World”;
Console.WriteLine(str.Length);
Console.WriteLine(str.ToUpper());
Console.WriteLine(str.ToLower());
Console.WriteLine(str.Trim());
Console.WriteLine(str.Replace(“World”, “C#”));
Console.WriteLine(str.Substring(6, 5));
string[] words = str.Split(‘ ‘);
foreach (string word in words)
{
Console.WriteLine(word);
}
Console.WriteLine(str.IndexOf(“World”));
Console.WriteLine(str.LastIndexOf(“World”));
Console.WriteLine(str.Insert(6, “C#”));
Console.WriteLine(str.Remove(6, 3));
Console.WriteLine(str.PadLeft(20));
Console.WriteLine(str.PadRight(20));
char[] chars = str.ToCharArray();
foreach (char ch in chars)
{
Console.WriteLine(ch);
}
}
}
Output:
11HELLO WORLD
hello world
Hello World
Hello C#World
Hello
World
6
6
Hello C#World
Hello ld
Hello World
Hello World
H
e
l
l
o
W
o
r
l
d
Reading from the Keyboard
It is possible to read a string value interactively from the keyboard and assign it to a string object.
string s = Console.ReadLine();
On reaching this statement, the computer will wait for a string of characters to be entered from the keyboard. When the ‘return key’ is pressed, the string will be read and assigned to the string object s.
Deference to author, some fantastic entropy.