Advertisement
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. |
//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);
}
}
Original String is 0123456789
Substring extracted from index 3 to the end :3456789
Advertisement
//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) );
}
}
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
//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);
}
}
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!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement