insert

Summary

Inserts a substring at the specified position in the string.

Usage

public string insert(int startIndex, string value)

Returns

A new string value with the specified substring inserted at the specified position.

Parameters

startIndex

The zero-based position in the current string to insert the new substring. This value can be negative to insert the new substring at a position relative to the end of the current string.

value

The substring to insert.

Description

Inserts the specified substring at the specified position in the current string. If the specified position is a negative index, the new substring will be inserted relative to the current string's ending index.

Examples

Basic Usage
1
2
3
import System;
 
Console.log("def".insert(0, "abc")); // "abcdef"
Inserting from the end of the string
1
2
3
import System;
 
Console.log("foo".insert(-1, "bar")); // "fobaro"
Inserting in the middle of the string
1
2
3
4
5
6
import System;
 
string str1 = "adef";
string str2 = str1.insert(1, "bc");
 
Console.log(str2); // "abcdef"

Share

HTML | BBCode | Direct Link