C# String Substring() (With Examples) - Programiz
Maybe your like
Become a certified Python programmer.
ENROLLPopular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning PythonPopular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python ExamplesReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View allCreated with over a decade of experience.
- Learn
- Practice
- Compete
Certification Courses
Created with over a decade of experience and thousands of feedback.
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses onBecome a certified Python programmer.
Try Programiz PRO!Popular Tutorials
Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python TutorialsReference Materials
Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R KotlinBecome a certified Python programmer.
Try Programiz PRO!Popular Examples
Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python ExamplesC# String Methods
- C# String Format()
- C# String Split()
- C# String Substring()
- C# String Compare()
- C# String Replace()
- C# String Contains()
- C# String Join()
- C# String Concat()
- C# String Trim()
- C# String Equals()
- C# String IndexOf()
- C# String LastIndexOf()
- C# String Remove()
- C# String ToUpper()
- C# String ToLower()
- C# String EndsWith()
- C# String StartsWith()
- C# String ToCharArray()
- C# String PadLeft()
- C# String PadRight()
C# Tutorials
- C# String Replace()
- C# String ToCharArray()
- C# String IndexOf()
- C# String EndsWith()
- C# String LastIndexOf()
- C# String
The Substring() method returns a substring from the given string.
Example
using System; namespace CsharpString { class Test { public static void Main(string[] args) { string text = "C# is fun"; // Returns substring of length 3 from index 6 Console.WriteLine(text.Substring(6, 3)); Console.ReadLine(); } } } // Output: funSubstring() Syntax
The syntax of the string Substring() method is:
Substring(int startIndex, int length)Here, Substring() is a method of class String.
Substring() Parameters
The Substring() method takes the following parameters:
- startIndex - the beginning index of the substring
- length - (optional) - length of the substring
Substring() Return Value
The Substring() method returns a substring from the given string.
Example 1: C# Substring() Without Length
using System; namespace CsharpString { class Test { public static void Main(string[] args) { string text = "Programiz"; // Returns substring from the second character string result = text.Substring(1); Console.WriteLine(result); Console.ReadLine(); } } }Output
rogramizNotice this line in the above example:
string result = text.Substring(1);The code text.Substring(1) returns the substring from the second character of "Programiz".
Example 2: C# Substring() With Length
using System; namespace CsharpString { class Test { public static void Main(string[] args) { string text = "Programiz is for programmers"; // Returns substring of length 9 from the first character string result = text.Substring(0, 9); Console.WriteLine(result); Console.ReadLine(); } } }Output
ProgramizNotice this line in the above example:
string result = text.Substring(0, 9);Here,
- 0 (the first character of text) is the beginning index of the substring
- 9 is the length of the substring
This gives us the substring "Programiz".
Example 3: C# Substring() Before Specific Character
using System; namespace CsharpString { class Test { public static void Main(string[] args) { string text = "C#. Programiz"; // Returns substring from index 0 to index before '.' string result = text.Substring(0, text.IndexOf('.')); Console.WriteLine(result); Console.ReadLine(); } } }Output
C#Here, text.IndexOf('.') gives the length of the substring, which is the index of '.'.
Previous Tutorial: C# String Split() Next Tutorial: C# String Compare() Share on: Did you find this article helpful?Sorry about that.
How can we improve it? Feedback * Leave this field blankYour builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
Try Programiz PRO- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
Tag » What Is Substring In C#
-
Substring In C# - C# Corner
-
C# | Substring() Method - GeeksforGeeks
-
String.Substring Method (System) | Microsoft Learn
-
C# Substring() Method - Tutorialspoint
-
How To Use C# String Substring
-
C# Substring Examples - Dot Net Perls
-
C# Substring 문자열 자르기 - 개발인생
-
How To Get A Substring From A String In C#
-
C# String SubString() Method - Javatpoint
-
082 - How To Use C# String Substring - YouTube
-
What Is Substring In C# With Example? [60 Answers Found]
-
How To Check If String Contains Specified Substring In C#?
-
Visual C# .net Strings - Substring - Home And Learn Courses
-
JavaScript String Substring() Method - W3Schools