Share via


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입니다.

반환 값

이 인스턴스와 동일하지만 startIndex 위치에 value가 삽입된 새 String을 반환합니다.

예외

예외 형식 조건

ArgumentNullException

value가 Null 참조(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