Training
Module
Perform basic string formatting in C# - Training
Combine literal and variable text data that contain special characters, formatting, and Unicode into meaningful messages for the end user.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
.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.
The easiest way to create a new String object is simply to assign a string literal to a String object.
You can use overloads of the 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 following table lists several useful methods that return new string objects.
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. |
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.
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.
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.
In this example,DateTime.Now displays the current date and time in a manner specified by the culture associated with the current thread.
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.
string helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
// Hello World!
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!
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.
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!
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!
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.
string sentence = "Once a time.";
Console.WriteLine(sentence.Insert(4, " upon"));
// The example displays the following output:
// Once upon a time.
Dim sentence As String = "Once a time."
Console.WriteLine(sentence.Insert(4, " upon"))
' The example displays the following output:
' Once upon a time.
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.
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
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
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Perform basic string formatting in C# - Training
Combine literal and variable text data that contain special characters, formatting, and Unicode into meaningful messages for the end user.
Documentation
Learn about strings in C# programming. See information on declaring and initializing strings, the immutability of string objects, and string escape sequences.
Using the StringBuilder Class in .NET - .NET
Learn how to use the StringBuilder class in .NET. Use this class to modify a string without creating a new object.
How to modify string contents - C#
Review examples of several techniques to modify existing string contents in C#, which return a new string object.
How to determine whether a string represents a numeric value - C#
Learn how to determine whether a string is a valid representation of a specified numeric type. See code examples and view additional resources.