Lire en anglais

Partager via


String.Insert(Int32, String) Méthode

Définition

Retourne une nouvelle chaîne dans laquelle une chaîne spécifiée est insérée dans cette instance à une position d'index spécifiée.

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

Paramètres

startIndex
Int32

Position d'index de base zéro de l'insertion.

value
String

Chaîne à insérer.

Retours

String

Nouvelle chaîne qui est équivalente à cette instance, mais avec value inséré à la position startIndex.

Exceptions

value a la valeur null.

startIndex est négatif ou supérieur à la longueur de cette instance.

Exemples

L’exemple suivant insère un espace à la quatrième position de caractère (le caractère à l’index 3) d’une chaîne.

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'

L’application console suivante invite les utilisateurs à entrer un ou plusieurs adjectifs pour décrire deux animaux. Il appelle ensuite la Insert méthode pour insérer le texte entré par l’utilisateur dans une chaîne.

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.

Remarques

Si startIndex est égal à la longueur de cette instance, value est ajouté à la fin de cette instance.

Notes

Cette méthode ne modifie pas la valeur de l’instance actuelle. Au lieu de cela, elle retourne une nouvelle chaîne dans laquelle value est inséré dans l’instance actuelle.

Par exemple, la valeur de retour de "abc".Insert(2, "XYZ") est « abXYZc ».

S’applique à

Produit Versions
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Voir aussi