Advertisement



< Prev
Next >



C# Substring() method




As the name of the method says, the Substring() method of String class is used to extract a substring from the invoked String object, this substring starts at a particular index. There are two forms of Substring() method, let's discuss each of those with example one by one.




Some overloads of Substring() method



Methods Description
Substring(int startIndex)
This method returns a substring String value which starts at startIndex and ends at the last index of the invoked String object.

Substring(int startIndex, int length)
This method returns a substring String value which starts at startIndex and has a specified length within the invoked String object.






Note :


The startIndex int value is a zero-based index, hence the first index is counted as 0.




An example of Substring(int startIndex)


Here in program below, we have a String object and we are extracting a substring out of it by calling the first form of Substring() method i.e. substring(int startIndex) method.

//C# Program to find a substring of a String, starting at a specific index.


using System;

class StringSubstring
{
public static void Main()
{
	//Creating a String object
	String str1= "0123456789";

	//Calling the Substring() method on the String object
	//to extract substring starting at index 3
	String substring  = str1.Substring(3);

	//Printing the original String
	Console.WriteLine("Original String is "+ str1);
	
	//Printing the extracted substring
	Console.WriteLine("Substring extracted from index 3 to the end :"+ substring);
}
}

Output is :

Original String is 0123456789
Substring extracted from index 3 to the end :3456789


Program Analysis




Note: Because startIndex is an zero-based index int value, that's why the first index of a string is counted as 0.


Advertisement




Example of Substring(int startIndex, int length)


In the next example, we have a String object and we are extracting a substring out of it by calling the second form of Substring() method i.e. Substring(int startIndex, int length) method, which returns a substring String value which starts at startIndex and has a specified length within the invoked String object.

//C# Example of Substring(int startIndex, int length)


using System;

class StringSubstring
{
public static void Main()
{
	//Creating a String object
	String str1= "Always think positive!";


	//Printing the original String
	Console.WriteLine("Original String is:"+ str1);


	//Calling the Substring() method on the String object
	//to extract substring starting at index 2 and has a length of 5 characters
	Console.WriteLine("Substring from index 2 and a length of 5:"+ str1.Substring(2,5));


	//Calling the Substring() method on the String object
	//to extract substring starting at index 6 and has a length of 2 characters
	Console.WriteLine("Substring from index 6 and a length of 2:"+ str1.Substring(6,2));


	//Calling the Substring() method on the String object
	//to extract substring starting at index 0 and has a length of 5 characters
	Console.WriteLine("Substring from index 0 and a length of 5:"+ str1.Substring(0,5) );
	

	//Calling the Substring() method on the String object
	//to extract substring starting at index 4 and has a length of 4 characters
	Console.WriteLine("Substring from index 4 and a length of 4:"+ str1.Substring(4,4) );
}
}


Output is :


Original String is:Always think positive!
Substring from index 2 and a length of 5:ways
Substring from index 6 and a length of 2: t
Substring from index 0 and a length of 5:Alway
Substring from index 4 and a length of 4:ys t





The Substring() method does not modify the original content in a String object


Although we have already explained how String objects are immutable i.e. they cannot be modified but still for those who would like it to proved to them. Let us show you an example to display how either version of the Substring() only returns a substring copy from the invoked String object i.e. it does not modify the original content of a String object. Let's understand this by an example.

//C# The Substring() method doesn't modify the invoked String.


using System;

class StringSubstring
{
public static void Main()
{
	//Creating a String object
	String str1= "Always think positive!";


	//Printing the original value of the first String
	Console.WriteLine("Original value of the first String: "  + str1);


	//Calling the Substring() method on the String object
	//to extract substring starting at index 3, which removes leading and trailing white spaces
	Console.WriteLine("Substring from index 3 to the end: " + str1.Substring(3));


	//Calling the Substring() method on the String object
	//to extract substring starting at index 2 and has a length of 5 characters
	Console.WriteLine("Substring from index 2 and a length of 5:"+ str1.Substring(2,5));


	//Printing the value of the String after calling Substring() on it.
	Console.WriteLine("First string after calling the Substring() method : " + str1);
}
}


Output is :


Original value of the first String: Always think positive!
Substring from index 3 to the end: ays think positive!
Substring from index 2 and a length of 5:ways
First string after calling the Substring() method : Always think positive!





Please share this article -





< Prev
Next >
< C# Intern() Method
C# ToString() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement