Skip to content

Datatype String And Some Of Its Methods

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Intro

In this article, we provide the source code for the lecture “Datatype String And Some Of Its Methods” from the C# Masterclass. Moreover, here you can find extra information about the string data type.

 

String type overview

The string data type is a data type used to represent sequences of characters, typically used to represent text. In most programming languages, a string is enclosed in quotes (either single or double quotes) and can contain any combination of letters, numbers, symbols, and spaces.

 

In C#, strings are implemented as objects with various methods and properties that allow you to manipulate and work with strings. For example, you can concatenate two strings, extract a portion of a string, convert a string to upper or lowercase, and so on.

Strings are an important data type in programming, as they are often used to represent text-based data, such as names, addresses, and other types of information. They are also frequently used to store and manipulate information entered by users in forms, web pages, and other applications.

Useful string methods

The following are the four most popular methods for working with strings in C#, along with code examples to illustrate how they work:

  1. Length: This method returns the length of the string. For example:
string name = "John Doe";
int length = name.Length; // length = 8

 

  1. Substring: This method returns a portion of the string. You can specify the starting index and the length of the substring. For example:
string name = "John Doe";
string firstName = name.Substring(0, 4); // firstName = "John"

 

  1. Replace: This method replaces all occurrences of a specified character or substring within the string with a new value. For example:
string name = "John Doe";
string updatedName = name.Replace("John", "Jane"); // updatedName = "Jane Doe"

4. Trim: This method removes leading and trailing white space characters from the string. For example:

string name = " John Doe ";
string trimmedName = name.Trim();

 

These four methods are among the most popular for working with strings in C#, and can be used to perform various operations such as manipulating, transforming, and formatting text. Whether you’re a seasoned developer or just starting out, mastering these string methods is essential for writing efficient and effective code.

Advanced understanding

In order to understand how we are changing uppercase to lowercase and vice versa, you need to know a few things. First of all, I will remind you that a string is just a sequence of type chars.

 

But how is char represented in our memory? You will be surprised or not, but it is also a numerical type. Back then, in 1961, people agreed to use a table that showed the corresponding value of each English letters and special characters. And it is still a useful table called ASCII: https://www.asciitable.com/

On it, you can notice that there are both lowercase and uppercase letters.

So now I can tell you a “secret”. The methods we showed above do addition or subtraction to each char in the string.

 

Source code

string myName = "Denis";
string message = "My name is " + myName; // concatination

string capsMessage =  message.ToUpper(); // "MY NAME IS DENIS"
string lowerMessage = message.ToLower(); // "my name is denis"

Console.WriteLine(message);
Console.WriteLine(capsMessage);
Console.WriteLine(lowerMessage);

 

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide