Leggere in inglese

Condividi tramite


String.Concat Metodo

Definizione

Concatena una o più istanze di Stringo le rappresentazioni String dei valori di una o più istanze di Object.

Overload

Concat(String, String, String, String)

Concatena quattro istanze specificate di String.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatena le rappresentazioni di stringa di quattro intervalli di caratteri di sola lettura specificati.

Concat(Object, Object, Object, Object)

Concatena le rappresentazioni di stringa di quattro oggetti specificati e gli oggetti specificati in un elenco di parametri di lunghezza variabile facoltativo.

Concat(String, String, String)

Concatena tre istanze specificate di String.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatena le rappresentazioni di stringa di tre intervalli di caratteri di sola lettura specificati.

Concat(Object, Object, Object)

Concatena le rappresentazioni di stringa di tre oggetti specificati.

Concat(String, String)

Concatena due istanze specificate di String.

Concat(Object)

Crea la rappresentazione di stringa di un oggetto specificato.

Concat(Object, Object)

Concatena le rappresentazioni di stringa di due oggetti specificati.

Concat(String[])

Concatena gli elementi di una matrice di String specificata.

Concat(ReadOnlySpan<String>)

Concatena gli elementi di un intervallo specificato di String.

Concat(ReadOnlySpan<Object>)

Concatena le rappresentazioni di stringa degli elementi in un intervallo di oggetti specificato.

Concat(Object[])

Concatena le rappresentazioni di stringa degli elementi in una matrice di Object specificata.

Concat(IEnumerable<String>)

Concatena i membri di una raccolta IEnumerable<T> costruita di tipo String.

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatena le rappresentazioni di stringa di due intervalli di caratteri di sola lettura specificati.

Concat<T>(IEnumerable<T>)

Concatena i membri di un'implementazione di IEnumerable<T>.

Commenti

Nota

È anche possibile usare l'operatore di concatenazione di stringhe del linguaggio, ad esempio + in C# e F#, oppure & e + in Visual Basic, per concatenare stringhe. Entrambi i compilatori convertono l'operatore di concatenazione in una chiamata a uno degli overload di String.Concat.

Concat(String, String, String, String)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena quattro istanze specificate di String.

C#
public static string Concat (string str0, string str1, string str2, string str3);
C#
public static string Concat (string? str0, string? str1, string? str2, string? str3);

Parametri

str0
String

Prima stringa da concatenare.

str1
String

Seconda stringa da concatenare.

str2
String

Terza stringa da concatenare.

str3
String

Quarta stringa da concatenare.

Restituisce

Concatenazione di str0, str1, str2e str3.

Esempio

Nell'esempio seguente viene definita una matrice di parole di quattro lettere e le singole lettere vengono archiviate in una matrice di stringhe per poterle sbattere. Chiama quindi il metodo Concat(String, String, String, String) per riassemblare le parole sbattete.

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

Commenti

Il metodo concatena str0, str1, str2e str3; non aggiunge alcun delimitatore.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa di quattro intervalli di caratteri di sola lettura specificati.

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);

Parametri

str0
ReadOnlySpan<Char>

Primo intervallo di caratteri di sola lettura da concatenare.

str1
ReadOnlySpan<Char>

Secondo intervallo di caratteri di sola lettura da concatenare.

str2
ReadOnlySpan<Char>

Terzo intervallo di caratteri di sola lettura da concatenare.

str3
ReadOnlySpan<Char>

Quarto intervallo di caratteri di sola lettura da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori di str0, str1, str2 e str3.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object, Object)

Importante

Questa API non è conforme a CLS.

Concatena le rappresentazioni di stringa di quattro oggetti specificati e gli oggetti specificati in un elenco di parametri di lunghezza variabile facoltativo.

C#
[System.CLSCompliant(false)]
public static string Concat (object arg0, object arg1, object arg2, object arg3);

Parametri

arg0
Object

Primo oggetto da concatenare.

arg1
Object

Secondo oggetto da concatenare.

arg2
Object

Terzo oggetto da concatenare.

arg3
Object

Quarto oggetto da concatenare.

Restituisce

Rappresentazione di stringa concatenata di ogni valore nell'elenco di parametri.

Attributi

Esempio

Nell'esempio seguente viene illustrato l'uso del metodo Concat(Object, Object, Object, Object) per concatenare un elenco di parametri di variabile. In questo caso, il metodo viene chiamato con nove parametri.

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

Commenti

Nota

Questa API non è conforme a CLS. L'alternativa conforme a CLS è String.Concat(Object[]). I compilatori C# e Visual Basic risolvono automaticamente una chiamata a questo metodo come chiamata a String.Concat(Object[]).

Il metodo concatena ogni oggetto nell'elenco di parametri chiamando il metodo ToString senza parametri; non aggiunge alcun delimitatore.

String.Empty viene usato al posto di qualsiasi argomento Null.

Nota

L'ultimo parametro del metodo Concat è un elenco facoltativo delimitato da virgole di uno o più oggetti aggiuntivi da concatenare.

Note per i chiamanti

Questo metodo è contrassegnato con la parola chiave vararg, il che significa che supporta un numero variabile di parametri. Il metodo può essere chiamato da Visual C++, ma non può essere chiamato dal codice C# o Visual Basic. I compilatori C# e Visual Basic risolvono le chiamate a Concat(Object, Object, Object, Object) come chiamate a Concat(Object[]).

Si applica a

.NET Framework 4.8.1 e altre versioni
Prodotto Versioni
.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

Concat(String, String, String)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena tre istanze specificate di String.

C#
public static string Concat (string str0, string str1, string str2);
C#
public static string Concat (string? str0, string? str1, string? str2);

Parametri

str0
String

Prima stringa da concatenare.

str1
String

Seconda stringa da concatenare.

str2
String

Terza stringa da concatenare.

Restituisce

Concatenazione di str0, str1e str2.

Esempio

Nell'esempio seguente viene utilizzato il metodo Concat per concatenare tre stringhe e visualizzare il risultato.

C#
using System;

public class Example
{
   public static void Main()
   {
      String s1 = "We went to a bookstore, ";
      String s2 = "a movie, ";
      String s3 = "and a restaurant.";

      var s = String.Concat(s1, s2, s3);
      Console.WriteLine(s);
   }
}
// The example displays the following output:
//      We went to a bookstore, a movie, and a restaurant.

Commenti

Il metodo concatena str0, str1e str2; non aggiunge alcun delimitatore.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa di tre intervalli di caratteri di sola lettura specificati.

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);

Parametri

str0
ReadOnlySpan<Char>

Primo intervallo di caratteri di sola lettura da concatenare.

str1
ReadOnlySpan<Char>

Secondo intervallo di caratteri di sola lettura da concatenare.

str2
ReadOnlySpan<Char>

Terzo intervallo di caratteri di sola lettura da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori di str0, str1 e str2.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa di tre oggetti specificati.

C#
public static string Concat (object arg0, object arg1, object arg2);
C#
public static string Concat (object? arg0, object? arg1, object? arg2);

Parametri

arg0
Object

Primo oggetto da concatenare.

arg1
Object

Secondo oggetto da concatenare.

arg2
Object

Terzo oggetto da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori di arg0, arg1e arg2.

Esempio

Nell'esempio seguente viene illustrato il metodo Concat.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Commenti

Il metodo concatena arg0, arg1e arg2 chiamando il metodo ToString senza parametri di ogni oggetto; non aggiunge alcun delimitatore.

String.Empty viene usato al posto di qualsiasi argomento Null.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(String, String)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena due istanze specificate di String.

C#
public static string Concat (string str0, string str1);
C#
public static string Concat (string? str0, string? str1);

Parametri

str0
String

Prima stringa da concatenare.

str1
String

Seconda stringa da concatenare.

Restituisce

Concatenazione di str0 e str1.

Esempio

Nell'esempio seguente viene concatenato il nome, il secondo e il cognome di una persona.

C#
using System;

public class ConcatTest {
    public static void Main() {

        // we want to simply quickly add this person's name together
        string fName = "Simon";
        string mName = "Jake";
        string lName = "Harrows";

        // because we want a name to appear with a space in between each name,
        // put a space on the front of the middle, and last name, allowing for
        // the fact that a space may already be there
        mName = " " + mName.Trim();
        lName = " " + lName.Trim();

        // this line simply concatenates the two strings
        Console.WriteLine("Welcome to this page, '{0}'!", string.Concat( string.Concat(fName, mName), lName ) );
    }
}
// The example displays the following output:
//        Welcome to this page, 'Simon Jake Harrows'!

Commenti

Il metodo concatena str0 e str1; non aggiunge alcun delimitatore.

Una stringa Empty viene usata al posto di qualsiasi argomento Null.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Crea la rappresentazione di stringa di un oggetto specificato.

C#
public static string Concat (object arg0);
C#
public static string Concat (object? arg0);

Parametri

arg0
Object

Oggetto da rappresentare o null.

Restituisce

Rappresentazione di stringa del valore di arg0o Empty se arg0 è null.

Esempio

Nell'esempio seguente viene illustrato il metodo Concat.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Commenti

Il metodo Concat(Object) rappresenta arg0 come stringa chiamando il metodo ToString senza parametri.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(Object, Object)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa di due oggetti specificati.

C#
public static string Concat (object arg0, object arg1);
C#
public static string Concat (object? arg0, object? arg1);

Parametri

arg0
Object

Primo oggetto da concatenare.

arg1
Object

Secondo oggetto da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori di arg0 e arg1.

Esempio

Nell'esempio seguente viene illustrato il metodo Concat.

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

Commenti

Il metodo concatena arg0 e arg1 chiamando il metodo ToString senza parametri di arg0 e arg1; non aggiunge alcun delimitatore.

String.Empty viene usato al posto di qualsiasi argomento Null.

Se uno degli argomenti è un riferimento alla matrice, il metodo concatena una stringa che rappresenta tale matrice anziché i relativi membri, ad esempio "System.String[]").

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(String[])

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Importante

Questa API non è conforme a CLS.

Concatena gli elementi di una matrice di String specificata.

C#
public static string Concat (params string[] values);
C#
public static string Concat (params string?[] values);
C#
[System.CLSCompliant(false)]
public static string Concat (params string[] values);

Parametri

values
String[]

Matrice di istanze di stringa.

Restituisce

Elementi concatenati di values.

Attributi

Eccezioni

values è null.

Memoria insufficiente.

Esempio

Nell'esempio seguente viene illustrato l'uso del metodo Concat con una matrice di String.

C#
using System;

public class Example
{
    public static void Main()
    {
        // Make an array of strings. Note that we have included spaces.
        string [] s = { "hello ", "and ", "welcome ", "to ",
                        "this ", "demo! " };

        // Put all the strings together.
        Console.WriteLine(string.Concat(s));

        // Sort the strings, and put them together.
        Array.Sort(s);
        Console.WriteLine(string.Concat(s));
    }
}
// The example displays the following output:
//       hello and welcome to this demo!
//       and demo! hello this to welcome

Commenti

Il metodo concatena ogni oggetto in values; non aggiunge alcun delimitatore.

Una stringa Empty viene usata al posto di qualsiasi oggetto Null nella matrice.

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(ReadOnlySpan<String>)

Concatena gli elementi di un intervallo specificato di String.

C#
public static string Concat (scoped ReadOnlySpan<string?> values);

Parametri

values
ReadOnlySpan<String>

Intervallo di istanze di String.

Restituisce

Elementi concatenati di values.

Si applica a

.NET 9
Prodotto Versioni
.NET 9

Concat(ReadOnlySpan<Object>)

Concatena le rappresentazioni di stringa degli elementi in un intervallo di oggetti specificato.

C#
public static string Concat (scoped ReadOnlySpan<object?> args);

Parametri

args
ReadOnlySpan<Object>

Intervallo di oggetti che contiene gli elementi da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori degli elementi in args.

Si applica a

.NET 9
Prodotto Versioni
.NET 9

Concat(Object[])

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa degli elementi in una matrice di Object specificata.

C#
public static string Concat (params object[] args);
C#
public static string Concat (params object?[] args);

Parametri

args
Object[]

Matrice di oggetti che contiene gli elementi da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori degli elementi in args.

Eccezioni

args è null.

Memoria insufficiente.

Esempio

Nell'esempio seguente viene illustrato l'uso del metodo Concat con una matrice di Object.

C#
using System;

public class ConcatTest {
    public static void Main() {
        // Create a group of objects.
        Test1 t1 = new Test1();
        Test2 t2 = new Test2();
        int i = 16;
        string s = "Demonstration";

        // Place the objects in an array.
        object [] o = { t1, i, t2, s };

        // Concatenate the objects together as a string. To do this,
        // the ToString method of each of the objects is called.
        Console.WriteLine(string.Concat(o));
    }
}

// Create two empty test classes.
class Test1 {
}

class Test2 {
}
// The example displays the following output:
//       Test116Test2Demonstration

Commenti

Il metodo concatena ogni oggetto in args chiamando il metodo ToString senza parametri di tale oggetto; non aggiunge alcun delimitatore.

String.Empty viene usato al posto di qualsiasi oggetto Null nella matrice.

Note per i chiamanti

Questo metodo non viene chiamato dal codice C++. Il compilatore C++ risolve le chiamate a Concat con quattro o più parametri oggetto come chiamata a Concat(Object, Object, Object, Object).

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.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

Concat(IEnumerable<String>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena i membri di una raccolta IEnumerable<T> costruita di tipo String.

C#
public static string Concat (System.Collections.Generic.IEnumerable<string> values);
C#
public static string Concat (System.Collections.Generic.IEnumerable<string?> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat (System.Collections.Generic.IEnumerable<string> values);

Parametri

values
IEnumerable<String>

Oggetto raccolta che implementa IEnumerable<T> e il cui argomento di tipo generico è String.

Restituisce

Le stringhe concatenate in valueso Empty se values è un IEnumerable(Of String)vuoto.

Attributi

Eccezioni

values è null.

Esempio

Nell'esempio seguente viene usato l'algoritmo Sieve di Eratosthenes per calcolare i numeri primi minori o uguali a 100. Assegna il risultato a un oggetto List<T> di tipo String, che passa quindi al metodo Concat(IEnumerable<String>).

C#
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      IEnumerable<String> primeList = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Concat(primeList));
   }

   private static IEnumerable<String> GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 }); 
      // Use Sieve of Erathsthenes to determine prime numbers.
      for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
      {
                           
         if ((int) values.GetValue(ctr) == 1) continue;
         
         for (int multiplier = ctr; multiplier <=  maxPrime / 2; multiplier++)
            if (ctr * multiplier <= maxPrime)
               values.SetValue(1, ctr * multiplier);
      }      
      
      List<String> primes = new List<String>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr.ToString() + " ");
      return primes;
   }   
}
// The example displays the following output:
//    Primes less than 100:
//       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Commenti

Il metodo concatena ogni oggetto in values; non aggiunge alcun delimitatore. Per specificare un delimitatore tra ogni membro di values, chiamare il metodo Join(String, IEnumerable<String>).

Una stringa Empty viene usata al posto di qualsiasi elemento Null in values.

Se values è un IEnumerable(Of String)vuoto, il metodo restituisce String.Empty. Se values è null, il metodo genera un'eccezione ArgumentNullException.

Concat(IEnumerable<String>) è un metodo pratico che consente di concatenare ogni elemento in una raccolta IEnumerable(Of String) senza prima convertire gli elementi in una matrice di stringhe. È particolarmente utile con le espressioni di query LINQ (Language-Integrated Query). Nell'esempio seguente viene passato un oggetto List(Of String) contenente le lettere maiuscole o minuscole dell'alfabeto a un'espressione lambda che seleziona lettere uguali o maggiori di una determinata lettera (che, nell'esempio, è "M"). L'insieme IEnumerable(Of String) restituito dal metodo Enumerable.Where viene passato al metodo Concat(IEnumerable<String>) per visualizzare il risultato come una singola stringa.

C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Main()
   {
      string output = String.Concat( GetAlphabet(true).Where( letter => 
                      letter.CompareTo("M") >= 0));
      Console.WriteLine(output);  
   }

   private static List<string> GetAlphabet(bool upper)
   {
      List<string> alphabet = new List<string>();
      int charValue = upper ? 65 : 97;
      for (int ctr = 0; ctr <= 25; ctr++)
         alphabet.Add(((char)(charValue + ctr)).ToString());
      return alphabet; 
   }
}
// The example displays the following output:
//      MNOPQRSTUVWXYZ

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.NET Framework 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

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena le rappresentazioni di stringa di due intervalli di caratteri di sola lettura specificati.

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);

Parametri

str0
ReadOnlySpan<Char>

Primo intervallo di caratteri di sola lettura da concatenare.

str1
ReadOnlySpan<Char>

Secondo intervallo di caratteri di sola lettura da concatenare.

Restituisce

Rappresentazioni di stringa concatenate dei valori di str0 e str1.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat<T>(IEnumerable<T>)

Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs
Origine:
String.Manipulation.cs

Concatena i membri di un'implementazione di IEnumerable<T>.

C#
public static string Concat<T> (System.Collections.Generic.IEnumerable<T> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat<T> (System.Collections.Generic.IEnumerable<T> values);

Parametri di tipo

T

Tipo dei membri di values.

Parametri

values
IEnumerable<T>

Oggetto raccolta che implementa l'interfaccia IEnumerable<T>.

Restituisce

Membri concatenati in values.

Attributi

Eccezioni

values è null.

Esempio

Nell'esempio seguente viene definita una classe Animal molto semplice che contiene il nome di un animale e l'ordine a cui appartiene. Definisce quindi un oggetto List<T> per contenere un numero di oggetti Animal. Viene chiamato il metodo di estensione Enumerable.Where per estrarre gli oggetti Animal la cui proprietà di Order è uguale a "Dro". Il risultato viene passato al metodo Concat<T>(IEnumerable<T>) e visualizzato alla console.

C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Animal
{
   public string Kind;
   public string Order;
   
   public Animal(string kind, string order)
   {
      this.Kind = kind;
      this.Order = order;
   }
   
   public override string ToString()
   {
      return this.Kind;
   }
}

public class Example
{
   public static void Main()
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      animals.Add(new Animal("Capybara", "Rodent"));
      string output = String.Concat(animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output:
//      SquirrelCapybara

Commenti

Il metodo concatena ogni oggetto in values; non aggiunge alcun delimitatore.

Una stringa Empty viene usata al posto di qualsiasi argomento Null.

Concat<T>(IEnumerable<T>) è un metodo pratico che consente di concatenare ogni elemento in una raccolta IEnumerable<T> senza prima convertire gli elementi in stringhe. È particolarmente utile con le espressioni di query LINQ (Query) Language-Integrated, come illustrato nell'esempio. La rappresentazione di stringa di ogni oggetto nell'insieme IEnumerable<T> viene derivata chiamando il metodo ToString dell'oggetto.

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.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
.NET Framework 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