Share via


Creating New Strings

The .NET Framework provides several methods in the System.String class that create new string objects by combining several strings, arrays of strings, or objects. The following table lists several useful methods.

Method name Use
String.Format Builds a formatted string from a set of input objects.
String.Concat Builds strings from two or more strings.
String.Join Builds a new string by combining an array of strings.
String.Insert Builds a new string by inserting a string into the specified index of an existing string.
String.CopyTo Copies specified characters in a string into a specified position in an array of characters.

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.

Dim MyInt As Integer = 12
Dim MyString As String = [String].Format("Your dog has {0} fleas. It is time to get a flea collar. The current universal date is: {1:u}." , MyInt, DateTime.Now)
Console.WriteLine(MyString)
[C#]
int MyInt = 12;
string MyString = String.Format("Your dog has {0} fleas. It is time to get a flea collar. The current universal date is: {1:u}." , MyInt, DateTime.Now);
Console.WriteLine(MyString);

The preceding example displays the text 'Your dog has 12 fleas. It is time to get a flea collar. The current universal date is: 2001-04-10 15:51:24Z.' to the console. DateTime.Now displays the current date and time in a manner specified by the culture associated with current thread.

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.

Dim MyString As String = "Hello"
Dim YourString As String = "World!"
Console.WriteLine(String.Concat(MyString, " "c, YourString))
[C#]
string MyString = "Hello";
string YourString = "World!";
Console.WriteLine(String.Concat(MyString, ' ', YourString));

This code displays Hello World! to the console.

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.

Dim MyString As String() =  {"Hello", "and", "welcome", "to", "my" , "world!"}
Console.WriteLine(String.Join(" ", MyString))
[C#]
string[] MyString = {"Hello", "and", "welcome", "to", "my" , "world!"};
Console.WriteLine(String.Join(" ", MyString));

This code displays Hello and welcome to my world! to the console.

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.

Dim MyString As String = "Once a time"
Console.WriteLine(MyString.Insert(4, " upon"))
[C#]
string MyString = "Once a time.";   
Console.WriteLine(MyString.Insert(4, " upon"));

This code displays Once upon a time. to the console.

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.

Dim MyString As String = "Hello World!"
Dim MyCharArray As Char() =  {"W"c, "h"c, "e"c, "r"c, "e"c}
Console.WriteLine("The original character array: {0}", MyCharArray)
MyString.CopyTo(0, MyCharArray, 0, 5)
Console.WriteLine("The new character array: {0}", MyCharArray)
[C#]
string MyString = "Hello World!";
char[] MyCharArray = {'W','h','e','r','e'};
Console.WriteLine("The original character array: {0}", MyCharArray);
MyString.CopyTo(0, MyCharArray,0 ,5);
Console.WriteLine("The new character array: {0}", MyCharArray);

This code displays the following to the console.

The original character array: Where
The new character array: Hello

See Also

Basic String Operations | Composite Formatting