- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Substring() Method
The Substring() method in C# is used to retrieve a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
Syntax
The syntax is as follows -
public string Substring (int begnIndex); public string Substring (int begnIndex, int len);
Above, begnIndex is the zero-based starting character position of a substring in this instance. The len parameter is the number of the substring to retrieve
Example
Let us now see an example -
using System; public class Demo { public static void Main(String[] args) { string str1 = "Katherine"; string str2 = "PQRS"; Console.WriteLine("String1 = "+str1); Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant()); Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4)); Console.WriteLine("
String2 = "+str2); Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant()); Console.WriteLine("String2 Substring from index2 = " + str2.Substring(2)); } }
Output
String1 = Katherine String1 ToUpperInvariant = KATHERINE String1 Substring from index4 = erine String2 = PQRS String2 ToUpperInvariant = pqrs String2 Substring from index2 = RS
Example
Let us now see another example -
using System; public class Demo { public static void Main(String[] args) { string str1 = "Notebook"; string str2 = "Ultrabook"; Console.WriteLine("String1 = "+str1); Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant()); Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4)); Console.WriteLine("
String2 = "+str2); Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant()); Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5)); } }
Output
String1 = Notebook String1 ToUpperInvariant = NOTEBOOK String1 Substring from index4 = book String2 = Ultrabook String2 ToUpperInvariant = ultrabook String2 Substring from index2 = Ultra
Advertisements