As a beginner, you may have endless doubts and questions about the different concepts, functions, and keywords in C#, as you rightfully should. Or you be at an intermediate level looking to hone your skills or perhaps, even an expert, looking to challenge your skills and refresh your memory about the programming language. No matter at what stage of expertise you may have, this is the perfect place to jog your cognitive skills to learn C# through and through. Thus, in this article, we will be going through the fundamentals of one of the simplest concepts in C#, which is the C# substring. Let us get down to it.
Before we dive into the concept of C# substrings, let us take a brief look at what strings are, to understand how both of these concepts work hand in hand.
Table of Contents
What is a C# string?
Strings are one of the most useful types of data that you will be working with, in C#. We will show you in brief how to work with strings and how you can leverage its power within your program. A string simply put, refers to a data type that contains multiple characters, like text. If you want to enter any text into your program, you would make use of the ‘string’ data type. Like any other data type in C#, you can also store strings within variables. Let us look at a basic example of a syntax used in working with strings in C#.
using System;
class Program {
static void Main (string[] args) {
string example = "string substring";
// Here, ‘string’ is the variable, ‘example’ is the name associated to the variable and 'string substring’ is the string value stored within the variable
Console.WriteLine( example);
Console.ReadLine();
}
}
OUTPUT:
String substring
If you wish to add one string to another and make it seem like one continuous string, all you need to do is to add ‘+’ between the two strings. This process is called as the Concatenation of strings. For instance,
using System;
class Program {
static void Main (string[] args) {
string example = "string substring" + “is the topic of this article”;
Console.WriteLine( example);
Console.ReadLine();
}
}
OUTPUT:
String substring is the topic of this article
Moving on, let us now proceed to look at how C# Substrings work and how we can use it to our advantage.
What is a C# Substring?
If there is ever an occurrence in which you have a piece of data of the string data type, you can then extract a specific part of that string into a separate string by using the ‘substring method.’ Simply put, what substring in C# does is that it creates a smaller string out of a larger string.
While using the substring method, there are two important parameters that the coder or developer needs to define in order for the program to run in the desired manner. They need to tell the program from part of the existing string it should begin reading from and at what point it should stop; essentially the starting point (start index) for your desired substring.
While typing out the program, you will see an indicator dialogue box define this as the ‘zero-based starting character position of a substring in this instance.’ It may sound verbose and complicated, but what it simply means is that if you correspond every character in the base string to a numbered index – indexes in programming languages begin with 0 – then the index position of the point from which you want your C# substring to begin.
The 2nd parameter is the length of the substring that the coder or programmer desires. For instance, if the substring needs to contain the term ‘CODE,’ then, as you can see, it has 4 characters. Therefore, in this case, the length of the substring will be 4.
The string method can be overloaded by running different parameters to it. They are as follows:
- String.Substring(Int32): The substring retrieved will start at a determined character’s index position and continues to the end of the string.
- String.Substring(Int32, Int32): The substring retrieved will start at a determined character’s index position and will end at a specified length.
Let us look at a basic example.
Example:
using System;
class Program {
static void Main (string[] args) {
string Name = "Jane Doe";
// If you wish to separate the first name from the last name, you can do so by using the substring method
Console.WriteLine("Full Name:");
Console.WriteLine( Name);
Console.WriteLine( "First Name:");
Console.WriteLine(Name.Substring(0,4));
Console.WriteLine("Last Name:");
Console.WriteLine(Name.Substring(5,3));
Console.ReadLine();
}
}
OUTPUT:
Full Name:
Jane Doe
First Name:
Jane
Last Name:
Doe
If there is ever a moment where a coder or programmer wishes to create a substring of a particular string but does not know the index value of a specific character, they can use the IndexOf keyword to help them with this. For instance,
using System;
class Program {
static void Main (string[] args) {
string sentences = "My name is Jane. Nice to meet you";
// If you wish to separate the first sentence from the second sentence, you can do so by using the IndexOf method and adking the program for the index value of the perior '.' that separates the two sentences
Console.WriteLine("Full Statement:");
Console.WriteLine( sentences);
Console.WriteLine( "What is your name?");
Console.WriteLine(sentences.Substring(0, sentences.IndexOf('.')+1));
// We have added '+1' to indicate the program that we wish to include the period '.' within the substring
Console.ReadLine();
}
}
OUTPUT:
Full Statement:
My name is Jane. Nice to meet you
What is your name?
My name is Jane.
Other details about a substring C#:
- If you enter a start index value that is less than 0, you will be thrown an exception (ArgumentOutOfRangeException) and the program will not run.
- The substring, in no way, shape or form modifies the base string from which it has been derived. It simply creates a new string containing data from the base string.
- Substrings are also used extensively in the creation of buttons within the program, where you can automate the program to display one substring when a viewer clicks on the designated button.
Conclusion:
And with that, we now know the basics of how to use substrings in C# programs. Substrings are used by most coders, developers, and programmers to efficiently allocate data within the memory that they have and segregate information effectively. With the use of C# substrings, a coder will essentially eliminate the need to type out similar pieces of information over and over again, and with a few characters of code, can replicate the same date as many times as they wish to. As always, you will get the hang of strings and substrings as you keep trying your hand at it and will continue progressing as you keep using it within your code. Try to minimize excessive code by using these C# string substrings to get access to information without expanding your code. This not only saves memory but also makes the program run faster. Happy Learning!
Do you know? |
---|
1. C# Enum |
2. C# Dictionary |
3. C# Switch Case |
4. C# Substring |
5. C# Foreach |