İngilizce dilinde oku

Aracılığıyla paylaş


String.Insert(Int32, String) Yöntem

Tanım

Bu örnekte belirtilen bir dizin konumunda belirtilen dizenin eklendiği yeni bir dize döndürür.

C#
public string Insert(int startIndex, string value);

Parametreler

startIndex
Int32

Eklemenin sıfır tabanlı dizin konumu.

value
String

Eklenecek dize.

Döndürülenler

Bu örneğe eşdeğer, ancak value konumunda startIndexeklenmiş yeni bir dize.

Özel durumlar

value, null değeridir.

startIndex bu örneğin uzunluğundan negatif veya daha büyük.

Örnekler

Aşağıdaki örnek, bir dizenin dördüncü karakter konumuna (dizin 3'teki karakter) bir boşluk karakteri ekler.

C#
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'

Aşağıdaki konsol uygulaması, kullanıcılardan iki hayvanı tanımlamak için bir veya daha fazla sıfat girmesini ister. Ardından kullanıcı tarafından girilen metni bir dizeye eklemek için yöntemini çağırır Insert .

C#
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.

Açıklamalar

Bu örneğin uzunluğuna eşitse startIndex , value bu örneğin sonuna eklenir.

Not

Bu yöntem, geçerli örneğin değerini değiştirmez. Bunun yerine, geçerli örneğe eklenen yeni bir dize value döndürür.

Örneğin, dönüş değeri "abc".Insert(2, "XYZ") "abXYZc" olur.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.