Working With Strings: Windows Phone 7.5 Mango Apps Development – Part 9

Welcome to the Windows Phone 7.5 Mango App development series. This is a part of the Learn to Develop Windows Phone 7.5 Mango Applications. In our last tutorial we have seen how to use simple helper methods.  We have learned a lot and we will learn even more. Today we will learn about Working With Strings.

Many types of applications involve managing text. Managing text could include storing fields of data in a Customer Relationship Management application or managing large amounts of texts and paragraphs as in a Content Management System. Mastering string Manipulation is a vital skill. In this lesson we shall see several string manipulation methods that have been made easy by the .NET Framework Class library.

Alright let’s get started.

Create a new project with a unique name for this lesson, something like ‘StringManipulationTrial’. In the design mode, drag and drop a button in the top left corner of the emulator (see image).  Name the button properly and set its Content value to ‘Click Me’. Follow the same process for a text block and set its text property to blank. Set the text wrap property of the text block to wrap. Now double-click the Click Me button to open up the C# code for the button’s click event. Copy and Paste the following code in your button’s click event.

// STRINGS PART 1 – ESCAPE SEQUENCES
//string myString = "Go to your c:\\ drive";
// string myString = "My \"so called\" life";
//string myString = "What if I need \n a new line?";
// myTextBlock.Text = myString;

//STRINGS PART 2 – STRING.FORMAT
//string myString = string.Format("Make: {0} (Model: {1}", "BMW", "745li");
//string myString = String.Format("{0:C}", 123.45);
//string myString = String.Format("{0:N}", 1234567890);
//string myString = String.Format("{0:D}", 1234567890);
//string myString = String.Format("{0:P}", .127);
//string myString = String.Format("Phone number: {0:(###) ###-####}", 1234567890);
// string myString = "";

//STRINGS PART 3 - STRING BUILDER & ITS METHODS
//StringBuilder myString = new StringBuilder();
//for (int i = 0; i < 100; i++)
//{
//    //myString += "--" + i.ToString();
//    //myString = myString + "--" + i.ToString();
//    myString.Append("--");
//    myString.Append(i);
//}
myTextBlock.Text = myString;

Ok so now let’s have an in-depth look into the 3 parts of the program.

In the first part (STRINGS PART 1 –ESCAPE SEQUENCES) we shall introduce ourselves to the various escape sequences. Escape sequences basically are certain characters in combination with a backslash that perform a specific action like \t for tab movements, \n for carriage returns, \’ for single quotation marks etc. Certain escape sequences in C# help us print certain characters like double quotes or backslash. Such characters are preceded by a backslash as seen in the first two lines of the code, viz.

//string myString = "Go to your c:\\ drive";
// string myString = "My \"so called\" life";

 There are roughly 14 pre-defines escape sequences available to a C# programmer. You can learn more about escape sequences at MSDN.

In the second part we make use of an inbuilt method Format of the String Class to change the appearance of the text being displayed. The String.Format() method replaces each format item in a specified string with the text equivalent of a corresponding object’s value.  So in the following statement

//string myString = string.Format("Make: {0} (Model: {1}", "BMW", "745li");

 The text BMW is placed in the position of {0} and 745li in the position of {1}. The numerals in the curly braces indicate the index number of the argument passed. The String.Format() Method is especially useful in displaying numbers in a specific format as seen in the following statements

//string myString = String.Format("{0:C}", 123.45);
//string myString = String.Format("{0:N}", 1234567890);
//string myString = String.Format("{0:D}", 1234567890);
//string myString = String.Format("{0:P}", .127);
//string myString = String.Format("Phone number: {0:(###) ###-####}", 1234567890);

While formatting the index number precedes the type of formatting. For example in the case of {0:C}, 0 indicates the index number and C specifies Currency type formatting. The resultant text displayed would be $ 123.45. The String.Format() is an overloaded method, i.e., it can accept different types of artuguments or different number of arguments. For more information on String.Format() you could visit MSDN.

In the final part of our program we make us of the StringBuilder. A string builder represents a string like object whose value is a mutable sequence of characters. The value is mutable because it can be modified once it has been created by appending, removing, replacing or inserting characters. The StringBuilder class has a number of methods to help you modify your string. Visit MSDN to know more about the StringBuilder.

String can be manipulated and formatted in a lot of ways under C#. Keep on learning and experimenting with different formats and methods available under the String class and StringBuilder class.

Now lets learn about working with DateTime.

Posted by on , in Category Phone with Tags

Gadget freak, Apple lover, Windows Phone lover; well that’s me! I have been in this wonderful field of freelance writing for quite some time and looking forward to developing more illuminating content related to gadgets and technology, as I go along.