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 - Insert() 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 - Insert() Method



Previous
Quiz
Next

The C# String Insert() method is used to return a new string in which a specified string is inserted at a given index position in this instance of string.

Exception

There are a few exceptions to this method −

  • ArgumentNullException: When the value is null.
  • ArgumentOutOfRangeException: When startIndex is negative or greater than the length of the string object.

Syntax

Following is the syntax of the C# string Insert() method −

public string Insert(int startIndex, string value);

Parameters

This method accepts the following parameters −

  • startIndex: It represents the index position of current string where the new value will be inserted.
  • value: It represents a string to be inserted at the specified position.

Return value

This method returns a new string. That is equal to the current string object. But the string is modified with a value inserted at the specified position.

Example 1: Insert Space Character

The following example inserts a space character in the fourth character position (i.e. the character at index 3) of a string using the Insert() method −

    
using System;
public class Program {
   public static void Main()  {
      String original_str = "tutorialspoint";
      Console.WriteLine("The original string: '{0}'", original_str);
      // modify the string...
      String modified_str = original_str.Insert(3, " ");
      Console.WriteLine("The modified string: '{0}'", modified_str);
   }
}

Output

Following is the output −

The original string: 'tutorialspoint'
The modified string: 'tut orialspoint'

Example 2: Insert a String

Let us look at another example of the Insert() method. Here, we define a string, and between the string's words, we are inserting another one −

using System;
public class Program {
   public static void Main() {
      String str = "Hello World!";
      String to_be_insert = "tutorialspoint";
      Console.WriteLine("The original string: '{0}'", str);
      // modify the string...
      String modified_str = str.Insert(3, to_be_insert);
      Console.WriteLine("The modified string: '{0}'", modified_str);
   }
}

Output

Following is the output −

The original string: 'Hello World!'
The modified string: 'Heltutorialspointlo World!'

Example 3: Insert at the Beginning of a String

In this example, we use the Insert() method to add a string at the beginning of the string, specifically at index position 0 −

using System;
class Program {
   static void Main() {
      string str = "World!";
      string modified_str = str.Insert(0, "Hello ");
      Console.WriteLine(modified_str);
   }
}

Output

Following is the output −

Hello World!

Example 4: Insert at the End of a String

In this example, we use the Insert() method to insert a string at the end of the current string object −

using System;
class Program {
   static void Main() {
      string str = "Hello";
      string to_be_insert = " tutorialspoint!";
      // modified the string
      string modified_str = str.Insert(str.Length, to_be_insert);
      Console.WriteLine(modified_str);
   }
}

Output

Following is the output −

Hello tutorialspoint!
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.