Chapter 3 String manipulation Lowercase n n string

  • Slides: 10
Download presentation
Chapter 3 String manipulation

Chapter 3 String manipulation

Lowercase? n n string. To. Lower(); Example: string name = “JONES”; string lastname =

Lowercase? n n string. To. Lower(); Example: string name = “JONES”; string lastname = name. To. Lower(); Or Console. Write. Line(“Name: {0}”, name. To. Lower()); Result: lastname=“jones”;

Uppercase n n string. To. Upper(); Example: string name = “jones”; string lastname =

Uppercase n n string. To. Upper(); Example: string name = “jones”; string lastname = name. To. Upper(); Result: lastname=“JONES”;

Length n How long is the string? int howlong; string text = “this is

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

Padding Sometimes you want to print a field so that it occupies a fixed

Padding Sometimes you want to print a field so that it occupies a fixed number of spaces (eg 20) string text = “Hello”; Console. Write. Line(“show ”+ text. Pad. Right(20, ’ ’); Or Console. Write. Line(“show ”+ text. Pad. Left(20, ’ ’);

Padding Example string number = Convert. To. String(200. 55); Console. Write. Line(“Padded unpadded”); Console.

Padding Example string number = Convert. To. String(200. 55); Console. Write. Line(“Padded unpadded”); Console. Write. Line(“{0} {1} {2}”, number. Pad. Right(15, ’ ’), number; Result: Padded 200. 55 unpadded 200. 55

double salary=400. 456; double tax = salary*. 02; double net = salary - tax;

double salary=400. 456; double tax = salary*. 02; double net = salary - tax; string strsal, strtax, strnet; strsal = String. Format("{0: N 2}", salary); strtax = String. Format("{0: N 2}", tax); strnet = String. Format("{0: N 2}", net); string hdsal="salary". Pad. Right(15, ' '); string hdtax="tax". Pad. Right(15, ' '); string hdnet="net". Pad. Right(15, ' '); Console. Write. Line("{0} {1} {2}", hdsal, hdtax, hdnet); Console. Write. Line("{0} {1} {2}", strsal. Pad. Right(15, ' '), strtax. Pad. Right(15, ' '), strnet. Pad. Right(15, ' ')); result salary 400. 46 tax 8. 01 net 392. 45

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

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

Menus… int selector=1; string text; while (selector != 0) { Console. Write(“Please enter your

Menus… int selector=1; string text; while (selector != 0) { Console. Write(“Please enter your choice: n ”); Console. Write(“ 1. Listn 2. Alphan 3. By Zip ”); selector = Convert. To. Int 32 (Console. Read. Line()); if (selector !=0) { Console. Write (“Please enter text ”); text = Console. Read. Line(); }//end if

Menu continued switch (selector) { case 0: Console. Write. Line(“The end”); break; case 1:

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