String.Insert()方法以及C#中的示例
C#方法String.Insert()
String.Insert()方法用于在指定索引处的存在字符串中插入字符串并返回新字符串。
语法:
public string String.Insert(int index, string value);
用“this”字符串调用该方法,即我们必须在其中插入字符串的字符串。
Parameter(s):
index–表示当前字符串的索引/位置,要在其中插入新的字符串值。
value–要插入的新字符串。
返回值:
string–返回一个新字符串,其中包含在给定索引处插入的字符串。
示例
Input:
string str = "nhooo";
string str1 = " programming ";
Function call
str.Insert(7, str1);
Output:
Include programming HelpC#使用String.Insert()方法将字符串转换为字符数组的示例
范例1:
using System;
class nhooo
{
static void Main()
{
// 声明字符串变量
string str = "nhooo";
// 在“包含”和“帮助”之间插入空格
string new_string = str.Insert(7, " ");
Console.WriteLine("str: " + str);
Console.WriteLine("new_string: " + new_string);
}
}输出结果
str: nhooo new_string: Include Help
范例2:
using System;
class Nhooo
{
static void Main()
{
// 声明字符串变量
string str = "nhooo";
string str1 = " programming ";
// 在“包含”之后插入str1"Include"
string new_string = str.Insert(7, str1);
Console.WriteLine("str: " + str);
Console.WriteLine("new_string: " + new_string);
}
}输出结果
str: nhooo new_string: Include programming Help