c# strings

31
Chapter 9 C# .NET strings

Upload: hock-leng-puah

Post on 11-Jul-2015

643 views

Category:

Education


0 download

TRANSCRIPT

Page 1: C# Strings

Chapter 9

C# .NET strings

Page 2: C# Strings

What will we be learning?

string variable and its methods

trim

contain

remove and replace

substring

Page 3: C# Strings

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

Page 4: C# Strings

Full list of methods and properties >

Mouse Over

Page 5: C# Strings

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

Page 6: C# Strings

Add codes into button click method:

Page 7: C# Strings
Page 8: C# Strings

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

Page 9: C# Strings

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

Page 10: C# Strings

Continue from previous projectAdd another button and textbox

Add codes into the button click method

Page 11: C# Strings

3 blank

spaces at the

start and the

end

Page 12: C# Strings
Page 13: C# Strings

Continue …

Page 14: C# Strings

Trim() TrimEnd()

Page 15: C# Strings

null => blank space

Trim() same as Trim(null)

TrimStart() same as TrimStart(null)

TrimEnd() same as TrimEnd(null)

Page 16: C# Strings

Trim other character

Page 17: C# Strings
Page 18: C# Strings

Trim more than one characters

Page 19: C# Strings
Page 20: C# Strings

Contains method

Page 21: C# Strings
Page 22: C# Strings
Page 23: C# Strings

Replace method

Spell checker:

Replace "mistak" with "mistake"

Replace "teh" with "the"

:

:

Page 24: C# Strings

Continue

Page 25: C# Strings

Add codes into button click method:

Page 26: C# Strings
Page 27: C# Strings

Substring method

int startIndex

=> starting

position to

extract the

substring

(start from 0)

int length =>

length of the

substring

Page 28: C# Strings

Continue

Add a button and textbox

Page 29: C# Strings

Add codes for button click method:

Page 30: C# Strings
Page 31: C# Strings

Summary

string variable and its methods

trim

contain

remove and replace

substring