Tutorials Icon

Tutorials > C# Strings

C# Strings

By Stephen Armstrong // November 23, 2019

How to add text to your game.

Strings are text. They are used in games to contain in-game elements such as dialogue and menu text, and behind-the-scenes elements such as save file locations.

Creating and using strings

Use the following C# code to create a string called exampleString that contains the following text: “This-Is-A-New-String”

string exampleString = "This-Is-A-New-String";

Strings have numerous properties/methods that you can use in your game. The following table is a brief overview, including example code and results.

.Contains()bool containsTest = exampleString.Contains(“new-st”);Checks if a string contains the specified string (is case sensitive).containsTest is False.
bool containsTest = exampleString.Contains(“New-St”);containsTest is True.
.ToUpper()string upperString = exampleString.ToUpper();CHANGES THE ENTIRE STRING TO UPPER CASE.upperString == “THIS-IS-A-NEW-STRING”
.ToLower()string lowerString = exampleString.ToLower();changes the entire string to lower case.lowerString == “this-is-a-new-string”
.Substring()string substringTest = exampleString.Substring(9);Gets everything after the ninth character.substringTest == “-New-String”
string substringTest = exampleString.Substring(9,4);Gets the first four characters after the ninth character.substringTest == “-New”
.Insert()string insertTest = exampleString.Insert(8,“Not-”);Inserts “Not-” into the string after the eighth character.insertTest == “This-Is-Not-A-New-String”
.StartsWith()bool startsTest = exampleString.StartsWith(“T”)Checks if the string starts with the specified string (is case sensitive).startsTest is True.
bool startsTest = exampleString.StartsWith(“t”)startsTest is False.
.EndsWith()bool endsTest = exampleString.EndsWith(“G”);Checks if the string ends with the specified string (is case sensitive).endsTest is False.
bool endsTest = exampleString.EndsWith(“g”);endsTest is True.
.Length()int testLength = exampleString.Length;The total length of the string (including spaces).testLength == 20
.Remove()string removeTest = exampleString.Remove(4);Removes everything after the fourth character.removeTest == “This”
string removeTest = exampleString.Remove(4,5);Removes the five characters that come after the fourth character.removeTest == “This-New-String”
.Replace()string replaceTest = exampleString.Replace(”-”,” ”);Replaces every dash (-) in the string with a space.replaceTest == “This Is A New String”
.Split()string[] splitTest = exampleString.Split(new char[] i);Splits the string into a string array. The split point is the character “i”.splitTest[0] == “Th”
splitTest[1] == “s-Is-A-New-Str”
splitTest[2] == “ng”
string[] splitTest = exampleString.Split(new char[] i, 2);Splits the string into a string array with a maximum length of two entries.
The split point is the character “i”.
splitTest[0] == “Th”
splitTest[1] == “s-Is-A-New-String”

As an overview, this table should provide you with some idea of how to use these properties/methods in your MonoGame project. Feel free to experiment with them to see what you can do!

Adding to a string

Strings use the same += operator as numbers, so it’s very easy to add extra content to a string

// Add content to the end of the string
exampleString += "-With-A-Little-Extra";

System.Console.WriteLine("Added extra content to exampleString: {0}", exampleString);

If you run that code, it will display the appended string in the Output.

< Go back

Return to top of page

Article Icon

Welcome to Industrian.net!

On this website you'll find more information about our games, and also some tutorials for you to start making games of your own! You can also follow us on various social platforms!