Advertisement
Methods | Description |
---|---|
Insert(int index, String str) | This method inserts a specific String at a particular index(zero-based index) of the invoked String. |
//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"));
}
}
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!
//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!"));
}
}
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
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement