+ All Categories
Home > Documents > Chapter5: Usage of Build-In Functions or Methods

Chapter5: Usage of Build-In Functions or Methods

Date post: 05-Dec-2014
Category:
Upload: ngeam-soly
View: 555 times
Download: 0 times
Share this document with a friend
Description:
មេរៀន Advance C# ជំពូកទី ៥ សំរាប់ក្រុម E1 & E2
20
Advanced Usa Worki The buil standard scripts. C System.C Expres To make in Listing Listing 16 char my The stat to a cha as illustr Some sp expresse quote ch literals. L sequenc d Programm age o ing with t-in char ty d for encod C# stores ea Char struct. ssing Cha e using cha g 16-1. 6-1. A Chara yChar = ' ement in Li r variable c rated by Fig pecial chara ed using an haracter can Listing 16-2 ce and a Un ming Method f Buil h Charac ype represe ding text. It ach charact . aracters r easier, C# cter Literal a'; isting 16-1 alled myCh gure 16-1. acters canno n escape seq nnot be exp 2 demonstr nicode enco dology in C# ld-In F cters nts a single supports n ter using tw Using Li # supports c uses a char har. Charact ot be expre quence or a pressed dire ates how to oding. # Funct e Unicode c early 200,00 wo bytes. Th terals character lit racter litera ter literals a essed direct a Unicode e ectly since o express th tions character. U 00 different he char key terals, an ex l to assign are expresse tly, and so c encoding. F it is used to his characte N or Me nicode is a t characters yword is an xample of w the lowerca ed between character lit For example o define oth er using an Norton Univ Pa ethod widely use s in 91 diffe alias to the which is sho ase a chara n single quo terals can b e, the single her characte escape ersity age 1 ds ed erent e own cter otes, be e er
Transcript
Page 1: Chapter5: Usage of Build-In Functions or Methods

Advanced 

UsaWorki

The builstandardscripts. CSystem.C

Expres

To makein Listing

Listing 16

char my

The statto a chaas illustr

Some spexpressequote chliterals. Lsequenc

d Programm

age oing with

t-in char tyd for encodC# stores eaChar struct.

ssing Cha

e using chag 16-1.

6-1. A Chara

yChar = '

ement in Lir variable crated by Fig

pecial charaed using anharacter canListing 16-2ce and a Un

ming Method

f Builh Charac

ype represeding text. It ach charact.

aracters

r easier, C#

cter Literal

a'; 

isting 16-1 alled myCh

gure 16-1.

acters cannon escape seqnnot be exp2 demonstrnicode enco

dology in C#

ld-In Fcters

nts a singlesupports nter using tw

Using Li

# supports c

uses a charhar. Charact

ot be exprequence or apressed direates how to

oding.

#

Funct

e Unicode cearly 200,00

wo bytes. Th

terals

character lit

racter literater literals a

essed directa Unicode eectly since o express th

tions

character. U00 differenthe char key

terals, an ex

l to assign are expresse

tly, and so cencoding. Fit is used tohis characte

N

or Me

nicode is at characters

yword is an

xample of w

the lowercaed between

character litFor exampleo define other using an

Norton Univ

Pa

ethod

widely uses in 91 diffealias to the

which is sho

ase a charan single quo

terals can be, the singleher characteescape

ersity

age 1

ds

ed erent e

own

cter otes,

be e er

Page 2: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Listing 16

char my

char my

char my

Charactesimple ewith a u)frequent

Using

The charmethods

d Programm

-2. Expressing

yEscapedQ

yUnicodeQ

yHexQuote 

er literal escescape sequ) or in hexatly used cha

Struct M

r keyword is to aid in w

ming Method

g a Character

uote = '\

uote = '\

= '\x002

cape sequeuence or a Uadecimal (paracter liter

Members

s an alias toworking wit

dology in C#

Through an E

\''; 

\u0027'; 

27'; 

ences start wUnicode enrefixed withral escape s

o the Systeth character

#

Escape Seque

with a backncoding, exph an x). Tabsequences.

m.Char strurs. The mos

ence

kslash charapressed eith

ble 16-2 sho

uct, which cst useful are

N

acter (\) andher in decimows some o

contains a ne described

Norton Univ

Pa

d can be a mal (prefixeof the most

number of sd in Table 16

ersity

age 2

ed

static 6-3.

Page 3: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Using

A string keywordyou can freely.

Expres

As with tdemons

Listing 16

string 

String litquotes (

d Programm

Strings

object reprd is an alias

choose to

ssing Str

the other Ctrated by L

-5. Using a St

myString 

terals are d("), as illustr

ming Method

resents a refor the Sysuse either t

rings Usin

C# built-in tisting 16-5.

tring Literal

= "Intro

efined by prated by Fig

dology in C#

ead-only sestem.String the keywor

ng String

ypes, you c.

oduction t

placing a segure 16-2.

#

quence of Uclass, and ld or the cla

g Literals

can define s

to C#"; 

quence of c

Unicode chlike all the oass name or

s

string value

characters

N

haracters. Thother built-r switch bet

es using lite

between a

Norton Univ

Pa

he string -in type aliatween them

erals, as

pair of dou

ersity

age 3

ases, m

uble

Page 4: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The statof “Intro

Perfor

The folloobjects.

Compa

C# lets yis no nee

These opstring in

Combin

Strings c

d Programm

ement in Lioduction of

rming Op

owing sectio

ring Strin

you compared to use a

perators arentroduction

ning Strin

can be com

ming Method

isting 16-5 C#” and as

perations

ons demon

ngs

re strings d compariso

e case sensn.

gs

mbined using

dology in C#

uses a strinssigns it to a

s on Stri

nstrate how

irectly usinon method,

sitive so tha

g the addit

#

ng literal to a string var

ngs

to perform

g the compsuch as the

at the string

ion operato

create a striable called

m common

parison opee Java equa

g Introduct

or (+)

N

ring object d myString.

operations

erators (== als method.

tion is not t

Norton Univ

Pa

with the va

on string

and !=). Th.

the same as

ersity

age 4

alue

here

s the

Page 5: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Readin

The strinYou canbelow denumera

Using

You can System.topic, bufind detahttp://mexample

d Programm

g Individu

ng class imp’t use the inemonstrateate the con

Regular

use regulaText.Regul

ut I have incails of C# re

msdn.microse of using re

ming Method

ual Charac

plements anndexer to mes using thetents of a s

Expressi

r expressiolarExpressicluded a simegular exprsoft.com/enegular expr

dology in C#

cters

n indexer thmodify the ce indexer, astring.

ions

ns to searcions.Regexmple exampressions at tn-us/library/ressions to

#

hat allows ycharacters slong with t

h and manx class. Regple so that the followin/hs600312.search strin

you to accesince stringhe Length

ipulate strinular expresyou know t

ng URL: aspx. Code

ngs.

N

ess individus are read-property, in

ngs using tssions are athe feature

e below con

Norton Univ

Pa

al characteonly. Code

n a for loop

he n advancedexists. You

ntains an

ersity

age 5

rs.

p to

d can

Page 6: Chapter5: Usage of Build-In Functions or Methods

Advanced 

This exairrespect

Manip

Table 16mentionvalue of

d Programm

mple searctive of case

pulating

6-4 describened, string v

a string are

ming Method

hes a set ofe (that is, pr

Strings

es the methvalues are re in fact cre

dology in C#

f book titlero, Pro, PRo

hod availabread-only, aeating new

#

s for the teo and prO w

le for maniand so thosstring value

rm pro, loowould all be

pulating ste methods es.

N

oking for me considere

rings. As prthat seem

Norton Univ

Pa

atches ed matches

reviously to edit the

ersity

age 6

).

Page 7: Chapter5: Usage of Build-In Functions or Methods

Advanced 

d Programmming Methoddology in C## NNorton Univ

Pa

ersity

age 7

Page 8: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Code be

d Programm

elow contai

ming Method

ns a demon

dology in C#

nstration of

#

f some of th

hese metho

N

ods.

Norton Univ

Pa

ersity

age 8

Page 9: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Search

The strin

Code be

d Programm

e above pro

hing Strin

ng class def

elow contai

ming Method

oduces the

ngs

fines severa

ns a demon

dology in C#

following r

al methods

nstration of

#

results:

to search s

f some of th

strings.

hese metho

N

ods.

Norton Univ

Pa

ersity

age 9

Page 10: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Other

Two othmanipul

Code be

d Programm

e above pro

String M

her memberating or sea

elow contai

ming Method

oduces the

Methods

rs of the strarching stri

ns a demon

dology in C#

following r

ring class arings catego

nstration of

#

results:

re worth meories.

f some of th

entioning b

hese metho

N

but do not f

ods.

Norton Univ

Pag

fit into the

ersity

ge 10

Page 11: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Using

The Strincompanrepresenefficientbuild str

Creati

There is instead this:

StringB

d Programm

e above pro

the Stri

ngBuilder cion to the r

nted by a Stthan worki

rings throug

ng a Stri

no equivaluse one of

Builder my

ming Method

oduces the

ingBuild

lass, contairead-only stringBuildeing with strgh multiple

ingBuilde

ent to the sthe class co

yBuilder 

dology in C#

following r

der Class

ned in the tring class. r object andrings directle append op

er Objec

string literaonstructors

= new Str

#

results:

s

System.TexYou can edd perform oly. The mainperations.

t

l to define , the simple

ringBuilde

xt namespadit the contoperations n use of the

StringBuildest of which

er(); 

N

ce, is the mtents of a stthat are fas

e StringBuil

der values. Yh has no pa

Norton Univ

Pag

mutable tring ster and molder class is

You must arameters, l

ersity

ge 11

ore s to

ike

Page 12: Chapter5: Usage of Build-In Functions or Methods

Advanced Programming Methodology in C# Norton University  

Page 12

A StringBuilder object uses a char array to store the characters of the string it represents. This array will be resized as needed to accommodate the effect of building a string by adding characters.

You can recommend an initial size of array for a StringBuilder object to allocate by providing an int constructor parameter, like this:

StringBuilder myBuilder = new StringBuilder(10); 

Suggesting an array capacity that is large enough to hold the string you intend to build can avoid the time-consuming operation of creating a larger array and copying the contents of the StringBuilder to it.

If you already have the foundation for the string that you want to build, you can provide this as the basis of the StringBuilder, like this:

StringBuilder myBuilder = new StringBuilder("Introduction to"); 

Using a StringBuilder Object

The StringBuilder class includes a range of members that help you manipulate the contents of strings.

Page 13: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Code be

d Programm

elow contai

ming Method

ns a demon

dology in C#

nstration of

#

f some of thhese metho

N

ods.

Norton Univ

Pag

ersity

ge 13

Page 14: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Using t

The Appto quickfragmen

d Programm

e above pro

the Appen

pend and Inkly build strints of string

ming Method

oduces the

nd and Ins

nsert methoings by con

gs into large

dology in C#

following r

sert Metho

ods are the ncatenatinger ones.

#

results:

ods

heart of the smaller str

e StringBuirings togeth

N

lder class. Ther and inse

Norton Univ

Pag

They allow yerting

ersity

ge 14

you

Page 15: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Using t

The last characteindexer

The cod

Forma

C# has ccomposformat s

Using

d Programm

e above pro

the String

point of noers using a cimplement

e above pro

atting St

comprehenite formattistrings in a

Compos

ming Method

oduces the

Builder In

ote for the Scustom inded in the st

oduces the

trings

sive supporing. The folrange of di

site Form

dology in C#

following r

ndexer

StringBuildeexer. (By cotring class.)

following r

rt for formalowing sectfferent way

matting

#

results:

er class is tontrast, you

results:

atting stringtions demoys.

hat you canu can only r

gs through onstrate how

N

n read and read charac

a feature cw to use th

Norton Univ

Pag

modify cters using t

alled is feature to

ersity

ge 15

the

o

Page 16: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The C# cwhich is

Code abitems is

A formaonward.local var

The stataccept aformat it

Format iis an exa

The outp

London 

Escapin

If you wformattibe interpwill be in

d Programm

composite fused to re

bove is basiillustrated

t item is wr There are triable, num

ic Console.Wa number oftem zero, th

items don’tample:

put from th

is the ho

ng Braces

ant to dispng, then yopreted as anterpreted

ming Method

formatting place the co

c compositbelow:

rapped in btwo formatbered 0 and

WriteLine mf parametehe next for

t have to ap

hese statem

ome city 

lay brace chou must use single opeas a single

dology in C#

feature wilorrespondi

e formattin

brace charact items contd 1.

method suprs. The firstformat item

ppear in seq

ments is as fo

of Adam. 

haracters ine an escapeen brace cha

close brace

#

l create a stng format i

ng example.

cters ({ and tained in th

pports the c parameterm one, and

quence and

ollows:

Adam is 

n a string the sequence.aracter ({), ae character

tring represitem.

. The string

}) and are he string ass

composite fr to follow tso on.

d can be use

38 years 

hat you will . Two open and two clo(}).

N

sentation fo

g that conta

numbered signed to th

formatting the string w

ed more th

old. 

use for combrace char

ose brace ch

Norton Univ

Pag

or each obj

ains the form

from zero he formatSt

feature andwill be used

an once. He

mposite racters ({{) wharacters (}

ersity

ge 16

ect,

mat

tring

d will for

ere

will })

Page 17: Chapter5: Usage of Build-In Functions or Methods

Advanced 

The cod

Forma

When yoinclude represenSystem.Ocomposrepresen

Using a

A formavalue sh

The statbold.

d Programm

e above pro

atting Ty

ou use comin the compntation of thObject and ite formattinted as a st

a Format C

t componeould be dis

ement that

ming Method

oduces the

ypes

mposite formposited strihe object oso is inheri

ing, there aring.

Componen

nt is an optsplayed.

t contains th

dology in C#

following r

matting, young. The com

or value by cited by all r

are some op

nt

tional part o

he format it

#

results:

u can specimposite forcalling the Treference anptions to co

of a format

tem with th

fy any objermatting syToString mnd value tyontrol how

t item that s

he format co

N

ect or value ystem create

method, whicpes. When an object o

specifies ho

omponent

Norton Univ

Pag

you want tes a string ch is defineyou use

or value is

ow an objec

is shown in

ersity

ge 17

to

ed in

ct or

n

Page 18: Chapter5: Usage of Build-In Functions or Methods

Advanced 

A formacharactespecifiesthe fixedplaces. T

Format ctypes. Th

Code be

d Programm

t componeer specifies s the precisd-point formThe code ab

componenthe most use

elow demon

ming Method

nt consists the kind ofion. In the cmatting shobove produ

ts are availaeful numeri

nstrates how

dology in C#

of a single f formattingcase of Listould be useuces the foll

able for theic format co

w to use th

#

character ag that shouing 16-24, t

ed and that lowing resu

e built-in nuomponents

e format co

and an optild be appliethe format the previsi

ults:

umeric types are describ

omponents

N

ional numeed, and thecomponenon should b

es and date bed in Tabl

s in Table 16

Norton Univ

Pag

eric value. Te numeric vant specifies be two dec

and time e 16-8.

6-8.

ersity

ge 18

he alue that imal

Page 19: Chapter5: Usage of Build-In Functions or Methods

Advanced 

Code ab

d Programm

bove produc

ming Method

ces the follo

dology in C#

owing resu

#

lts:

NNorton Univ

Pag

ersity

ge 19

Page 20: Chapter5: Usage of Build-In Functions or Methods

Advanced Programming Methodology in C# Norton University  

Page 20

References:

Adam Freeman. (2010). Introduction Visual C# 2010.


Recommended