C#idiomatic C#
creating-newstandard/base-types

Core library

Create new strings in .NET

Learn to create strings using assignment, class constructors, or System.String methods that combine several strings, arrays of strings, or objects in .NET.

  • page standard/base-types/creating-new
  • updated 03/16/2026

.NET allows strings to be created using simple assignment, and also overloads a class constructor to support string creation using a number of different parameters. .NET also provides several methods in the System.String class that create new string objects by combining several strings, arrays of strings, or objects.

Create strings using assignment

The easiest way to create a new System.String object is simply to assign a string literal to a System.String object.

Create strings using a class constructor

You can use overloads of the System.String class constructor to create strings from character arrays. You can also create a new string by duplicating a particular character a specified number of times. The System.String.%23ctor(System.ReadOnlySpan{System.Char}) constructor overload accepts a System.ReadOnlySpan%601 or a stack-allocated System.Span%601 of characters and avoids allocating an intermediate character array on the managed heap when you build small strings of a known size, although the resulting string instance is still allocated on the managed heap.

Methods that return strings

The following table lists several useful methods that return new string objects.

Method name Use
System.String.Format%2A Builds a formatted string from a set of input objects.
System.String.Concat%2A Builds strings from two or more strings.
System.String.Join%2A Builds a new string by combining an array of strings.
System.String.Insert%2A Builds a new string by inserting a string into the specified index of an existing string.
System.String.CopyTo%2A Copies specified characters in a string into a specified position in an array of characters.
System.String.Create%2A Creates a new string of a specified length, populating characters via a callback that receives a writable System.Span%601 and a caller-supplied state object.

String.Format

You can use the String.Format method to create formatted strings and concatenate strings representing multiple objects. This method automatically converts any passed object into a string. For example, if your application must display an Int32 value and a DateTime value to the user, you can easily construct a string to represent these values using the Format method. For information about formatting conventions used with this method, see the section on composite formatting.

The following example uses the Format method to create a string that uses an integer variable.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Strings.Creating/cs/Example.cs1

csharp
using System;

[assembly: CLSCompliant(true)]
public class Class1
{
   public static void Main()
   {
      UseStringFormat();
      Console.WriteLine();
      UseStringConcat();
      Console.WriteLine();
      UseStringJoin();
      Console.WriteLine();
      UseStringInsert();
      Console.WriteLine();
      UseStringCopyTo();
   }

   private static void UseStringFormat()
   {
      // <Snippet1>
      int numberOfFleas = 12;
      string miscInfo = String.Format("Your dog has {0} fleas. " +
                                      "It is time to get a flea collar. " +
                                      "The current universal date is: {1:u}.",
                                      numberOfFleas, DateTime.Now);
      Console.WriteLine(miscInfo);
      // The example displays the following output:
      //       Your dog has 12 fleas. It is time to get a flea collar.
      //       The current universal date is: 2008-03-28 13:31:40Z.
      // </Snippet1>
   }

   private static void UseStringConcat()
   {
      // <Snippet2>
      string helloString1 = "Hello";
      string helloString2 = "World!";
      Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
      // The example displays the following output:
      //      Hello World!
      // </Snippet2>
   }

   private static void UseStringJoin()
   {
      // <Snippet3>
      string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
      Console.WriteLine(String.Join(" ", words));
      // The example displays the following output:
      //      Hello and welcome to my world!
      // </Snippet3>
   }

   private static void UseStringInsert()
   {
       // <Snippet4>
     string sentence = "Once a time.";
      Console.WriteLine(sentence.Insert(4, " upon"));
      // The example displays the following output:
      //      Once upon a time.
      // </Snippet4>
   }

   private static void UseStringCopyTo()
   {
       // <Snippet5>
      string greeting = "Hello World!";
      char[] charArray = {'W','h','e','r','e'};
      Console.WriteLine($"The original character array: {new string(charArray)}");
      greeting.CopyTo(0, charArray,0 ,5);
      Console.WriteLine($"The new character array: {new string(charArray)}");
      // The example displays the following output:
      //       The original character array: Where
      //       The new character array: Hello
      // </Snippet5>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Strings.Creating/vb/Example.vb1

vb
' Visual Basic .NET Document
Option Strict On

<Assembly: CLSCompliant(True)>
Module Example

    Public Sub Main()
        UseStringFormat()
        Console.WriteLine()
        UseStringConcat()
        Console.WriteLine()
        UseStringJoin()
        Console.WriteLine()
        UseStringInsert()
        Console.WriteLine()
        UseStringCopyTo()
    End Sub

    Private Sub UseStringFormat()
        ' <Snippet1>
        Dim numberOfFleas As Integer = 12
        Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                               "It is time to get a flea collar. " & _
                                               "The current universal date is: {1:u}.", _
                                               numberOfFleas, Date.Now)
        Console.WriteLine(miscInfo)
        ' The example displays the following output:
        '       Your dog has 12 fleas. It is time to get a flea collar. 
        '       The current universal date is: 2008-03-28 13:31:40Z.
        ' </Snippet1>
    End Sub

    Private Sub UseStringConcat()
        ' <Snippet2>
        Dim helloString1 As String = "Hello"
        Dim helloString2 As String = "World!"
        Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
        ' The example displays the following output:
        '      Hello World!
        ' </Snippet2>      
    End Sub

    Private Sub UseStringJoin()
        ' <Snippet3>
        Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
        Console.WriteLine(String.Join(" ", words))
        ' The example displays the following output:
        '      Hello and welcome to my world!
        ' </Snippet3>      
    End Sub

    Private Sub UseStringInsert()
        ' <Snippet4>
        Dim sentence As String = "Once a time."
        Console.WriteLine(sentence.Insert(4, " upon"))
        ' The example displays the following output:
        '      Once upon a time.
        ' </Snippet4>      
    End Sub

    Private Sub UseStringCopyTo()
        ' <Snippet5>
        Dim greeting As String = "Hello World!"
        Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
        Console.WriteLine("The original character array: {0}", New String(charArray))
        greeting.CopyTo(0, charArray, 0, 5)
        Console.WriteLine("The new character array: {0}", New String(charArray))
        ' The example displays the following output:
        '       The original character array: Where
        '       The new character array: Hello
        ' </Snippet5>      
    End Sub
End Module

In this example, System.DateTime.Now%2A displays the current date and time in a manner specified by the culture associated with the current thread.

String.Concat

The String.Concat method can be used to easily create a new string object from two or more existing objects. It provides a language-independent way to concatenate strings. This method accepts any class that derives from System.Object. The following example creates a string from two existing string objects and a separating character.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Strings.Creating/cs/Example.cs2

csharp
using System;

[assembly: CLSCompliant(true)]
public class Class1
{
   public static void Main()
   {
      UseStringFormat();
      Console.WriteLine();
      UseStringConcat();
      Console.WriteLine();
      UseStringJoin();
      Console.WriteLine();
      UseStringInsert();
      Console.WriteLine();
      UseStringCopyTo();
   }

   private static void UseStringFormat()
   {
      // <Snippet1>
      int numberOfFleas = 12;
      string miscInfo = String.Format("Your dog has {0} fleas. " +
                                      "It is time to get a flea collar. " +
                                      "The current universal date is: {1:u}.",
                                      numberOfFleas, DateTime.Now);
      Console.WriteLine(miscInfo);
      // The example displays the following output:
      //       Your dog has 12 fleas. It is time to get a flea collar.
      //       The current universal date is: 2008-03-28 13:31:40Z.
      // </Snippet1>
   }

   private static void UseStringConcat()
   {
      // <Snippet2>
      string helloString1 = "Hello";
      string helloString2 = "World!";
      Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
      // The example displays the following output:
      //      Hello World!
      // </Snippet2>
   }

   private static void UseStringJoin()
   {
      // <Snippet3>
      string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
      Console.WriteLine(String.Join(" ", words));
      // The example displays the following output:
      //      Hello and welcome to my world!
      // </Snippet3>
   }

   private static void UseStringInsert()
   {
       // <Snippet4>
     string sentence = "Once a time.";
      Console.WriteLine(sentence.Insert(4, " upon"));
      // The example displays the following output:
      //      Once upon a time.
      // </Snippet4>
   }

   private static void UseStringCopyTo()
   {
       // <Snippet5>
      string greeting = "Hello World!";
      char[] charArray = {'W','h','e','r','e'};
      Console.WriteLine($"The original character array: {new string(charArray)}");
      greeting.CopyTo(0, charArray,0 ,5);
      Console.WriteLine($"The new character array: {new string(charArray)}");
      // The example displays the following output:
      //       The original character array: Where
      //       The new character array: Hello
      // </Snippet5>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Strings.Creating/vb/Example.vb2

vb
' Visual Basic .NET Document
Option Strict On

<Assembly: CLSCompliant(True)>
Module Example

    Public Sub Main()
        UseStringFormat()
        Console.WriteLine()
        UseStringConcat()
        Console.WriteLine()
        UseStringJoin()
        Console.WriteLine()
        UseStringInsert()
        Console.WriteLine()
        UseStringCopyTo()
    End Sub

    Private Sub UseStringFormat()
        ' <Snippet1>
        Dim numberOfFleas As Integer = 12
        Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                               "It is time to get a flea collar. " & _
                                               "The current universal date is: {1:u}.", _
                                               numberOfFleas, Date.Now)
        Console.WriteLine(miscInfo)
        ' The example displays the following output:
        '       Your dog has 12 fleas. It is time to get a flea collar. 
        '       The current universal date is: 2008-03-28 13:31:40Z.
        ' </Snippet1>
    End Sub

    Private Sub UseStringConcat()
        ' <Snippet2>
        Dim helloString1 As String = "Hello"
        Dim helloString2 As String = "World!"
        Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
        ' The example displays the following output:
        '      Hello World!
        ' </Snippet2>      
    End Sub

    Private Sub UseStringJoin()
        ' <Snippet3>
        Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
        Console.WriteLine(String.Join(" ", words))
        ' The example displays the following output:
        '      Hello and welcome to my world!
        ' </Snippet3>      
    End Sub

    Private Sub UseStringInsert()
        ' <Snippet4>
        Dim sentence As String = "Once a time."
        Console.WriteLine(sentence.Insert(4, " upon"))
        ' The example displays the following output:
        '      Once upon a time.
        ' </Snippet4>      
    End Sub

    Private Sub UseStringCopyTo()
        ' <Snippet5>
        Dim greeting As String = "Hello World!"
        Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
        Console.WriteLine("The original character array: {0}", New String(charArray))
        greeting.CopyTo(0, charArray, 0, 5)
        Console.WriteLine("The new character array: {0}", New String(charArray))
        ' The example displays the following output:
        '       The original character array: Where
        '       The new character array: Hello
        ' </Snippet5>      
    End Sub
End Module

String.Join

The String.Join method creates a new string from an array of strings and a separator string. This method is useful if you want to concatenate multiple strings together, making a list perhaps separated by a comma.

The following example uses a space to bind a string array.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Strings.Creating/cs/Example.cs3

csharp
using System;

[assembly: CLSCompliant(true)]
public class Class1
{
   public static void Main()
   {
      UseStringFormat();
      Console.WriteLine();
      UseStringConcat();
      Console.WriteLine();
      UseStringJoin();
      Console.WriteLine();
      UseStringInsert();
      Console.WriteLine();
      UseStringCopyTo();
   }

   private static void UseStringFormat()
   {
      // <Snippet1>
      int numberOfFleas = 12;
      string miscInfo = String.Format("Your dog has {0} fleas. " +
                                      "It is time to get a flea collar. " +
                                      "The current universal date is: {1:u}.",
                                      numberOfFleas, DateTime.Now);
      Console.WriteLine(miscInfo);
      // The example displays the following output:
      //       Your dog has 12 fleas. It is time to get a flea collar.
      //       The current universal date is: 2008-03-28 13:31:40Z.
      // </Snippet1>
   }

   private static void UseStringConcat()
   {
      // <Snippet2>
      string helloString1 = "Hello";
      string helloString2 = "World!";
      Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
      // The example displays the following output:
      //      Hello World!
      // </Snippet2>
   }

   private static void UseStringJoin()
   {
      // <Snippet3>
      string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
      Console.WriteLine(String.Join(" ", words));
      // The example displays the following output:
      //      Hello and welcome to my world!
      // </Snippet3>
   }

   private static void UseStringInsert()
   {
       // <Snippet4>
     string sentence = "Once a time.";
      Console.WriteLine(sentence.Insert(4, " upon"));
      // The example displays the following output:
      //      Once upon a time.
      // </Snippet4>
   }

   private static void UseStringCopyTo()
   {
       // <Snippet5>
      string greeting = "Hello World!";
      char[] charArray = {'W','h','e','r','e'};
      Console.WriteLine($"The original character array: {new string(charArray)}");
      greeting.CopyTo(0, charArray,0 ,5);
      Console.WriteLine($"The new character array: {new string(charArray)}");
      // The example displays the following output:
      //       The original character array: Where
      //       The new character array: Hello
      // </Snippet5>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Strings.Creating/vb/Example.vb3

vb
' Visual Basic .NET Document
Option Strict On

<Assembly: CLSCompliant(True)>
Module Example

    Public Sub Main()
        UseStringFormat()
        Console.WriteLine()
        UseStringConcat()
        Console.WriteLine()
        UseStringJoin()
        Console.WriteLine()
        UseStringInsert()
        Console.WriteLine()
        UseStringCopyTo()
    End Sub

    Private Sub UseStringFormat()
        ' <Snippet1>
        Dim numberOfFleas As Integer = 12
        Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                               "It is time to get a flea collar. " & _
                                               "The current universal date is: {1:u}.", _
                                               numberOfFleas, Date.Now)
        Console.WriteLine(miscInfo)
        ' The example displays the following output:
        '       Your dog has 12 fleas. It is time to get a flea collar. 
        '       The current universal date is: 2008-03-28 13:31:40Z.
        ' </Snippet1>
    End Sub

    Private Sub UseStringConcat()
        ' <Snippet2>
        Dim helloString1 As String = "Hello"
        Dim helloString2 As String = "World!"
        Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
        ' The example displays the following output:
        '      Hello World!
        ' </Snippet2>      
    End Sub

    Private Sub UseStringJoin()
        ' <Snippet3>
        Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
        Console.WriteLine(String.Join(" ", words))
        ' The example displays the following output:
        '      Hello and welcome to my world!
        ' </Snippet3>      
    End Sub

    Private Sub UseStringInsert()
        ' <Snippet4>
        Dim sentence As String = "Once a time."
        Console.WriteLine(sentence.Insert(4, " upon"))
        ' The example displays the following output:
        '      Once upon a time.
        ' </Snippet4>      
    End Sub

    Private Sub UseStringCopyTo()
        ' <Snippet5>
        Dim greeting As String = "Hello World!"
        Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
        Console.WriteLine("The original character array: {0}", New String(charArray))
        greeting.CopyTo(0, charArray, 0, 5)
        Console.WriteLine("The new character array: {0}", New String(charArray))
        ' The example displays the following output:
        '       The original character array: Where
        '       The new character array: Hello
        ' </Snippet5>      
    End Sub
End Module

String.Insert

The String.Insert method creates a new string by inserting a string into a specified position in another string. This method uses a zero-based index. The following example inserts a string into the fifth index position of MyString and creates a new string with this value.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Strings.Creating/cs/Example.cs4

csharp
using System;

[assembly: CLSCompliant(true)]
public class Class1
{
   public static void Main()
   {
      UseStringFormat();
      Console.WriteLine();
      UseStringConcat();
      Console.WriteLine();
      UseStringJoin();
      Console.WriteLine();
      UseStringInsert();
      Console.WriteLine();
      UseStringCopyTo();
   }

   private static void UseStringFormat()
   {
      // <Snippet1>
      int numberOfFleas = 12;
      string miscInfo = String.Format("Your dog has {0} fleas. " +
                                      "It is time to get a flea collar. " +
                                      "The current universal date is: {1:u}.",
                                      numberOfFleas, DateTime.Now);
      Console.WriteLine(miscInfo);
      // The example displays the following output:
      //       Your dog has 12 fleas. It is time to get a flea collar.
      //       The current universal date is: 2008-03-28 13:31:40Z.
      // </Snippet1>
   }

   private static void UseStringConcat()
   {
      // <Snippet2>
      string helloString1 = "Hello";
      string helloString2 = "World!";
      Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
      // The example displays the following output:
      //      Hello World!
      // </Snippet2>
   }

   private static void UseStringJoin()
   {
      // <Snippet3>
      string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
      Console.WriteLine(String.Join(" ", words));
      // The example displays the following output:
      //      Hello and welcome to my world!
      // </Snippet3>
   }

   private static void UseStringInsert()
   {
       // <Snippet4>
     string sentence = "Once a time.";
      Console.WriteLine(sentence.Insert(4, " upon"));
      // The example displays the following output:
      //      Once upon a time.
      // </Snippet4>
   }

   private static void UseStringCopyTo()
   {
       // <Snippet5>
      string greeting = "Hello World!";
      char[] charArray = {'W','h','e','r','e'};
      Console.WriteLine($"The original character array: {new string(charArray)}");
      greeting.CopyTo(0, charArray,0 ,5);
      Console.WriteLine($"The new character array: {new string(charArray)}");
      // The example displays the following output:
      //       The original character array: Where
      //       The new character array: Hello
      // </Snippet5>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Strings.Creating/vb/Example.vb4

vb
' Visual Basic .NET Document
Option Strict On

<Assembly: CLSCompliant(True)>
Module Example

    Public Sub Main()
        UseStringFormat()
        Console.WriteLine()
        UseStringConcat()
        Console.WriteLine()
        UseStringJoin()
        Console.WriteLine()
        UseStringInsert()
        Console.WriteLine()
        UseStringCopyTo()
    End Sub

    Private Sub UseStringFormat()
        ' <Snippet1>
        Dim numberOfFleas As Integer = 12
        Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                               "It is time to get a flea collar. " & _
                                               "The current universal date is: {1:u}.", _
                                               numberOfFleas, Date.Now)
        Console.WriteLine(miscInfo)
        ' The example displays the following output:
        '       Your dog has 12 fleas. It is time to get a flea collar. 
        '       The current universal date is: 2008-03-28 13:31:40Z.
        ' </Snippet1>
    End Sub

    Private Sub UseStringConcat()
        ' <Snippet2>
        Dim helloString1 As String = "Hello"
        Dim helloString2 As String = "World!"
        Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
        ' The example displays the following output:
        '      Hello World!
        ' </Snippet2>      
    End Sub

    Private Sub UseStringJoin()
        ' <Snippet3>
        Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
        Console.WriteLine(String.Join(" ", words))
        ' The example displays the following output:
        '      Hello and welcome to my world!
        ' </Snippet3>      
    End Sub

    Private Sub UseStringInsert()
        ' <Snippet4>
        Dim sentence As String = "Once a time."
        Console.WriteLine(sentence.Insert(4, " upon"))
        ' The example displays the following output:
        '      Once upon a time.
        ' </Snippet4>      
    End Sub

    Private Sub UseStringCopyTo()
        ' <Snippet5>
        Dim greeting As String = "Hello World!"
        Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
        Console.WriteLine("The original character array: {0}", New String(charArray))
        greeting.CopyTo(0, charArray, 0, 5)
        Console.WriteLine("The new character array: {0}", New String(charArray))
        ' The example displays the following output:
        '       The original character array: Where
        '       The new character array: Hello
        ' </Snippet5>      
    End Sub
End Module

String.CopyTo

The String.CopyTo method copies portions of a string into an array of characters. You can specify both the beginning index of the string and the number of characters to be copied. This method takes the source index, an array of characters, the destination index, and the number of characters to copy. All indexes are zero-based.

The following example uses the CopyTo method to copy the characters of the word "Hello" from a string object to the first index position of an array of characters.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/Strings.Creating/cs/Example.cs5

csharp
using System;

[assembly: CLSCompliant(true)]
public class Class1
{
   public static void Main()
   {
      UseStringFormat();
      Console.WriteLine();
      UseStringConcat();
      Console.WriteLine();
      UseStringJoin();
      Console.WriteLine();
      UseStringInsert();
      Console.WriteLine();
      UseStringCopyTo();
   }

   private static void UseStringFormat()
   {
      // <Snippet1>
      int numberOfFleas = 12;
      string miscInfo = String.Format("Your dog has {0} fleas. " +
                                      "It is time to get a flea collar. " +
                                      "The current universal date is: {1:u}.",
                                      numberOfFleas, DateTime.Now);
      Console.WriteLine(miscInfo);
      // The example displays the following output:
      //       Your dog has 12 fleas. It is time to get a flea collar.
      //       The current universal date is: 2008-03-28 13:31:40Z.
      // </Snippet1>
   }

   private static void UseStringConcat()
   {
      // <Snippet2>
      string helloString1 = "Hello";
      string helloString2 = "World!";
      Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
      // The example displays the following output:
      //      Hello World!
      // </Snippet2>
   }

   private static void UseStringJoin()
   {
      // <Snippet3>
      string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
      Console.WriteLine(String.Join(" ", words));
      // The example displays the following output:
      //      Hello and welcome to my world!
      // </Snippet3>
   }

   private static void UseStringInsert()
   {
       // <Snippet4>
     string sentence = "Once a time.";
      Console.WriteLine(sentence.Insert(4, " upon"));
      // The example displays the following output:
      //      Once upon a time.
      // </Snippet4>
   }

   private static void UseStringCopyTo()
   {
       // <Snippet5>
      string greeting = "Hello World!";
      char[] charArray = {'W','h','e','r','e'};
      Console.WriteLine($"The original character array: {new string(charArray)}");
      greeting.CopyTo(0, charArray,0 ,5);
      Console.WriteLine($"The new character array: {new string(charArray)}");
      // The example displays the following output:
      //       The original character array: Where
      //       The new character array: Hello
      // </Snippet5>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/Strings.Creating/vb/Example.vb5

vb
' Visual Basic .NET Document
Option Strict On

<Assembly: CLSCompliant(True)>
Module Example

    Public Sub Main()
        UseStringFormat()
        Console.WriteLine()
        UseStringConcat()
        Console.WriteLine()
        UseStringJoin()
        Console.WriteLine()
        UseStringInsert()
        Console.WriteLine()
        UseStringCopyTo()
    End Sub

    Private Sub UseStringFormat()
        ' <Snippet1>
        Dim numberOfFleas As Integer = 12
        Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                               "It is time to get a flea collar. " & _
                                               "The current universal date is: {1:u}.", _
                                               numberOfFleas, Date.Now)
        Console.WriteLine(miscInfo)
        ' The example displays the following output:
        '       Your dog has 12 fleas. It is time to get a flea collar. 
        '       The current universal date is: 2008-03-28 13:31:40Z.
        ' </Snippet1>
    End Sub

    Private Sub UseStringConcat()
        ' <Snippet2>
        Dim helloString1 As String = "Hello"
        Dim helloString2 As String = "World!"
        Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
        ' The example displays the following output:
        '      Hello World!
        ' </Snippet2>      
    End Sub

    Private Sub UseStringJoin()
        ' <Snippet3>
        Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
        Console.WriteLine(String.Join(" ", words))
        ' The example displays the following output:
        '      Hello and welcome to my world!
        ' </Snippet3>      
    End Sub

    Private Sub UseStringInsert()
        ' <Snippet4>
        Dim sentence As String = "Once a time."
        Console.WriteLine(sentence.Insert(4, " upon"))
        ' The example displays the following output:
        '      Once upon a time.
        ' </Snippet4>      
    End Sub

    Private Sub UseStringCopyTo()
        ' <Snippet5>
        Dim greeting As String = "Hello World!"
        Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
        Console.WriteLine("The original character array: {0}", New String(charArray))
        greeting.CopyTo(0, charArray, 0, 5)
        Console.WriteLine("The new character array: {0}", New String(charArray))
        ' The example displays the following output:
        '       The original character array: Where
        '       The new character array: Hello
        ' </Snippet5>      
    End Sub
End Module

String.Create

The System.String.Create%2A method lets you programmatically fill a new string's characters using a callback. The callback receives a writable System.Span%601 of characters and a caller-supplied state object, so you can build the string's content without allocating intermediate character buffers. The callback itself might still allocate, for example if it captures local variables or calls other allocation-heavy APIs.

The following example uses String.Create to build a five-character string from consecutive alphabet characters:

sample · csharpdocs/standard/base-types/snippets/creating-new/csharp/Program.csUsingStringCreate

csharp
using System;

// <UsingSpanConstructor>
static string CreateStringFromSpan()
{
    Span<char> span = stackalloc char[5];
    for (int i = 0; i < 5; i++)
    {
        span[i] = (char)('a' + i);
    }
    return new string(span);
}

Console.WriteLine(CreateStringFromSpan()); // abcde
// </UsingSpanConstructor>

// <UsingStringCreate>
string result = string.Create(5, 'a', (span, firstChar) =>
{
    for (int i = 0; i < span.Length; i++)
    {
        span[i] = (char)(firstChar + i);
    }
});

Console.WriteLine(result); // abcde
// </UsingStringCreate>

sample · vbdocs/standard/base-types/snippets/creating-new/vb/Program.vbUsingStringCreate

vb
Imports System

' <UsingStringCreate>
Module Program
    Sub Main()
        Dim result As String = String.Create(5, "a"c, Sub(span, firstChar)
                                                           For i As Integer = 0 To span.Length - 1
                                                               span(i) = ChrW(AscW(firstChar) + i)
                                                           Next
                                                       End Sub)

        Console.WriteLine(result) ' abcde
    End Sub
End Module
' </UsingStringCreate>

String.Create is designed for performance-sensitive scenarios where you know the final string length in advance and want to avoid allocating intermediate character buffers. The runtime allocates a new string, passes its backing buffer directly to your callback as a Span<char>, and returns the immutable string once the callback returns. No copy of the data occurs after the callback completes.

String.Create vs. new String(Span<char>)

Another option for building strings efficiently is to allocate a character buffer with stackalloc, fill it, and pass it to the String(ReadOnlySpan<char>) constructor:

sample · csharpdocs/standard/base-types/snippets/creating-new/csharp/Program.csUsingSpanConstructor

csharp
using System;

// <UsingSpanConstructor>
static string CreateStringFromSpan()
{
    Span<char> span = stackalloc char[5];
    for (int i = 0; i < 5; i++)
    {
        span[i] = (char)('a' + i);
    }
    return new string(span);
}

Console.WriteLine(CreateStringFromSpan()); // abcde
// </UsingSpanConstructor>

// <UsingStringCreate>
string result = string.Create(5, 'a', (span, firstChar) =>
{
    for (int i = 0; i < span.Length; i++)
    {
        span[i] = (char)(firstChar + i);
    }
});

Console.WriteLine(result); // abcde
// </UsingStringCreate>

Both approaches allocate the final string exactly once. The key differences are:

  • stackalloc + new string(span) places the working buffer on the stack. This is fastest for small, fixed-size buffers, but the stack is a finite resource; large or deeply nested allocations can cause a StackOverflowException. This example shows the C# stackalloc pattern; Visual Basic doesn't support stackalloc, but it can still call the String(ReadOnlySpan<char>) constructor when you have a ReadOnlySpan<char>.
  • String.Create allocates the working buffer on the heap as part of the string object itself, so there's no stack pressure. It also accepts a typed state parameter that the runtime passes to your callback without boxing, avoiding boxing allocations when the state is a reference type or a non-captured struct. In general, prefer stackalloc + new String(span) for small strings (typically fewer than a few hundred characters) with a known, bounded size. Use String.Create when the size might be large, when you want to avoid stack pressure, or when passing state into the callback without boxing.

See also