In C#, the Insert()
method is used to insert a specified string into another string at a specified index position. It returns a new string with the inserted substring, leaving the original string unchanged. The syntax for using the Insert()
method is as follows:
string result = originalString.Insert(index, stringToInsert);
Here’s a breakdown of the parameters:
originalString
: This is the original string into which you want to insert another string.index
: This is the index position at which you want to insert the string. The index is zero-based, so the first character has an index of 0.stringToInsert
: This is the string that you want to insert into the original string.
Here’s an example to illustrate the usage of the Insert()
method:
string originalString = "Hello, world!"; string stringToInsert = " beautiful"; int index = 6; string result = originalString.Insert(index, stringToInsert); Console.WriteLine(result); // Output: Hello, beautiful world!
In the example above, we insert the string ” beautiful” into the original string at index position 6, resulting in the string “Hello, beautiful world!”.
Signature:
Certainly! Here’s the signature of the Insert()
method in C#:
public string Insert(int startIndex, string value)
This signature indicates that the Insert()
method returns a string (string
) and takes two parameters:
startIndex
(int
): The index position at which thevalue
string should be inserted into the original string.value
(string
): The string to be inserted into the original string.
Remember that the Insert()
method returns a new string with the inserted substring, and the original string remains unchanged.
Return:
The Insert()
method in C# returns a new string with the inserted substring. It does not modify the original string on which the method is called. The return value is the resulting string after the insertion.
Here’s an example:
string originalString = "Hello, world!"; string stringToInsert = " beautiful"; int index = 6; string result = originalString.Insert(index, stringToInsert);
In this example, the Insert()
method is called on the originalString
with the index
and stringToInsert
as parameters. The resulting string after the insertion is assigned to the result
variable.
So, the return value of the Insert()
method is the modified string where the substring has been inserted.
C# String Insert() Method Example:
Certainly! Here’s an example demonstrating the usage of the Insert()
method in C#:
using System; class Program { static void Main() { string originalString = "Hello, world!"; string stringToInsert = " beautiful"; int index = 6; string result = originalString.Insert(index, stringToInsert); Console.WriteLine(result); // Output: Hello, beautiful world! } }
In this example, we have a string originalString
set to “Hello, world!”. We want to insert the string ” beautiful” at index position 6.
The Insert()
method is called on the originalString
with the index
and stringToInsert
as arguments. It returns a new string with the inserted substring, which is then assigned to the result
variable.
Finally, we output the result
string to the console, which will display “Hello, beautiful world!” as the output.