Escape Characters
Escape characters such as “\n” (new line) and “\t” (tab) can be included in strings. The line:
string hello = “Hello\nWorld!”
is the same as:
Hello
World!
If you want to include a backward slash, it must be preceded with another backward slash. The following string:
string filePath = \\\\My Documents\\
is actually the same as:
\\My Documents\
The @ Symbol
The @ symbol tells the string constructor to ignore escape characters and line breaks. The following two strings are therefore identical:
string p1 = “\\\\My Documents\\My Files\\”;
string p2 = @“\\My Documents\My Files\”;
ToString()
Like all objects derived from Object, strings provide the ToString method, which converts a value to a string. This method can be used to convert numeric values into strings, like this:
int year = 1999;
string msg = “Eve was born in “ + year.ToString();
System.Console.WriteLine(msg); // outputs “Eve was born in 1999″
Accessing Individual Characters
Individual characters contained in a string can be accessed using methods such as SubString(), Replace(), Split() and Trim().
string s3 = “Visual C# Express”;
System.Console.WriteLine(s3.Substring(7, 2)); // outputs “C#”
System.Console.WriteLine(s3.Replace(“C#”, “Basic”)); // outputs “Visual Basic Express”
It is also possible to copy the characters into a character array, like this:
string s4 = “Hello, World”;
char[] arr = s4.ToCharArray(0, s4.Length);
foreach (char c in arr)
{ System.Console.Write(c); // outputs “Hello, World”
}
Individual characters from a string can be accessed with an index, like this:
string s5 = “Printing backwards”;
for (int i = 0; i < s5.Length; i++)
{
System.Console.Write(s5[s5.Length - i - 1]); // outputs “sdrawkcab gnitnirP”
}
Changing Case
To change the letters in a string to upper or lower case, use ToUpper() or ToLower(), like this:
string s6 = “Battle of Hastings, 1066″;
System.Console.WriteLine(s6.ToUpper()); // outputs “BATTLE OF HASTINGS 1066″
System.Console.WriteLine(s6.ToLower()); // outputs “battle of hastings 1066″
Comparisons
The simplest way to compare two strings is to use the == and != operators, which perform a case-sensitive comparison.
string color1 = “red”;
string color2 = “green”;
string color3 = “red”;
if (color1 == color3)
{ System.Console.WriteLine(“Equal”);
}
if (color1 != color2)
{
System.Console.WriteLine(“Not equal”);
}
String objects also have a CompareTo() method that returns an integer value based on whether one string is less-than (<)or greater-than (>) another. When comparing strings, the Unicode value is used, and lower case has a smaller value than upper case.
string s7 = “ABC”;
string s8 = “abc”;
if (s7.CompareTo(s8) > 0)
{
System.Console.WriteLine(“Greater-than”);
}
else
{
System.Console.WriteLine(“Less-than”);
}
To search for a string inside another string, use IndexOf(). IndexOf() returns -1 if the search string is not found; otherwise, it returns the zero-based index of the first location at which it occurs.
string s9 = “Battle of Hastings, 1066″;
System.Console.WriteLine(s9.IndexOf(“Hastings”)); // outputs 10
System.Console.WriteLine(s9.IndexOf(“1967″)); // outputs -1
Splitting a String into Substrings
Splitting a string into substrings—such as splitting a sentence into individual words—is a common programming task. The Split() method takes a char array of delimiters, for example, a space character, and returns an array of substrings. You can access this array with foreach, like this:
char[] delimit = new char[] { ‘ ‘ };
string s10 = “The cat sat on the mat.”;
foreach (string substr in s10.Split(delimit))
{
System.Console.WriteLine(substr);
}
Null Strings and Empty Strings
An empty string is an instance of a System.String object that contains zero characters. Empty strings are used quite commonly 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 like this:
string s = “”;
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 results in 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:
string str = “hello”;
string nullStr = null;
string emptyStr = “”;
string tempStr = str + nullStr; // tempStr = “hello”
bool b = (emptyStr == nullStr);// b = false;
emptyStr + nullStr = “”; // creates a new empty string
int I = nullStr.Length; // throws NullReferenceException
For more info visit http://msdn.microsoft.com/en-us/library/362314fe(VS.80).aspx .
| 2.5 |

H_acktivis_T




No Comment Received
Leave A Reply