Strings
4.1 Introduction to strings
4.2 Creating an string object
4.3 Methods of string class
4.4 String functions
Introduction to strings
A string in C# is a sequence of characters used to store text data like names, addresses, etc. It is a reference type stored in memory and cannot be modified once created. Strings are defined using double quotes or the String class. They can be manipulated using various methods like concatenation, finding length, and searching for substrings.
Reference Type
In C#, a reference type is a type that is stored in memory on the heap rather than on the stack. The heap is a region of memory used for storing objects and data structures, whereas the stack is used for storing method calls and local variables.
Examples of reference types include: strings, arrays, objects of classes, etc
Creating an string object
There are several ways to create a string object in C#:
Using string literals: You can create a string literal by enclosing a sequence of characters in double quotes:
Example:
string greeting = "Hello, World!";
Using the String class: You can create a string using the String class and its various constructors:
Example:
char[] chars = { 'H', 'e', 'l', 'l', 'o' };
string greeting = new string(chars);
Concatenating strings: You can create a string by concatenating other strings using the + operator:
Example:
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Using string interpolation: You can create a string using string interpolation, which allows you to embed expressions within a string literal:
Example:
int age = 30;
string message = $"My name is John and I am {age} years old.";
Methods of string class
These are some of the most commonly used methods of the System.String class in C#.Some of the most commonly used methods are:
Length: Returns the number of characters in the string.
Example:
string greeting = "Hello, World!";
int length = greeting.Length;
Concat: Concatenates two or more strings into a single string.
Example:
string firstName = "John";
string lastName = "Doe";
string fullName = string.Concat(firstName, " ", lastName);
Substring: Returns a portion of the string.
Example:
string greeting = "Hello, World!";
string firstWord = greeting.Substring(0, 5);
Replace: Replaces all occurrences of a specified string with another string.
Example:
string greeting = "Hello, World!";
string updatedGreeting = greeting.Replace("Hello", "Goodbye");
ToLower and ToUpper: Converts the string to lowercase or uppercase.
Example:
string greeting = "Hello, World!";
string lowercaseGreeting = greeting.ToLower();
string uppercaseGreeting = greeting.ToUpper();
String functions
"string method" and "string function" refer to the same thing in C#. Both terms are used to describe the methods of the System.String class that perform various operations on strings. These methods can be used to modify, search, and manipulate strings in various ways, making it easier to work with strings in C#. Whether you call them "string methods" or "string functions", they serve the same purpose and are used in the same way.
If you have any dough, please let me know