In C#,
Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows:
- String.Substring(Int32) Method
- String.Substring(Int32, Int32) Method
String.Substring Method (startIndex)
This method is used to retrieves a substring from the current instance of the string. The parameter "startIndex" will specify the starting position of substring and then substring will continue to the end of the string.
Syntax:
public string Substring(int startIndex)
- Parameter: This method accept one parameter "startIndex". This parameter will specify the starting position of the substring which has to be retrieve. The type of this parameter is System.Int32.
- Return Value: This method will return the substring which begins from startIndex and continues to the end of the string. The return value type is System.String.
Exception: If startIndex is less than zero or greater than the length of current instance then it will arise
ArgumentOutOfRangeException.
Example:
Input : str = "GeeksForGeeks"
str.Substring(5);
Output: ForGeeks
Input : str = "GeeksForGeeks"
str.Substring(8);
Output: Geeks
Below program illustrate the above-discussed method:
Csharp
// C# program to demonstrate the
// String.Substring Method (startIndex)
using System;
class Geeks {
// Main Method
public static void Main()
{
// define string
String str = "GeeksForGeeks";
Console.WriteLine("String : " + str);
// retrieve the substring from index 5
Console.WriteLine("Sub String1: " + str.Substring(5));
// retrieve the substring from index 8
Console.WriteLine("Sub String2: " + str.Substring(8));
}
}
Output:
String : GeeksForGeeks
Sub String1: ForGeeks
Sub String2: Geeks
String.Substring Method (int startIndex, int length)
This method is used to extract a substring that begins from specified position describe by parameter
startIndex and has a specified
length. If startIndex is equal to the length of string and parameter length is zero, then it will return nothing substring.
Syntax :
public string Substring(int startIndex, int length)
- Parameter: This method accept two parameters "startIndex" and length. First parameter will specify the starting position of the substring which has to be retrieve and second parameter will specify the length of the substring. The type of both the parameters is System.Int32.
- Return Value: This method will return the substring which begins from specified position and substring will have a specified length. The return value type is System.String.
Exception: This method can arise
ArgumentOutOfRangeException in two conditions:
- if the parameters startIndex or length is less than zero.
- If startIndex + length indicates a position which is not within current instance.
Example:
Input : str = "GeeksForGeeks"
str.Substring(0,8);
Output: GeeksFor
Input : str = "GeeksForGeeks"
str.Substring(5,3);
Output: For
Input : str = "Geeks"
str.Substring(4,0);
Output:
Below program illustrate the above-discussed method:
Csharp
// C# program to demonstrate the
// String.Substring Method
// (int startIndex, int length)
using System;
class Geeks {
// Main Method
public static void Main()
{
// define string
String str = "GeeksForGeeks";
Console.WriteLine("String : " + str);
// retrieve the substring from index 0 to length 8
Console.WriteLine("Sub String1: " + str.Substring(0, 8));
// retrieve the substring from index 5 to length 3
Console.WriteLine("Sub String2: " + str.Substring(5, 3));
}
}
Output:
String : GeeksForGeeks
Sub String1: GeeksFor
Sub String2: For
References:
Similar Reads
std::string::compare() in C++ The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
substr() in C++ In C++, the string substr() function is used to extract a substring from the given string. It generates a new string with its value initialized to a copy of a sub-string of the given string.Example:C++#include <iostream> #include <string> using namespace std; int main() { // Take any str
3 min read
JavaScript String substr() Method The substr() method in JavaScript extracts a portion of a string, starting from a specified index position and extending for a given number of characters.This method was traditionally used to extract a part of a string, however, the method is now considered outdated, and the substring() or slice() m
4 min read
Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
PHP substr() function The substr() is a built-in function in PHP that is used to extract a part of string. Syntax: substr(string_name, start_position, string_length_to_cut) Parameters: The substr() function allows 3 parameters or arguments out of which two are mandatory and one is optional. string_name: In this parameter
2 min read
Practice questions on Strings String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = âstringâ), both of which can be access
4 min read
std::string::find_last_not_of in C++ It searches the string for the first character, from the end of the string, that does not match any of the characters specified in its arguments. Return value : Index of first unmatched character when successful or string::npos if no such character found. Syntax 1: Search for the last character that
6 min read
strcspn() in C The strcspn() is a part of the C standard library, it is defined in the <string.h> header file in C. The strcsspn function is used to find the number of characters in the given string before the 1st occurrence of a character from the defined set of characters or string. Syntax of strcspn()Cstr
3 min read
strlen() function in c The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file. It doesn't count the null character '\0'. Syntax of C strlen() The syntax of strlen() function in C is as follows: size_t strlen(const char* str);Parameters The strlen() func
1 min read
isprint() in C The C isprint() function is used to check if a character passed as the argument is a printable character or not. isprint() is defined in the <ctype.h> standard library header file.Printable characters in C are:digits ( 0123456789 )uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )lowercase lette
2 min read