String.Insert(Int32, String) 메서드

정의

이 인스턴스의 지정된 인덱스 위치에 지정한 문자열이 삽입되는 새 문자열을 반환합니다.

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

삽입의 0부터 시작하는 인덱스 위치입니다.

value
String

삽입할 문자열입니다.

반환

String

이 인스턴스와 동일하지만 value 위치에 startIndex가 삽입된 새 문자열입니다.

예외

valuenull입니다.

startIndex가 음수이거나 이 인스턴스의 길이보다 큽니다.

예제

다음 예제에서는 문자열의 네 번째 문자 위치(인덱스 3의 문자)에 공백 문자를 삽입합니다.

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'

다음 콘솔 애플리케이션 사용자에 게 두 동물을 설명 하기 위해 하나 이상의 형용사를 입력 합니다. 라는 메시지가 표시 됩니다. 그런 다음 Insert 메서드를 호출하여 사용자가 입력한 텍스트를 문자열에 삽입합니다.

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 가 이 인스턴스의 끝에 추가됩니다.

참고

이 메서드는 현재 인스턴스의 값을 수정 하지 않습니다. 대신 현재 value 인스턴스에 삽입되는 새 문자열을 반환합니다.

예를 들어 의 반환 "abc".Insert(2, "XYZ") 값은 "abXYZc"입니다.

적용 대상

추가 정보