Udemy logo

javadatatypesWhen you create a programming variable, you must define this variable’s data type. A data type tells the compiler what type of data the variable will contain. When you define the data type, it locks in the type of data allowed (although, you can convert this data type later in your program). A C# string is a data type that allows you to contain characters.

Get started with C# more quickly with an online training course

Defining a C# String

The following code shows you how to define a string in C#:

string mystring = string.empty;

In the above line of code, a simple string is created and set to “string.empty.” The string.empty declaration set the string to “empty” while still creating a memory location for it.

Of course, if you define a string, you probably want to use it at some point in your code. You can assign the new variable any data as long as it’s a string data type. For instance, the following code sets a value for the string:

mystring = “this is my value”;

Now, if you were to print on the above line of code, it would print out the exact string value.

As stated, you cannot assign a string variable a different data type than the data type defined when you create a variable. For instance, the following statement will result in an error:

mystring = 1;

The above code attempts to store an integer in the string variable. The above line of code will throw a compiler error. However, if you really need to store a number in a string (which happens sometimes), you can convert your “1” to a string. For instance, the following are two ways you can store a number in a string variable:

mystring = “1”;

OR

mystring = Convert.ToString(1);

The “Convert” class lets you convert from several data types to several data types. The above instance is one example of converting from an integer data type to a string data type.

Manipulating Strings

In most programs, you won’t just assign a string variable and then never use the variable again. In most cases, you’ll need to manipulate your string variables. You can concatenate, delete, remove, and search for characters in the string variable.

Understand strings and your C# compiler at Udemy.com

The first common way you will work with strings is searching for a value within a string. You can use the “Contains” or “IndexOf” methods. You typically use these two methods with an “if” statement. If a specific value is found, then you execute certain code. If the value isn’t found, you then execute an alternative block of code.

The following code shows you an example of the “Contains” method:

if (mystring.Contains(“value”))
{
Console.Write(“The value was found”);
}

In the above line of code, the “Contains” method searches for the value “value” in the string. If it’s found, then the statement “The value was found” is written to the console. Note that strings are case sensitive in your code. This means that “value,” “Value,” and “VaLuE” are all different values to your C# compiler. For instance, if you used the if statement above and the string contained “VALUE,” the if statement would return false.

To get around this issue, you can use the “ToLower” and “ToUpper” functions. These two functions convert strings to either all lower or upper case. The following is an example of assigning a temporary variable with the string “Value” in all lower case characters:

string myTempVar = mystring.ToLower();

Conversely, the following statement would convert the string to all upper case characters:

string myTempVar = mystring.ToUpper();

In addition to “Contains,” you can also use “IndexOf.” The difference between these two methods is that “Contains” returns a true or false value and “IndexOf” returns the location in the string where the value is found. Just like Contains, IndexOf is also case sensitive, so you might need to apply the ToUpper or ToLower methods to ensure you find the value if you don’t care about case sensitivity.

The following is an example of using the if statement to determine if a string of characters is found:

if (mystring.IndexOf(“value”) > -1)
{
Console.Write(“The value was found a ” + mystring.IndexOf(“value”).ToString());
}

Notice in this statement the value returned by IndexOf must be greater than -1. The reason it is -1 instead of zero is that string arrays start at index 0. Therefore, if the value is found at the first character, the value returned by IndexOf is 0.

If the string is found, the compiler prints out the location of where in the string the value is found. Notice the “ToString” function. This function is used, because the IndexOf returns an integer data type. To avoid any bugs, you should convert any printed values to strings. The “ToString” method can be applied to almost any data type, so you can quickly turn your variables to strings and print them to the console or the web page.

There are times you might want to concatenate or join two strings. The following code concatenates a first and last name to make a full name string:

string first_name = “Jennifer”;
string last_name = “Smith”;
string full_name = first_name + “ “ + last_name;

The above code first creates a first and last name variable. Then, the two strings are concatenated (with a space character in between them). The “full_name” variable contains “Jennifer Smith” as a value. If you did not add the space character, the full_name variable would print out “JenniferSmith.”

These are just a few string functions you will encounter in your coding career. Strings are an integral part of all programming languages. Once you learn how to use strings in one language, you can quickly pick up on how to use strings in other languages such as C, C++, Java and VB.NET. For a full understanding of how C# works and to learn more about string values, take a course at Udemy.com and learn advanced C# string concepts.

Page Last Updated: April 2014

Top courses in C# (programming language)

Design Patterns in C# Made Simple
Zoran Horvat
4.7 (404)
Ultimate C# Masterclass for 2024
Krystyna Ślusarczyk
4.7 (1,031)
Bestseller
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (40,269)
Bestseller
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.4 (3,537)
Bestseller
Unity RPG Inventory Systems Asset Pack: Behind The Scenes
GameDev.tv Team, Rick Davidson
4.3 (837)
Advanced Topics in C#
Dmitri Nesteruk
4.5 (555)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.8 (206)
Bestseller
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (27,247)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.4 (11,223)
Bestseller

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo