Strings in C#
In this part of the C# tutorial, we will work with string data in more detail.
Strings are the most important data types in computer languages. That is why we dedicate a whole chapter to working with strings in C#.
A string is a sequence of characters. In C#, a string is a sequence of unicode characters. It is a data type which stores a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding. When a string appears literally in source code, it is known as a string literal.
First example
In C# string literals are enclosed by double quotes.
using System;
public class CSharpApp
{
static void Main()
{
string str1 = "There are 10";
string str2 = " apples";
Console.WriteLine(str1 + str2);
Console.WriteLine("The length of the first string is "
+ str1.Length.ToString() + " characters");
}
}
In the preceding example, we create two string variables. Then we add them and compute the length of the first string.
string str1 = "There are 10";
A string variable is declared and initiated.
Console.WriteLine(str1 + str2);
Two strings are concatenated. We use the + operator to add two strings.
Console.WriteLine("The length of the first string is "
+ str1.Length.ToString() + " characters");
The Length property is used to
determine the length of the string.
$ ./basics.exe There are 10 apples The length of the first string is 12 characters
Running the example gives this result.
Using quotes
What if we wanted to display quotes, for example in a direct speech? In such a case, inner quotes must be escaped.
using System;
public class CSharpApp
{
static void Main()
{
Console.WriteLine("There are many stars.");
Console.WriteLine("He said, \"Which one is your favourite?\"");
}
}
We use the (\) character to escape additional quotes.
$ ./quotes.exe There are many stars. He said, "Which one is your favourite?"
Output.
Multiline strings
It is possible to create a multiline string in C#.
using System;
public class CSharpApp
{
static void Main()
{
string multiString = @"I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good";
Console.WriteLine(multiString);
}
}
A multiline string can be created easily by using the (@) character at the beginning of the string.
$ ./multiline.exe I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good
Comparing strings
We can compare two strings with a (==) operator.
using System;
public class CSharpApp
{
static void Main()
{
Console.WriteLine("12" == "12");
Console.WriteLine("17" == "9");
Console.WriteLine("aa" == "ab");
}
}
Comparing strings.
$ ./compare.exe True False False
Output of the program.
There is a string.Compare() method, which compares two specified strings
and returns an integer that indicates their relative position in the sort order.
If the returned value is less than zero, the first string is less than the second. If
it returns zero, both strings are equal. Finally, if the returned value is greater than
zero, the first string is greater than the second.
using System;
public class CSharpApp
{
static void Main()
{
string str1 = "ZetCode";
string str2 = "zetcode";
Console.WriteLine(string.Compare(str1, str2, true));
Console.WriteLine(string.Compare(str1, str2, false));
}
}
There is an optional third ignoreCase argument, that determines, if the case should be honored or not.
Console.WriteLine(string.Compare(str1, str2, true));
Compare two strings and ignore the case. This line prints 0 to the console.
String methods
C# has useful built-in methods that can be used for working with strings.
The Join() and Split()
are very handy methods.
using System;
public class CSharpApp
{
static void Main()
{
string[] items = new string[] {"C#", "Visual Basic",
"Java", "Perl"};
string langs = string.Join(",", items);
Console.WriteLine(langs);
string[] ls = langs.Split(',');
foreach (string lang in ls)
{
Console.WriteLine(lang);
}
}
}
In our program, we will join and split strings.
string[] items = new string[] {"C#", "Visual Basic",
"Java", "Perl"};
This is an array of strings. These strings are going to be joined.
string langs = string.Join(",", items);
All words from the array are joined. We build one string from them. There will be a comma character between each two words.
string[] ls = langs.Split(',');
As a reverse operation, we split the langs string. The
Split() method returns an array of
words, delimited by a character. In our case it is a
comma character.
foreach (string lang in ls)
{
Console.WriteLine(lang);
}
We go through the array and print its elements.
$ ./joinsplit.exe C#,Visual Basic,Java,Perl C# Visual Basic Java Perl
Output of the example.
using System;
public class CSharpApp
{
static void Main()
{
string str = "Determination";
Console.WriteLine(str.Contains("e"));
Console.WriteLine(str.IndexOf("e"));
Console.WriteLine(str.LastIndexOf("i"));
Console.WriteLine(str.ToUpper());
Console.WriteLine(str.ToLower());
}
}
We introduce five string methods in the above example.
Console.WriteLine(str.Contains("e"));
The Contains() method returns True
if the string contains a specific character.
Console.WriteLine(str.IndexOf("e"));
The IndexOf() returns the first index
of a letter in the string.
Console.WriteLine(str.LastIndexOf("i"));
The LastIndexOf() methods
returns the last index of a letter in a string.
Console.WriteLine(str.ToUpper()); Console.WriteLine(str.ToLower());
Letters of the string are converted to uppercase with
the ToUpper() method and to lowercase
with the ToLower() method.
$ ./strmethods.exe True 1 10 DETERMINATION determination
Running the program.
Copy vs Clone
We will describe a difference between two methods. Copy()
and Clone(). The Copy() method creates
a new instance of string with the same value as a specified string.
The Clone() method returns a reference
to the string, which is being cloned. It is not an independent
copy of the string on the Heap. It is another reference on the same
string.
using System;
public class CSharpApp
{
static void Main()
{
string str = "ZetCode";
string cloned = (string) str.Clone();
string copied = string.Copy(str);
Console.WriteLine(str.Equals(cloned)); // prints true
Console.WriteLine(str.Equals(copied)); // prints true
Console.WriteLine(ReferenceEquals(str, cloned)); // prints true
Console.WriteLine(ReferenceEquals(str, copied)); // prints false
}
}
Our example demonstrates the difference between the two methods.
string cloned = (string) str.Clone(); string copied = string.Copy(str);
The string value is cloned and copied.
Console.WriteLine(str.Equals(cloned)); // prints true Console.WriteLine(str.Equals(copied)); // prints true
The Equals() method determines whether
two string objects have the same value. The contents of
all three strings are the same.
Console.WriteLine(ReferenceEquals(str, cloned)); // prints true Console.WriteLine(ReferenceEquals(str, copied)); // prints false
The ReferenceEquals() method compares
the two reference objects. Therefore comparing a
copied string to the original string returns false.
Because they are two distinct objects.
Formatting strings
In the next examples, we will format strings. The .NET Framework has a feature called composite formatting. It is supported by Format() and WriteLine() methods. A method takes a list of objects and a composite format string as input. The format string consists of fixed string plus some format items. These format items are indexed placeholders which correspond to the objects in the list.
The format item has the following syntax:
{index[,length][:formatString]}
The index component is mandatory. It is a number starting from 0 that refers to an item from the list of objects. Multiple items can refer to the same element of the list of objects. An object is ignored, if it is not referenced by a format item. If we refer outside the bounds of the list of objects, a runtime exception is thrown.
The length component is optional. It is the minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned. If it is specified, there must by a colon separating the index and the length.
The formatString is optional. It is a string that formats a value is a specific way. It can be used to format dates, times, numbers or enumerations.
Here we show, how to work with length component of the format items. We print three columns of numbers to the terminal. Left, middle and right aligned.
using System;
public class CSharpApp
{
static void Main()
{
int oranges = 2;
int apples = 4;
int bananas = 3;
string str1 = "There are {0} oranges, {1} apples and " +
"{2} bananas";
string str2 = "There are {1} oranges, {2} bananas and " +
"{0} apples";
Console.WriteLine(str1, oranges, apples, bananas);
Console.WriteLine(str2, apples, oranges, bananas);
}
}
We print a simple message to the console. We use only index component of the format item.
string str1 = "There are {0} oranges, {1} apples and " +
"{2} bananas";
The {0}, {1}, and {2} are format items. We specify the index component. Other components are optional.
Console.WriteLine(str1, oranges, apples, bananas);
Now we put together the composite formatting. We have the string and
the list of objects (oranges, apples, bananas). The {0} format item
refers to the oranges. The WriteLine() method
replaces the {0} format item with the contents of the oranges
variable.
string str2 = "There are {1} oranges, {2} bananas and " +
"{0} apples";
The order of the format items referring to the objects is notation important.
$ ./format1.exe There are 2 oranges, 4 apples and 3 bananas There are 2 oranges, 3 bananas and 4 apples
using System;
public class CSharpApp
{
static void Main()
{
Console.WriteLine("{0} {1, 12}",
"Decimal", "Hexadecimal");
Console.WriteLine("{0:D} {1,8:X}",
502, 546);
Console.WriteLine("{0:D} {1,8:X}",
345, 765);
Console.WriteLine("{0:D} {1,8:X}",
320, 654);
Console.WriteLine("{0:D} {1,8:X}",
120, 834);
Console.WriteLine("{0:D} {1,8:X}",
620, 454);
}
}
We print numbers in a decimal and hexadecimal format. We also align the numbers using the length component.
Console.WriteLine("{0:D} {1,8:X}",
502, 546);
The {0:D} format item specifies, the first item from the list of supplied objects will be taken and formatted in the decimal format. The {1,8:X} format item takes the second item. Formats it in the hexadecimal format (:X). And the string length will be 8 characters (,8). Because the number has only three characters, it is right aligned and padded with empty strings.
$ ./format2.exe Decimal Hexadecimal 502 222 345 2FD 320 28E 120 342 620 1C6
Running the example.
The last two examples will format numeric and date data.
using System;
public class CSharpApp
{
static void Main()
{
Console.WriteLine(string.Format("Number: {0:N}", 126));
Console.WriteLine(string.Format("Scientific: {0:E}", 126));
Console.WriteLine(string.Format("Currency: {0:C}", 126));
Console.WriteLine(string.Format("Percent: {0:P}", 126));
Console.WriteLine(string.Format("Hexadecimal: {0:X}", 126));
}
}
The example demonstrates the standard formatting specifiers for numbers. Number 126 is printed in five different formats; normal, scientific, currency, percent and hexadecimal.
$ ./format3.exe Number: 126.00 Scientific: 1.260000E+002 Currency: $126.00 Percent: 12,600.00 % Hexadecimal: 7E
Output.
Finally, we will format date and time data.
using System;
public class CSharpApp
{
public static void Main()
{
DateTime today = DateTime.Now;
Console.WriteLine(string.Format("Short date: {0:d}", today));
Console.WriteLine(string.Format("Long date: {0:D}", today));
Console.WriteLine(string.Format("Short time: {0:t}", today));
Console.WriteLine(string.Format("Long time: {0:T}", today));
Console.WriteLine(string.Format("Month: {0:M}", today));
Console.WriteLine(string.Format("Year: {0:Y}", today));
}
}
The preceding example demonstrates the standard formatting specifiers for dates.
$ ./format4.exe Short date: 4/11/2011 Long date: Monday, April 11, 2011 Short time: 11:14 AM Long time: 11:14:54 AM Month: April 11 Year: April, 2011
Output.
This part of the C# tutorial covered strings.