Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 String manipulation.

Similar presentations


Presentation on theme: "Chapter 3 String manipulation."— Presentation transcript:

1 Chapter 3 String manipulation

2 Lowercase? string.ToLower(); Example: string name = “JONES”;
string lastname = name.ToLower(); Or Console.WriteLine(“Name: {0}”, name.ToLower()); Result: lastname=“jones”;

3 Uppercase string.ToUpper(); Example: string name = “jones”;
string lastname = name.ToUpper(); Result: lastname=“JONES”;

4 Length How long is the string? int howlong;
string text = “this is a sentence”; howlong = text.Length();

5 Padding Sometimes you want to print a field so that it occupies a fixed number of spaces (eg 20) string text = “Hello”; Console.WriteLine(“show ”+ text.PadRight(20,’ ’); Or Console.WriteLine(“show ”+ text.PadLeft(20,’ ’);

6 Padding Example string number = Convert.ToString(200.55);
Console.WriteLine(“Padded unpadded”); Console.WriteLine(“{0} {1} {2}”, number.PadRight(15,’ ’), number, number; Result: Padded unpadded

7 double salary=400.456;double tax = salary*.02;
double net = salary - tax; string strsal, strtax, strnet; strsal = String.Format("{0:N2}",salary); strtax = String.Format("{0:N2}",tax); strnet = String.Format("{0:N2}",net); string hdsal="salary".PadRight(15,' '); string hdtax="tax".PadRight(15,' '); string hdnet="net".PadRight(15,' '); Console.WriteLine("{0} {1} {2}",hdsal,hdtax, hdnet); Console.WriteLine("{0} {1} {2}",strsal.PadRight(15,' '), strtax.PadRight(15,' '),strnet.PadRight(15,' ')); result salary tax net

8 Menus Please enter your choice: 1 List 2 Alpha 3 By zipcode

9 Menus… string text; while (selector != 0) {
int selector=1; string text; while (selector != 0) { Console.Write(“Please enter your choice:\n ”); Console.Write(“1. List\n2. Alpha\n3. By Zip ”); selector = Convert.ToInt32 (Console.ReadLine()); if (selector !=0) Console.Write (“Please enter text ”); text = Console.ReadLine();}//end if

10 Menu continued switch (selector) { case 0:
Console.WriteLine(“The end”); break; case 1: Console.WriteLine(“Code for List ”); case 2: default: break;}// end switch


Download ppt "Chapter 3 String manipulation."

Similar presentations


Ads by Google