String.Insert 方法

在此实例中的指定索引位置插入一个指定的 String 实例。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function Insert ( _
    startIndex As Integer, _
    value As String _
) As String
用法
Dim instance As String
Dim startIndex As Integer
Dim value As String
Dim returnValue As String

returnValue = instance.Insert(startIndex, value)
public string Insert (
    int startIndex,
    string value
)
public:
String^ Insert (
    int startIndex, 
    String^ value
)
public String Insert (
    int startIndex, 
    String value
)
public function Insert (
    startIndex : int, 
    value : String
) : String

参数

  • startIndex
    此插入的索引位置。
  • value
    要插入的 String

返回值

此实例的一个新 String 等效项,但在位置 startIndex 处插入 value。

异常

异常类型 条件

ArgumentNullException

value 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

startIndex 为负,或大于此实例的长度。

备注

如果 startIndex 等于此实例的长度,则将 value 追加到此实例的末尾。

例如,"abc".Insert(2, "XYZ") 的返回值为“abXYZc”。

示例

下面的控制台应用程序提供了 Insert 方法的简单使用说明。

Imports System

Public Class InsertTest
    
    Public Shared Sub Main()
        Dim animal1 As String = "fox"
        Dim animal2 As String = "dog"
        Dim strTarget As String = [String].Format("The {0} jumped over the {1}.", animal1, animal2)
        
        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget)
        
        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1)
        Dim adj1 As String = Console.ReadLine()
        
        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2)
        Dim adj2 As String = Console.ReadLine()
        
        adj1 = adj1.Trim() + " "
        adj2 = adj2.Trim() + " "
        
        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1)
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2)
        
        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget)
    End Sub 'Main
End Class 'InsertTest
using System;

public class InsertTest {
    public static void Main() {

        string animal1 = "fox";
        string animal2 = "dog";

        string strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2);

        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget);

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1);
        string adj1 = Console.ReadLine();

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2);    
        string adj2 = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);

        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget);
    }
}
using namespace System;
int main()
{
   String^ animal1 = "fox";
   String^ animal2 = "dog";
   String^ strTarget = String::Format( "The {0} jumped over the {1}.", animal1, animal2 );
   Console::WriteLine( "The original string is:{0}{1}{0}", Environment::NewLine, strTarget );
   Console::Write( "Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1 );
   String^ adj1 = Console::ReadLine();
   Console::Write( "Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2 );
   String^ adj2 = Console::ReadLine();
   adj1 = String::Concat( adj1->Trim(), " " );
   adj2 = String::Concat( adj2->Trim(), " " );
   strTarget = strTarget->Insert( strTarget->IndexOf( animal1 ), adj1 );
   strTarget = strTarget->Insert( strTarget->IndexOf( animal2 ), adj2 );
   Console::WriteLine( " {0}The final string is: {0} {1}", Environment::NewLine, strTarget );
}
import System.*;

public class InsertTest
{
    public static void main(String[] args)
    {
        String animal1 = "fox";
        String animal2 = "dog";

        String strTarget = String.Format("The {0} jumped over the {1}.", 
            animal1, animal2);
        Console.WriteLine("The original string is:{0}{1}{0}", 
            Environment.get_NewLine(), strTarget);
        Console.Write("Please enter an adjective (or a group of adjectives) "
            + "to describe the {0}: ==> ", animal1);
        String adj1 = Console.ReadLine();

        Console.Write("Please enter an adjective (or a group of adjectives) "
            + "to describe the {0}: ==> ", animal2);
        String adj2 = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);
        Console.WriteLine("{0}The final string is:{0}{1}", 
            Environment.get_NewLine(), strTarget);
    } //main
} //InsertTest
import System;

public class InsertTest {
    public static function Main() : void {

        var animal1 : String = "fox";
        var animal2 : String = "dog";

        var strTarget : String = String.Format("The {0} jumped over the {1}.", animal1, animal2);

        Console.WriteLine("The original string is:{0}{1}{0}", Environment.NewLine, strTarget);

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal1);
        var adj1 : String = Console.ReadLine();

        Console.Write("Please enter an adjective (or a group of adjectives) to describe the {0}: ==> ", animal2);    
        var adj2 : String = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);

        Console.WriteLine("{0}The final string is:{0}{1}", Environment.NewLine, strTarget);
    }
}
InsertTest.Main();

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

String 类
String 成员
System 命名空间
Int32 结构
Concat
CopyTo
Insert
Join
Remove
Replace
Split
Substring
Trim