2.2.3.15.18 string::Insert

Description

This overloaded member function inserts a char or substring at the given index within the string.

Syntax

int Insert( int nIndex, char ch )


int Insert( int nIndex, LPCTSTR pstr )

Parameters

nIndex
[input]The index of the character before which the insertion will take place.
ch
[input]The character to be inserted.


nIndex
[input]The index of the character before which the insertion will take place.
pstr
[input] A pointer to the substring to be inserted.

Return

The length of the changed string.

Examples

EX1

//Insert a character
void string_Insert_ex1()
{
    string str(" love summer");
    str.Insert(0,'I');
    printf("After insertion: the string is \"%s\".\n", str);
    //After insertion: the string is "I love summer".
}


EX2

//Insert a string
void string_Insert_ex2()
{
    string str("I summer");
    str.Insert(2,"love ");
	printf("After insertion: the string is \"%s\".\n", str);
	//After insertion: the string is "I love summer".
}

Remark

This overloaded member function inserts a substring at the given index within the string. The nIndex parameter identifies the first character that will be moved to make room for the substring. If nIndex is zero, the insertion will occur before the entire string. If nIndex is higher than the length of the string, the function will concatenate the string and the substring.

See Also

string::Delete

Header to Include

origin.h