C# String Substring() (With Examples) - Programiz

Tutorials Courses Python JavaScript TypeScript SQL HTML CSS C C++ Java R Ruby RUST Golang Kotlin Swift C# DSA

Become a certified Python programmer.

ENROLL

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python Examples

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Programiz Pro Logo

Created with over a decade of experience.

  • Learn
  • Practice
  • Compete
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on Programiz Pro Logo Python Basics Python Intermediate C++ Basics C++ Intermediate C++ OOP C Programming Java Basics Java Intermediate Java OOP View all Courses on Programiz Pro Logo Python Challenges JavaScript Challenges Java Challenges C++ Challenges C Challenges View all Challenges on Programiz Pro Logo 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 on Programiz Pro Logo Learn C Learn C++ Learn Java Python JavaScript TypeScript SQL HTML CSS C C++ Java More languages

Become 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 Tutorials

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R Kotlin

Become 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 Examples

C# 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
C# String Substring()

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: fun

Substring() 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

rogramiz

Notice 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

Programiz

Notice 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 blank

Your 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#