C# String DataType

Strings in C#

A string in C# is a sequence of characters used to represent text. It is one of the most used data types in programming and is essential for handling textual data. Here’s a comprehensive explanation of what a string is, its characteristics, and how it is used:
Creating a simple string  with a value

string Name =  “Justin”;

Gets the number of characters in the string

int size = Name.Length;
Console.WriteLine(size); // Output will be 6

Joining 2 string using concatenation format

string firstname = “Justin”;
string lastname = “bieber”;
Console.WriteLine(firstname + “ “ + lastname) ; // Output will be (Justin bieber)
This line prints a concatenated string to the console. The + operator is used to concatenate the strings. In this case: firstname + “ “ + lastname combines the string with a space between Justin & bieber resulting in (Justin bieber)

Joining 2 string using interpolation format

string firstname = “Justin”;
string lastname = “bieber”;
Console.WriteLine($”{firstname} {lastname}”) ; // Output will be (Justin bieber)
The $ before the quotes means you’re using interpolation format, respectively. During execution,   {firstname} is replaced with “Justin” and  {lastname} is replaced with “bieber”.

Joining 2 string using placeholder format

string firstname = “Justin”;
string middlename = “hudson”;
string lastname = “bieber”;
Console.WriteLine(“{0} {1}”firstname , lastname) ; // Output will be (Justin bieber)
{0} is a placeholder for the first value (firstname), which is “Justin” and {1} is a placeholder for the second value (lastname), which is “bieber”.When this line runs, it fills in {0} with “Justin” and {1} with “bieber”, so it prints: Justin bieber.
Console.WriteLine(“{0} {1} {2}”firstname ,middlename, lastname) ; // Output will be (Justin hudson bieber)
{0} is for firstname, which is “Justin” , {1} is for middlename, which is “hudson” and {2} is for lastname, which is “bieber”.When this line runs, it fills in {0} with “Justin”, {1} with “hudson”, and {2} with “bieber”, so it prints: Justin hudson bieber.

Convert string all characters to uppercase

string Name = “Justin”;
string Capital = Name.ToUpper();
ConsoleWriteLine(Capital) ; //Output will be JUSTIN

Convert string all characters to lowercase

string Name = “Justin”;
string lowercase = Name.ToLower();
ConsoleWriteLine(lowercase) ; //Output will be justin

Retrieves a substring from the string

string Name = “Justin”;
string substring = Name.Substring(0, 4);
Console.WriteLine(substring); // Output  will be Just
The first parameter (0) in the substring method specifies the starting index of the substring. Indexing in strings is zero-based, meaning the first character has an index of 0. The second parameter (4) specifies the length of the substring to retrieve. This means we want to extract 4 characters starting from index 0. So, we get output as Just

Removes all starting and ending white-space characters

string Name = “ Justin drake ”;
string space = Name.Trim();
Console.WriteLine(space); // Output will be “Justin drake”
it has removed white space from the start & end and kept blank space between Justin and drake

Determines whether a string contains a specified substring

string Name = “Justin drake”;
bool shortname = Name.Contains(“Justin”);
Console.WriteLine(shortname); // Output will be True

Determines whether a string starts with a specified substring

string Name = “Justin”;
bool starting = Name.StartsWith(“Ju”);
Console.WriteLine(starting); // Output will be True

Determines whether a string ends with a specified substring

string Name = “Justin”;
bool ending= Name.EndsWith(“!”);
Console.WriteLine(ending); // Output will be False

Determines whether the value in the string is the same as the value mentioned in Equals method

string Name = “Justin”;
bool  value = Name.Equals(“Hello, World!”);
Console.WriteLine(value ); // Output will be False

Copies one string variable value to another string variable

string Name = “Justin”;
string duplicate = string.Copy(Name);
Console.WriteLine(duplicate); // Output will be Justin value will copied in duplicate varibale

Let’s suppose we have a variable called as Name = “Justin bieber” and we want to define another variable called as changeword =  drake bieber then we can use the replace method to change the name of Justin bieber to drake bieber and save it in the changeword variable

string Name = “Justin bieber”;
string changeword = Name.Replace(“Justin”, “Drake”);
string changecharacter = Name.Replace(“b”, “B”);
Console.WriteLine(changeword); // Output: “Drake bieber”
Console.WriteLine(changecharacter); // Output: “Justin BieBer”
It can replace entire words (“Justin” -> “Drake”) or individual characters (‘b’ -> ‘B’). In the first output, “Justin” is replaced by “Drake”, resulting in “Drake bieber”. In the second output, all lowercase ‘b’ characters are replaced by uppercase ‘B’, producing “Justin BieBer”

Input the value mentioned in the insert method into the string variable.

string Name = “Justin”;
string newName = Name.Insert(6, ” bieber”);
Console.WriteLine(newName); // Output: Justin bieber

For example, if string variable name = Justin we want to add Bieber then we use the insert method as Name.Insert(value of the index, value to be added ). The value of the index is the place where the new value is to be added i.e. in the below example, we are adding Bieber after Justin i.e. on the 6th  index value

The remove method will Delete all the values mentioned after the index number which mentioned in the remove method. In the below example Name.Remove(5) will delete all the characters after index 5 i.e. “n drake” is deleted from Justin drake

string Name = “Justin drake”;
string shortened = Name.Remove(5);
Console.WriteLine(shortened); // Output will be Justi

Course Video

Task:

1. Create a Simple String: Create a string variable called myName and assign it your name. Print the string to the console.

2. Get Length of String: Write a program to get the number of characters in the string myName and print the length.

3. Concatenation: Create two string variables firstName and lastName with your first and last names. Concatenate them using the + operator and print the result. Repeat the above step using string interpolation. Repeat the above step using placeholders.

4. Case Conversion: Convert the string myName to uppercase and print the result. Convert the string myName to lowercase and print the result.

5. Substring: Extract a substring from myName that includes the first three characters and print it. Extract the last three characters of myName and print them.

6. Trim: Create a string with leading and trailing white spaces and print it. Then, remove the white spaces using the Trim method and print the result.

7. Contains: Check if the string myName contains a specified substring (e.g., your first name) and print the result.
StartsWith and EndsWith: Check if the string myName starts with your first name and print the result. Check if the string myName ends with your last name and print the result.

8. Equals: Create another string variable with the same value as myName and check if they are equal using the Equals method. Print the result.

9. Copy: Copy the value of myName into another string variable called copiedName and print it.

10. Replace: Replace your first name in the string myName with another name and print the result. Replace all occurrences of a specific character in myName with another character and print the result.

11. Insert: Insert a middle name into the string myName at the appropriate position and print the result.

12. Remove: Remove the last name from the string myName and print the result.