Data Types: Character and String

Character and especially strings are data types that will be used most of the time in your program so it is important to have good knowledge about them. In today's post we will discuss these both Data types.

Character

In C#, Character is declared by using “char” keyword. Character has two bytes to contain any single Unicode character (Unicode is International standard for storing wide variety of character).

Example
char a = 's';
  1. You can initialize a character by assigning it a value in single quote.
  2. You can assign any lower letter, upper letter, a number or punctuation.
  3. You can’t surround a character in double quote that is specified for strings. You can’t hold more than one character in a single variable of character because that would make it a string and you should use string variable for that.
  4. Char is basically an alias to System.Char which means when you create a variable of type char, you can perform some functions that are already built in. For example
char.isDigit(a);	//checks if a is digit
char.isLetter(a);	//checks if a is letter
char.isPunctuation(a);	//checks if a is puctuation
char.isWhiteSpace(a);	//checks if a is white space
char.ToUpper(a);	//change a to upper case if possible
char.isLower(a);	//changes a to lower case if possible

Strings

In C#, strings are used to store a series of Unicode characters. You can use “string” keyword to declare a string.

Example
string country = "Pakistan";
You can initialize a string by assigning it a value in double quote. Value can be any series of Unicode characters.

Escape Sequences

Escape sequence is a series of character that behave differently than they look. When escape character (i.e. back slash ‘’) is followed by some special character code than they act like a command rather than data. Some basic escape sequences are given below.
\n			new line
\t			tab
\v			vertical tab
\r			carriage return
\b			back space
\			\character
\’			‘ character
\”			“ character
\?			? character
Your string can contain escape sequence.

Example
string message = "Notice the use of n next line escape sequence.";
Console.WriteLine(message);
Output
Character and Strign

@ Symbol and Avoiding Escape sequence

You could be in situation where you want to actually use escape sequence as data rather than command. For example you have string like “I want to write \t” and you want same output as your string. For this purpose you can use @ symbol before your string value. It tells your C# compiler that don’t interpret any escape sequence and use the value same as given.

Example
string message2 = @"The \n won't take us to next line.";
Console.WriteLine(message2);
Output
Character and Strign


This is extremely useful when dealing with directory addresses because by doing it in normal way you always have to use escape sequence to show back slash ‘\’ but in later case you can give directory address without putting some extra effort and also it gives better look.

Example
string address = "C:\\Program Files\\test.txt";
string address2 = @"C:\Program Files\test.txt";
Both are same addresses but second method is recommended.

String is Object

Same as character string is also an object. It is alias to System.String and you can perform built-in functions. For example
string message = "test string";

message.ToUpper();	//output: TEST STRING
message.Contains(“test”); //return: true
message.Substring(5);	//output: string
And there are many functions that you can use according to your needs.

Strings are Immutable

When you declare a string, value can be assigned to it. You are able to change that value at any point in your program or can modify it. But that is not what actually happens because strings are immutable which means once the value is assigned to variable you can’t change it. When you do it C# actually create a new string and assign the value to it but it looks like the string is just modified. So if you are coding in which you need to alter your string many time than you should use StringBuilder class rather than string. That will lead you to better performance.

Example
string alpha = "a";
alpha += "b";
alpha += "c";
Console.WriteLine(alpha);

StringBuilder alpha2 = new StringBuilder();
alpha2.Append("a");
alpha2.Append("b");
alpha2.Append("c");
Console.WriteLine(alpha2);
Output for both alpha and alpha2 will be same but alpha2 will be faster and recommended.

<< Data Types: Numbers and Double vs Decimal
Tags: C-sharp
comments powered by Disqus