GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C# String - Substring() Method

Csharp Tutorial
  • C# - Home
  • C# - Overview
  • C# - Environment
  • C# - Program Structure
  • C# - Basic Syntax
  • C# - Data Types
  • C# - Type Conversion
  • C# - Variables
  • C# - Constants
  • C# - Operators
  • C# - Arithmetic Operators
  • C# - Assignment Operators
  • C# - Relational Operators
  • C# - Logical Operators
  • C# - Bitwise Operators
  • C# - Miscellaneous Operators
  • C# - Operators Precedence
  • C# Conditional Statements
  • C# - Decision Making
  • C# - If
  • C# - If Else
  • C# - Nested If
  • C# - Switch
  • C# - Nested Switch
  • C# - Switch Expressions
  • C# Control Statements
  • C# - Loops
  • C# - For Loop
  • C# - While Loop
  • C# - Do While Loop
  • C# - Nested Loops
  • C# - Break
  • C# - Continue
  • C# - Foreach Loop
  • C# - Goto Statement
  • C# OOP & Data Handling
  • C# - Encapsulation
  • C# - Methods
  • C# - Nullables
  • C# - Arrays
  • C# - Strings
  • C# - Structure
  • C# - Enums
  • C# - Classes
  • C# - Inheritance
  • C# - Polymorphism
  • C# - Operator Overloading
  • C# - Interfaces
  • C# - Namespaces
  • C# - Preprocessor Directives
  • C# - Regular Expressions
  • C# - Custom Exceptions
  • C# - Exception Handling
  • C# - File I/O
  • C# Advanced Tutorial
  • C# - Attributes
  • C# - Reflection
  • C# - Properties
  • C# - Indexers
  • C# - Delegates
  • C# - Events
  • C# - Collections
  • C# - Generics
  • C# - LINQ
  • C# - IEnumerable vs IEnumerator
  • C# - Anonymous Methods
  • C# - Unsafe Codes
  • C# - Tasks and Parallel Programming
  • C# - Multithreading
  • C# - Extension Methods
  • C# - Lambda Expressions
  • C# - Async and Await
  • C# Modern Features
  • C# - Tuples
  • C# - Records
  • C# - Pattern Matching Enhancements
  • C# - Top-level Statements
  • C# - Nullable Reference Types
  • C# - What's New in C# 11 / 12 / 13
  • C# - Global Usings
  • C# - File-Scoped Namespaces
  • C# Practical & Advanced Usage
  • C# - JSON & XML Handling
  • C# - Data Serialization & Deserialization
  • C# - REST API Calls with Httpclient
  • C# - Dependency Injection
  • C# - Unit Testing with NUnit, xUnit & MSTest
  • C# - Package Management with NuGet
  • C# Useful Resources
  • C# - Developer's AI Tools
  • C# - Questions and Answers
  • C# - Cheatsheet
  • C# - Quick Guide
  • C# - Useful Resources
  • C# - Discussion
  • C# - Online Compiler
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C# String - Substring() Method



Previous
Quiz
Next

The C# String Substring() method is used to retrieve a substring from the current instance of a string, starting at a specified character position and continuing to the end of the string. If a specific length is given, the method returns the substring starting from the character position and extending to the given length.

Syntax

Following are the syntax of the C# string Substring() method −

Syntax with only startIndex −

public string Substring (int startIndex);

Syntax with both startIndex and length −

public string Substring(int startIndex, int length);

Parameters

This method accepts the following parameters −

  • startIndex: It represents the starting character position of the substring in the current string's instance.
  • length: It represents the number of characters to include in the substring.

Return Value

This method returns a new string which is a substring of the current string's instance, starting at the specified position and having the specified length.

Example 1: Extract a Substring From String

Here is a basic example of the Substring() method, where we extract a substring that starts with a specified index position and continues at the end of the current instance −

using System;
class Program {
   public static void Main() {
      string text = "Hello, tpians!";
      string Substring = text.Substring(3);
      Console.Write("This is substring: "+ Substring);
   }
}

Output

Following is the output −

This is substring: lo, tpians!

Example 2: Extract Substring within Specified Length

Let's look at another example. Here, we use the substring() method to retrieve a substring from a given starting position to a specified length. −

using System;
class Program {
   public static void Main() {
      string text = "Hello, tpians!";
      string Substring = text.Substring(3, 7);
      Console.Write("This is substring: "+ Substring);
   }
}

Output

Following is the output −

This is substring: lo, tpi

Example 3: Extract First N Characters

In this example, we use the Substring() method to extract the first n character. To extract the first n character, we need to mention startIndex at '0' and then the length −

using System;
class Program {
   public static void Main() {
      string text = "Hello, tutorialspoint";
      string Substring = text.Substring(0, 10);
      Console.Write("First n character: "+ Substring);
   }
}

Output

Following is the output −

True

Example 4: Handle Out-of-Range Scenario

The below example handles the out-of-range scenario. If the start index is greater than the length of the string or '-1', then we get the error −

using System;
class Program {
   public static void Main() {
      string str = "Hi, tutorialspoint";
      try {
         string substring = str.Substring(20);
      } catch (ArgumentOutOfRangeException ex) {
         Console.WriteLine($"Error: {ex.Message}");
      }
   }
}

Output

Following is the output −

Error: startIndex cannot be larger than length of string.
Parameter name: startIndex
csharp_strings.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.