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.
Example
Example
Example
Example
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
Example
<< Data Types: Numbers and Double vs Decimal
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';
- You can initialize a character by assigning it a value in single quote.
- You can assign any lower letter, upper letter, a number or punctuation.
- 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.
- 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 \? ? characterYour string can contain escape sequence.
Example
string message = "Notice the use of n next line escape sequence."; Console.WriteLine(message);Output
@ 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
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 examplestring message = "test string"; message.ToUpper(); //output: TEST STRING message.Contains(“test”); //return: true message.Substring(5); //output: stringAnd 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

Muhammad Abid
Muhammad Abid is software engineer, a passionate programmer who have created many projects in C# and Java.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
CSV
Validation
Android
Java
Nashorn
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE