Advertisement



< Prev
Next >



C# Insert() method




As the name of the method goes, the Insert() method of String class inserts a specific String at a particular index(zero-based index) of the invoked String.




Explaining the Insert() method



Methods Description
Insert(int index, String str)
This method inserts a specific String at a particular index(zero-based index) of the invoked String.






Example of Insert(int, String)


In the upcoming code, we have a String object initialized to a value Hello World and we are going to insert a String object this String object starts with a specific String value or not.

//C# Insert() method of String.

using System;

class StringStartsWith
{
public static void Main()
{
	//Creating a String object
	String str1 = "Hello world!";
	
	//Printing the String object
	Console.WriteLine("String is: "  + str1);

	//Calling the Insert() method 
	Console.WriteLine("Insert() method to insert 'there' at index 6: " + str1.Insert(6, "there "));

	//Calling the Insert() method
	Console.WriteLine("Insert() method to insert 'XYZ' at index 2: " + str1.Insert(2, "XYZ"));

	//Calling the Insert Method
	Console.WriteLine("Insert() method to insert 'ting tong' at index 7: " + str1.Insert(7, "ting tong"));
	
	//Calling the Insert Method
	Console.WriteLine("Insert() method to insert '/Hi There' at index 5: " + str1.Insert(5, "/Hi there"));
}
}


Output is :


String is: Hello world!
Insert() method to insert 'there' at index 6: Hello there world!
Insert() method to insert 'XYZ' at index 2: HeXYZllo world!
Insert() method to insert 'ting tong' at index 7: Hello wting tongorld!
Insert() method to insert '/Hi There' at index 5: Hello/Hi there world!

Note: In all the method calls to Insert() method, the index passedto it is a zero-based index.



The ArgumentOutOfRangeException when calling the Insert() method


The Insert() method throws an ArgumentOutOfRangeException when it is passed an out-of-range index when calling it. Let us see how.

//C# Exception when calling the Insert() method of String.

using System;

class StringStartsWith
{
public static void Main()
{
	//Creating a String object
	String str1 = "HAHAHAHA!";
	
	//Printing the String object
	Console.WriteLine("String is: "  + str1);

	//Calling the Insert() method 
	Console.WriteLine("Insert() method to insert 'hehehe' at index 2: " + str1.Insert(2, "hehehe"));

	//Calling the Insert() method
	Console.WriteLine("Insert() method to insert 'Ciao' at index 10: " + str1.Insert(10, "Ciao!"));
}
}


Output is :


String is: HAHAHAHA!
Insert() method to insert 'hehehe' at index 2: HAheheheHAHAHA!

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: startIndex
   at System.String.Insert(Int32 startIndex, String value)
   at StringStartsWith.Main()








Advertisement




The Insert() does not modify the content of invoked String object


Strings are immutable and the Insert() method cannot modify the original contents of the invoked String.

//C# Insert() method of String.

using System;

class StringStartsWith
{
public static void Main()
{
	//Creating a String object
	String str1 = "When you smile, the world smiles with you";
	
	//Printing the String object
	Console.WriteLine("String is: "  + str1);

	//Calling the Insert() method 
	Console.WriteLine("Insert() method to insert 'right said' at index 6: " + str1.Insert(4, "right said "));

	///Printing the String's value after calling Insert() method on it.
	Console.WriteLine("String's value after Insert() is called : " + str1);
}
}


Output -


String is: When you smile, the world smiles with you
Insert() method to insert 'right said' at index 6: Whenright said  you smile, the world smiles with you
String's value after Insert() is called : When you smile, the world smiles with you


Program Analysis


We have initialized a String object with value When you smile, the world smiles with you Post calling the Insert() method, the String object still has the same value because the String objects are immutable and that's why the Insert() method only return a modified copy of the invoked String, without changing the original String.






Please share this article -




< Prev
Next >
< C# Replace method
C# Intern() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement