c# strings

Post on 11-Jul-2015

643 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 9

C# .NET strings

What will we be learning?

string variable and its methods

trim

contain

remove and replace

substring

string variable and its methods

string stringVar = "Dear Mr Tan, how are you?";

string anotherStringVar;

anotherStringVar = stringVar.

IntelliSense

- shows you a list of Methods

and Properties that are

available for the variable

Full list of methods and properties >

Mouse Over

Part 1 variable and methods

New solution and project:

SpfChapter9_strings

Save all and rename the project to "Part 1

variable and methods"

Add a button and a textbox

Add codes into button click method:

Length property of string variable

This property return the number of characters in a string

It also counts the blank spaces

Eg: stringVar.Length;

Note that you don't need any round brackets, because it's a property not a method

trim method

To trim unwanted characters from the

string – used often to trim blank spaces

at the start and the end

- Trim(): both at the start and the end

- TrimEnd(): at the end

- TrimStart(): at the start

Continue from previous projectAdd another button and textbox

Add codes into the button click method

3 blank

spaces at the

start and the

end

Continue …

Trim() TrimEnd()

null => blank space

Trim() same as Trim(null)

TrimStart() same as TrimStart(null)

TrimEnd() same as TrimEnd(null)

Trim other character

Trim more than one characters

Contains method

Replace method

Spell checker:

Replace "mistak" with "mistake"

Replace "teh" with "the"

:

:

Continue

Add codes into button click method:

Substring method

int startIndex

=> starting

position to

extract the

substring

(start from 0)

int length =>

length of the

substring

Continue

Add a button and textbox

Add codes for button click method:

Summary

string variable and its methods

trim

contain

remove and replace

substring

top related