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
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec
14 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
6 min read