Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Learn Microsoft.NET

Posted by Ravi Varma Thumati on February 9, 2015

Microsoft.NET

In this series of articles, we will understand the fundamentals of Microsoft.NET, Core components of .NET, Framework versions, Assemblies and experiencing the new enhanced development environment Visual Studio.

Start learning .Microsoft.NET now

C#.NET

In this series of articles, we will learn the fundamentals of C# programming language. Object Oriented Programming using C# and understanding Collections, Generics, Delegates, File systems and Exception handling.

Start learning C# Programming now

ASP.NET

In this series of articles, we will understand the fundamentals of Web Programming, Introduction to ASP.NET web applications, working with Server Controls, managing the state, working with the Data Controls, applying styles to the web applications and configuring the web applications.

Start leaning ASP.NET Programming now

Posted in 1. Microsoft.NET | Tagged: | Leave a Comment »

Strings in C#

Posted by Ravi Varma Thumati on February 12, 2015

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters (”). The Length property of a string represents the number of Char objects it contains, not the number of Unicode characters. To access the individual Unicode code points in a string, use the StringInfo object.

string vs. System.String

In C#, the string keyword is an alias for String. Therefore, String and string are equivalent, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. In addition, the C# language overloads some operators to simplify common string operations.

Declaring and Initializing Strings

You can declare and initialize strings in various ways, as shown in the following example:

/

// Declare without initializing.
string message1;

// Initialize to null.
string message2 = null;

// Initialize as an empty string.
// Use the Empty constant instead of the literal “”.
string message3 = System.String.Empty;

//Initialize with a regular string literal.
string oldPath = “c:\\Program Files\\Microsoft Visual Studio 8.0″;

// Initialize with a verbatim string literal.
string newPath = @”c:\Program Files\Microsoft Visual Studio 9.0”;

// Use System.String if you prefer.
System.String greeting = “Hello World!”;

// In local variables (i.e. within a method body)
// you can use implicit typing.
var temp = “I’m still a strongly-typed System.String!”;

// Use a const string to prevent ‘message4’ from
// being used to store another string value.
const string message4 = “You can’t get rid of me!”;

// Use the String constructor only when creating
// a string from a char*, char[], or sbyte*. See
// System.String documentation for details.
char[] letters = { ‘A’, ‘B’, ‘C’ };
string alphabet = new string(letters);

Note that you do not use the new operator to create a string object except when initializing the string with an array of chars.

Initialize a string with the Empty constant value to create a new String object whose string is of zero length. The string literal representation of a zero-length string is “”. By initializing strings with the Empty value instead of null, you can reduce the chances of a NullReferenceException occurring. Use the static IsNullOrEmpty(String) method to verify the value of a string before you try to access it.

Immutability of String Objects

String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

string s1 = “A string is more “;
string s2 = “than the sum of its chars.”;

// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;

System.Console.WriteLine(s1);
// Output: A string is more than the sum of its chars.

Because a string “modification” is actually a new string creation, you must use caution when you create references to strings. If you create a reference to a string, and then “modify” the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. The following code illustrates this behavior:

string s1 = “Hello “;
string s2 = s1;
s1 += “World”;

System.Console.WriteLine(s2);
//Output: Hello

Regular and Verbatim String Literals

Use regular string literals when you must embed escape characters provided by C#, as shown in the following example:

string columns = “Column 1\tColumn 2\tColumn 3”;
//Output: Column 1 Column 2 Column 3

string rows = “Row 1\r\nRow 2\r\nRow 3”;
/* Output:
Row 1
Row 2
Row 3
*/

string title = “\”The \u00C6olean Harp\”, by Samuel Taylor Coleridge”;
//Output: “The Æolean Harp”, by Samuel Taylor Coleridge

Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:

string filePath = @”C:\Users\scoleridge\Documents\”;
//Output: C:\Users\scoleridge\Documents\

string text = @”My pensive SARA ! thy soft cheek reclined
Thus on mine arm, most soothing sweet it is
To sit beside our Cot,…”;
/* Output:
My pensive SARA ! thy soft cheek reclined
Thus on mine arm, most soothing sweet it is
To sit beside our Cot,…
*/

string quote = @”Her name was “”Sara.”””;
//Output: Her name was “Sara.”

String Escape Sequences

Escape sequence Character name Unicode encoding
\’ Single quote 0x0027
\” Double quote 0x0022
\\ Backslash 0x005C
Null 0x0000
\a Alert 0x0007
\b Backspace 0x0008
\f Form feed 0x000C
\n New line 0x000A
\r Carriage return 0x000D
\t Horizontal tab 0x0009
\U Unicode escape sequence for surrogate pairs. \Unnnnnnnn
\u Unicode escape sequence \u0041 = “A”
\v Vertical tab 0x000B
\x Unicode escape sequence similar to “\u” except with variable length. \x0041 = “A”

Format Strings

A format string is a string whose contents can be determined dynamically at runtime. You create a format string by using the static Format method and embedding placeholders in braces that will be replaced by other values at runtime. The following example uses a format string to output the result of each iteration of a loop:

class FormatString
{
static void Main()
{
// Get user input.
System.Console.WriteLine(“Enter a number”);
string input = System.Console.ReadLine();

// Convert the input string to an int.
int j;
System.Int32.TryParse(input, out j);

// Write a different string each iteration.
string s;
for (int i = 0; i < 10; i++)
{
// A simple format string with no alignment formatting.
s = System.String.Format(“{0} times {1} = {2}”, i, j, (i * j));
System.Console.WriteLine(s);
}

//Keep the console window open in debug mode.
System.Console.ReadKey();
}
}

One overload of the WriteLine method takes a format string as a parameter. Therefore, you can just embed a format string literal without an explicit call to the method. However, if you use the WriteLine method to display debug output in the Visual Studio Output window, you have to explicitly call the Format method because WriteLine only accepts a string, not a format string. For more information about format strings, see Formatting Types.

Substrings

A substring is any sequence of characters that is contained in a string. Use the Substring method to create a new string from a part of the original string. You can search for one or more occurrences of a substring by using the IndexOf method. Use the Replace method to replace all occurrences of a specified substring with a new string. Like the Substring method, Replace actually returns a new string and does not modify the original string.

string s3 = “Visual C# Express”;
System.Console.WriteLine(s3.Substring(7, 2));
// Output: “C#”

System.Console.WriteLine(s3.Replace(“C#”, “Basic”));
// Output: “Visual Basic Express”

// Index values are zero-based
int index = s3.IndexOf(“C”);
// index = 7

Accessing Individual Characters

You can use array notation with an index value to acquire read-only access to individual characters, as in the following example:

string s5 = “Printing backwards”;

for (int i = 0; i < s5.Length; i++)
{
System.Console.Write(s5[s5.Length – i – 1]);
}
// Output: “sdrawkcab gnitnirP”

If the String methods do not provide the functionality that you must have to modify individual characters in a string, you can use a StringBuilder object to modify the individual chars “in-place”, and then create a new string to store the results by using the StringBuilder methods. In the following example, assume that you must modify the original string in a particular way and then store the results for future use:

string question = “hOW DOES mICROSOFT wORD DEAL WITH THE cAPS lOCK KEY?”;
System.Text.StringBuilder sb = new System.Text.StringBuilder(question);

for (int j = 0; j < sb.Length; j++)
{
if (System.Char.IsLower(sb[j]) == true)
sb[j] = System.Char.ToUpper(sb[j]);
else if (System.Char.IsUpper(sb[j]) == true)
sb[j] = System.Char.ToLower(sb[j]);
}
// Store the new string.
string corrected = sb.ToString();
System.Console.WriteLine(corrected);
// Output: How does Microsoft Word deal with the Caps Lock key?

Null Strings and Empty Strings

An empty string is an instance of a System.String object that contains zero characters. Empty strings are used often in various programming scenarios to represent a blank text field. You can call methods on empty strings because they are valid System.String objects. Empty strings are initialized as follows:

string s = String.Empty;

By contrast, a null string does not refer to an instance of a System.String object and any attempt to call a method on a null string causes a NullReferenceException. However, you can use null strings in concatenation and comparison operations with other strings. The following examples illustrate some cases in which a reference to a null string does and does not cause an exception to be thrown:

static void Main()
{
string str = “hello”;
string nullStr = null;
string emptyStr = String.Empty;

string tempStr = str + nullStr;
// Output of the following line: hello
Console.WriteLine(tempStr);

bool b = (emptyStr == nullStr);
// Output of the following line: False
Console.WriteLine(b);

// The following line creates a new empty string.
string newStr = emptyStr + nullStr;

// Null strings and empty strings behave differently. The following
// two lines display 0.
Console.WriteLine(emptyStr.Length);
Console.WriteLine(newStr.Length);
// The following line raises a NullReferenceException.
//Console.WriteLine(nullStr.Length);

// The null character can be displayed and counted, like other chars.
string s1 = “\x0” + “abc”;
string s2 = “abc” + “\x0”;
// Output of the following line: * abc*
Console.WriteLine(“*” + s1 + “*”);
// Output of the following line: *abc *
Console.WriteLine(“*” + s2 + “*”);
// Output of the following line: 4
Console.WriteLine(s2.Length);
}

Using StringBuilder for Fast String Creation

String operations in .NET are highly optimized and in most cases do not significantly impact performance. However, in some scenarios such as tight loops that are executing many hundreds or thousands of times, string operations can affect performance. The StringBuilder class creates a string buffer that offers better performance if your program performs many string manipulations. The StringBuilder string also enables you to reassign individual characters, something the built-in string data type does not support. This code, for example, changes the content of a string without creating a new string:

System.Text.StringBuilder sb = new System.Text.StringBuilder(“Rat: the ideal pet”);
sb[0] = ‘C’;
System.Console.WriteLine(sb.ToString());
System.Console.ReadLine();

//Outputs Cat: the ideal pet

In this example, a StringBuilder object is used to create a string from a set of numeric types:

class TestStringBuilder
{
static void Main()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Create a string composed of numbers 0 – 9
for (int i = 0; i < 10; i++)
{
sb.Append(i.ToString());
}
System.Console.WriteLine(sb); // displays 0123456789

// Copy one character of the string (not possible with a System.String)
sb[0] = sb[9];

System.Console.WriteLine(sb); // displays 9123456789
}
}

Properties of the String Class

The String class has the following two properties:

S.N Property Name & Description
1 Chars
Gets the Char object at a specified position in the current String object.
2 Length
Gets the number of characters in the current String object.

Methods of the String Class

The String class has numerous methods that help you in working with the string objects. The following table provides some of the most commonly used methods:

S.N Method Name & Description
1 public static int Compare( string strA, string strB ) 
Compares two specified string objects and returns an integer that indicates their relative position in the sort order.
2 public static int Compare( string strA, string strB, bool ignoreCase ) 
Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true.
3 public static string Concat( string str0, string str1 ) 
Concatenates two string objects.
4 public static string Concat( string str0, string str1, string str2 ) 
Concatenates three string objects.
5 public static string Concat( string str0, string str1, string str2, string str3 ) 
Concatenates four string objects.
6 public bool Contains( string value ) 
Returns a value indicating whether the specified string object occurs within this string.
7 public static string Copy( string str ) 
Creates a new String object with the same value as the specified string.
8 public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count ) 
Copies a specified number of characters from a specified position of the string object to a specified position in an array of Unicode characters.
9 public bool EndsWith( string value ) 
Determines whether the end of the string object matches the specified string.
10 public bool Equals( string value ) 
Determines whether the current string object and the specified string object have the same value.
11 public static bool Equals( string a, string b ) 
Determines whether two specified string objects have the same value.
12 public static string Format( string format, Object arg0 ) 
Replaces one or more format items in a specified string with the string representation of a specified object.
13 public int IndexOf( char value ) 
Returns the zero-based index of the first occurrence of the specified Unicode character in the current string.
14 public int IndexOf( string value ) 
Returns the zero-based index of the first occurrence of the specified string in this instance.
15 public int IndexOf( char value, int startIndex ) 
Returns the zero-based index of the first occurrence of the specified Unicode character in this string, starting search at the specified character position.
16 public int IndexOf( string value, int startIndex ) 
Returns the zero-based index of the first occurrence of the specified string in this instance, starting search at the specified character position.
17 public int IndexOfAny( char[] anyOf ) 
Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
18 public int IndexOfAny( char[] anyOf, int startIndex ) 
Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters, starting search at the specified character position.
19 public string Insert( int startIndex, string value ) 
Returns a new string in which a specified string is inserted at a specified index position in the current string object.
20 public static bool IsNullOrEmpty( string value ) 
Indicates whether the specified string is null or an Empty string.
21 public static string Join( string separator, params string[] value ) 
Concatenates all the elements of a string array, using the specified separator between each element.
22 public static string Join( string separator, string[] value, int startIndex, int count ) 
Concatenates the specified elements of a string array, using the specified separator between each element.
23 public int LastIndexOf( char value ) 
Returns the zero-based index position of the last occurrence of the specified Unicode character within the current string object.
24 public int LastIndexOf( string value ) 
Returns the zero-based index position of the last occurrence of a specified string within the current string object.
25 public string Remove( int startIndex ) 
Removes all the characters in the current instance, beginning at a specified position and continuing through the last position, and returns the string.
26 public string Remove( int startIndex, int count ) 
Removes the specified number of characters in the current string beginning at a specified position and returns the string.
27 public string Replace( char oldChar, char newChar ) 
Replaces all occurrences of a specified Unicode character in the current string object with the specified Unicode character and returns the new string.
28 public string Replace( string oldValue, string newValue ) 
Replaces all occurrences of a specified string in the current string object with the specified string and returns the new string.
29 public string[] Split( params char[] separator ) 
Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array.
30 public string[] Split( char[] separator, int count ) 
Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. The int parameter specifies the maximum number of substrings to return.
31 public bool StartsWith( string value ) 
Determines whether the beginning of this string instance matches the specified string.
32 public char[] ToCharArray()
Returns a Unicode character array with all the characters in the current string object.
33 public char[] ToCharArray( int startIndex, int length ) 
Returns a Unicode character array with all the characters in the current string object, starting from the specified index and up to the specified length.
34 public string ToLower()
Returns a copy of this string converted to lowercase.
35 public string ToUpper()
Returns a copy of this string converted to uppercase.
36 public string Trim()
Removes all leading and trailing white-space characters from the current String object.

The above list of methods is not exhaustive, please visit MSDN library for the complete list of methods and String class constructors.

Examples:

The following example demonstrates some of the methods mentioned above:

Comparing Strings:

using System;

namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str1 = “This is test”;
string str2 = “This is text”;

if (String.Compare(str1, str2) == 0)
{
Console.WriteLine(str1 + ” and ” + str2 + ” are equal.”);
}
else
{
Console.WriteLine(str1 + ” and ” + str2 + ” are not equal.”);
}
Console.ReadKey() ;
}
}
}

When the above code is compiled and executed, it produces the following result:

This is test and This is text are not equal.

String Contains String:

using System;

namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str = “This is test”;
if (str.Contains(“test”))
{
Console.WriteLine(“The sequence ‘test’ was found.”);
}
Console.ReadKey() ;
}
}
}

When the above code is compiled and executed, it produces the following result:

The sequence ‘test’ was found.

Getting a Substring:

using System;

namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str = “Last night I dreamt of San Pedro”;
Console.WriteLine(str);
string substr = str.Substring(23);
Console.WriteLine(substr);
}
Console.ReadKey() ;
}
}

When the above code is compiled and executed, it produces the following result:

San Pedro

Joining Strings:

using System;

namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string[] starray = new string[]{“Down the way nights are dark”,
“And the sun shines daily on the mountain top”,
“I took a trip on a sailing ship”,
“And when I reached Jamaica”,
“I made a stop”};

string str = String.Join(“\n”, starray);
Console.WriteLine(str);
}
Console.ReadKey() ;
}
}

When the above code is compiled and executed, it produces the following result:

Down the way nights are dark

And the sun shines daily on the mountain top

I took a trip on a sailing ship

And when I reached Jamaica

I made a stop

Posted in a C# Fundamentals | Tagged: | Leave a Comment »