String.Insert(Int32, String) String.Insert(Int32, String) String.Insert(Int32, String) String.Insert(Int32, String) Method

정의

이 인스턴스의 지정된 인덱스 위치에 지정한 문자열이 삽입되는 새 문자열을 반환합니다.Returns a new string in which a specified string is inserted at a specified index position in this instance.

public:
 System::String ^ Insert(int startIndex, System::String ^ value);
public string Insert (int startIndex, string value);
member this.Insert : int * string -> string
Public Function Insert (startIndex As Integer, value As String) As String

매개 변수

startIndex
Int32 Int32 Int32 Int32

삽입의 0부터 시작하는 인덱스 위치입니다.The zero-based index position of the insertion.

value
String String String String

삽입할 문자열입니다.The string to insert.

반환

이 인스턴스와 동일하지만 value 위치에 startIndex가 삽입된 새 문자열입니다.A new string that is equivalent to this instance, but with value inserted at position startIndex.

예외

startIndex 음수이거나 이 인스턴스의 길이보다 큽니다.is negative or greater than the length of this instance.

예제

다음 예제에서는 네 번째 문자 위치에 있는 (인덱스 3에는 문자) 문자열의 공백 문자를 삽입합니다.The following example inserts a space character in the fourth character position (the character at index 3) of a string.

using System;

public class Example
{
   public static void Main()
   {
      String original = "aaabbb";
      Console.WriteLine("The original string: '{0}'", original);
      String modified = original.Insert(3, " ");
      Console.WriteLine("The modified string: '{0}'", modified);
   }
}
// The example displays the following output:
//     The original string: 'aaabbb'
//     The modified string: 'aaa bbb'
Public Module Example
   Public Sub Main()
      Dim original As String = "aaabbb"
      Console.WriteLine("The original string: '{0}'", original)
      Dim modified As String = original.Insert(3, " ")
      Console.WriteLine("The modified string: '{0}'", modified)
   End Sub
End Module
' The example displays the following output:
'     The original string: 'aaabbb'
'     The modified string: 'aaa bbb'

다음 콘솔 응용 프로그램 사용자에 게 두 동물을 설명 하기 위해 하나 이상의 형용사를 입력 합니다. 라는 메시지가 표시 됩니다.The following console application prompts the users to enter one or more adjectives to describe two animals. 그런 다음 호출 하 여 Insert 문자열로 사용자가 입력 한 텍스트를 삽입 하는 방법입니다.It then calls the Insert method to insert the text entered by the user into a string.

using namespace System;

int main()
{
   String^ animal1 = "fox";
   String^ animal2 = "dog";
   String^ strTarget = String::Format( "The {0} jumps over the {1}.", animal1, animal2 );
   Console::WriteLine( "The original string is:{0}{1}{0}", Environment::NewLine, strTarget );
   Console::Write( "Enter an adjective (or group of adjectives) to describe the {0}: ==> ", animal1 );
   String^ adj1 = Console::ReadLine();
   Console::Write( "Enter an adjective (or 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 );
}
// Output from the example might appear as follows:
//       The original string is:
//       The fox jumps over the dog.
//       
//       Enter an adjective (or group of adjectives) to describe the fox: ==> bold
//       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy
//       
//       The final string is:
//       The bold fox jumps over the lazy dog.
using System;

public class Example {
    public static void Main() 
    {
        string animal1 = "fox";
        string animal2 = "dog";

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

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

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

        Console.Write("Enter an adjective (or 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);
    }
}
// Output from the example might appear as follows:
//       The original string is:
//       The fox jumps over the dog.
//       
//       Enter an adjective (or group of adjectives) to describe the fox: ==> bold
//       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy
//       
//       The final string is:
//       The bold fox jumps over the lazy dog.
Public Class Example
    Public Shared Sub Main()
        Dim animal1 As String = "fox"
        Dim animal2 As String = "dog"
        Dim strTarget As String = String.Format("The {0} jumps over the {1}.", 
                                                animal1, animal2)
        
        Console.WriteLine("The original string is: {0}{1}{0}", 
                          Environment.NewLine, strTarget)
        
        Console.Write("Enter an adjective (or group of adjectives) " +
                      "to describe the {0}: ==> ", animal1)
        Dim adj1 As String = Console.ReadLine()
        
        Console.Write("Enter an adjective (or 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 
End Class 
' Output from the example might appear as follows:
'       The original string is:
'       The fox jumps over the dog.
'       
'       Enter an adjective (or group of adjectives) to describe the fox: ==> bold
'       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy
'       
'       The final string is:
'       The bold fox jumps over the lazy dog.

설명

하는 경우 startIndex 가이 인스턴스의 길이 같고 value 이 인스턴스의 끝에 추가 됩니다.If startIndex is equal to the length of this instance, value is appended to the end of this instance.

참고

이 메서드는 현재 인스턴스의 값을 수정 하지 않습니다.This method does not modify the value of the current instance. 새 문자열을 반환 하는 대신 value 가 현재 인스턴스에 삽입 합니다.Instead, it returns a new string in which value is inserted into the current instance.

반환 값의 예를 들어 "abc".Insert(2, "XYZ") "abXYZc" 됩니다.For example, the return value of "abc".Insert(2, "XYZ") is "abXYZc".

적용 대상

추가 정보