Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

OPI_LR2011_ch2m1

.pdf
Скачиваний:
7
Добавлен:
07.02.2016
Размер:
1.35 Mб
Скачать

41

< ^ ] :

string stringOne = "Line one\nline Two"; string stringTwo = @"Line One

line Two";

= Y ( ] ].

3.2.3 System.Object.ToString()

1 Y ] , \

'] ToString() string.

^ , \ ] -

( ) .

] ToString() int, \ .

int myint = 10;

string intstring = myint.ToString() ;

@ ToString() '] myint

10.

9 System.String .NET ] ( (

, ^

. h ^ ( ^

( . ' -

CLR ^]

. ' ( ] « -

» , \ ( .NET.

3.2.4 4 6_" 2 238 !7

9 string ] , Y . < ( -

( :

Empty – , \ ], ( ; Compare() – + ;

CompareOrdinal() – ^] ( -

^;

Concat() – ^] Y ; Copy() – ^] ;

Equals() – ], ;

42

Format() – + ] , ^ +-

;

Intern() – ] ^ ; Join() – ] - ( ^ -

;

2h rs – ; Length – ;

2l n () – ] ^ ; CompareTo() –^] Y;

CopyTo() – ^] Unicode

;

EndsWith() – ], ] ^ -

^ ;

Insert() – ] ( ^;

LastIndexOf() – ] (

;

PadLeft() – ^] ^, ^

Y ( );

PadRight() – ^] ^, ^

Y ( );

Remove() – ] ;

Split() – ] , -

;

StartsWith() – ], ] -

;

Substring() – ] ; ToCharArray() – ^] ; ToLower() – ( ; ToUpper() – ;

Trim() – ] (

;

TrimEnd() – ] (

;

TrimStart() – ] (

.

? . h Y

, \ ] Compare(), Concat(), Copy(),

43

Insert() Y. @ ] - ^ textBox1.

private void button1_Click(object sender, EventArgs e)

{

string str1 = "PYS_"; string str2 = "|}X~";

string str3 = @"C# S \ Y k T Z^< "+

"`SR_ S Z P TS .NET P q Z<R"; int result;

//< R ZTS Z TS

//SR ZR S^\< P Rh ^ q^ iTk Compare ZTS result = string.Compare (str1, str2);

textBox1.Text = "ZTS k\< str1: " + str1 + " T str2: " + str2 + ", Z Q^ [ P: " + result + "\r\n";

//SR ZR S^\< q^ iTk Compare Q P SR< PZP< Z < // T_Z^SP Z_T Z^ Z P

result = string.Compare(str1, str2, true);

textBox1.Text = textBox1.Text + "O ZTS k\< Y Q SZPs S^SP Z_T Z^";

textBox1.Text = textBox1.Text + "str1: " + str1 + " T str2: " + str2 + ", Z Q^ [ P: " + result + "\r\n";

// < R Y'\ P Z TS

//SR ZR S^\< q^ iTk Z TS string str4 = string.Concat(str1, str2);

textBox1.Text = textBox1.Text + "m S Zk\< str4 ` s < " + "Y'\ P str1 T str2: " + str4 + "\r\n";

//SR ZR S^\< Z SP Pt Rl ZP Z

//Y'\ P Z TS

string str5 = str1 + str2;

textBox1.Text = textBox1.Text + "str5 = str1 + str2: " + str5 + "\r\n";

// SR ZR S^\< < Copy TkSP Z ^ string str6 = string.Copy(str5) ;

textBox1.Text = textBox1.Text + "str6 Tl SP P Q str5: " + str6 + "\r\n";

//SR ZR S^\< Z SP Pt Rl ZP Z TkSP

string str7 = str6;

textBox1.Text = textBox1.Text + "str7 = str6: " + str7 + "\r\n";

//T [ P YTS ZTS

//SR ZR S^khR < Equals P<_ Y'\ P

textBox1.Text = textBox1.Text + "str7.Equals(str6): " + str7.Equals(str6) + "\r\n";

//SR ZR S^khR P Rh Rl < Equals

44

textBox1.Text = textBox1.Text + "str7 T str6 ZTS T?: " + string.Equals(str7, str6) + "\r\n";

//SR ZR S^khR ZP Z ==

textBox1.Text = textBox1.Text + "str7==str6? " + (str7 == str6) + "\r\n";

//SRQ Ph StR R Z P

textBox1.Text = textBox1.Text + "p str7 <P\ StR ^ " + str7.Length + "R<S TS" + "\r\n";

//SRQ Ph R<S ^ Z P l_ T ^

textBox1.Text = textBox1.Text + "O'R< < < ^ Z ^ str7 \ R<S " + str7[4] + "\r\n";

//ZTS T i Z P TQ SsT R< Q < Z < textBox1.Text = textBox1.Text + "str3: " + str3 + "hR

€P T h^\ [ i l Z S < \"T Z^<\"? " + str3.EndsWith("T Z^<") + "\r\n";

//ZTS T i Z P TQ SsT R< Q < Z < textBox1.Text = textBox1.Text + "str3: " + str3 + "hR

€P T h^\ [ i l Z S < \"P q Z<P\"? " + str3.EndsWith("P q Z<P") + "\r\n";

//`^ Z`_ Ss t T Z R S Z

textBox1.Text = textBox1.Text + "O Z` Ss t SP T Z^<

^Z str3 <P\ T " + str3.IndexOf("T Z^<")+ "\r\n";

//S PS \< S S S Z

string str8 = str3.Insert(str3.IndexOf("P TS"), "^t Rs"); textBox1.Text = textBox1.Text + "str8: " + str8;

}

? :

ZTS k\< str1: PYS_ T str2: |}X~, Z Q^ [ P: -1

O ZTS k\< Y Q SZPs S^SP Z_T Z^str1: PYS_ T str2: |}X~, Z Q^ [ P: 0

m S Zk\< str4 ` s < Y'\ P str1 T str2: PYS_|}X~ str5 = str1 + str2: PYS_|}X~

str6 Tl SP P Q str5: PYS_|}X~ str7 = str6: PYS_|}X~ str7.Equals(str6): True

str7 T str6 ZTS T?: True str7==str6? True

p str7 <P\ StR ^ 8R<S TS

O'R< < < ^ Z ^ str7 \ R<S |

str3: C# S \ Y k T Z^< `SR_ S Z P TS

.NET P q Z<R •R QP T h^\ [ i l Z S < "T Z^<"? False str3: C# S \ Y k T Z^< `SR_ S Z P TS

.NET P q Z<R •R pP T h^\ [ i l Z S < "P q Z<P"? False O Z` Ss t SP T Z^< ^ Z str3 <P\ T 15 str8: C# S \ Y k T Z^< `SR_ S Z ^t Rs-

P TS .NET P q Z<R

45

( Y . h

Y str3 -

. 2 , \ (

Y ^. ' ] -

(+). =

] .

string str3 = @"m# Z PS \ Y k T Z^< " + "`SR _ S Z P TS .NET P q Z<R"; result = string.Compare(str1, str2);

] +

. 8 ( ]

( :

Y , \ Y Y ;

0, \ ;

Y , \ Y Y .

Y :

ZTS k\< strl: PYS_ T str2: |}X~, Z Q^ [ P : -1

S ( ^ Y , ( ,

.

+ Compare ]

. '

+ – true.

result = string.Compare(str1, str2, true);

textBox1.Text = textBox1.Text + "O ZTS k\< Y Q SZPs S^SP

Z _T Z^";

textBox1.Text = textBox1.Text + "str1: " + str1 + " T str2: " + str2 + ", Z Q^ [ P : " + result + "\r\n";

@ :

O ZTS k\< Y Q SZPs S^SP Z _T Z^ str1: PYS_, str2:|}X~, Z - Q^ [ P : 0

8 -

, ^] -

. ( ] :

-> 6S@o, 6S@o = 6S@o ? =69 0

h '] ( -string. / - + Concat().

String Str4 = string.Concat (str1, str2);

h – (+). string str5 = str1 +

str2;

/ (+) string ( , \ -

] ^, + Concat(). /

46

strl+str2, \ ], ^ -

^ +.

6 ( ( + ]^ Copy()

(=). @ ^ ^^

Y. ? ] Y :

string str6 = string.Copy (str5); string Str7 = Str6;

1str6, str7 ,

str5.

9 string 2# ]

. ' Y - Equals().

str7.Equals(str6)

'] str7 ]

'] str6. h ] -

+ string.Equals().

string.Equals (str7, str6)

= (

(==).

str7 == str6;

9 ( ] True, \ , False, \ .

@ Length ] . 6

([]) ] , \ ] . <:

textBox1.Text = "O'R< < < ^ Z ^ str7 \ R<S + str7[4];

( ] ' str7,

^ 4 ([]),

2#, C++, ^ ( . 3.1).

= 3.1 – 1

?

 

 

 

 

6

S

@

o

1

0

1

2

3

4

5

6

7

'

1

2

3

4

5

6

7

8

] , \ ' str7 ]

«6» , \ ], 4.

47

3.2.5 A Y 8 43 87

= string ] ( Substring()

. / ] -

, ^ . h ]

, , \ ,

Y.

Substring ( . ' ,

:

//_`^\< Z YZ Y R string s1 = "r R oSP ZR • RZR";

//Zt^\< T P [_ Z YT ^ int ix = s1.LastIndexOf(" ");

//Zt^\< P \ S S Z ^ string s2 = s1.Substring (ix+1);

//S P S k\< s1 P T Z ^, j hR P\ [

// 0- _ T ^, j l QP T h^\ [ P T< Z YT < s1 = s1.Substring(0, ix) ;

//Q S^ `^ P\< T P [_ Z YT ^ ix = s1.LastIndexOf(" ");

//S P S k\< s3 P P \ S Z ^ string s3 =s1.Substring(ix+1);

//R P\< si P T Z ^

//ST ^ [ S_ R<S ^ ix

s1 = s1.Substring(0,ix);

//R P\< ix P Z YT

//<Tt "R" T "SP"

ix = s1.LastIndexOf(" ");

//S P S k\< s4 P T Z ^ T string s4 = s1.Substring (ix+1) ; //S P S k\< s1 P T Z ^ ST 0 ix //Zt^\< T [ R S "R"

s1 = s1.Substring(0,ix);

//P<P_P\< ZtP R T P [_ Z YT ^

//P i[_ ZPQ^ q^ iT LastIndexof S Z -1 ix = s1.LastIndexOf(" ");

//S P S k\< s5 P T Z ^ hR PkhR Q

//T [ R ix = 1, s5 S P S k\ [ P hP s1 string s5 =s1.Substring(ix+1);

//XRS R< Z Q^ [ P

textBox3.Text = "s2: " + s2 + "\r\n" + "s3: " + s3 + "\r\n";

48

textBox3.Text = textBox3.Text + "s4: " + s4 + "\r\n" + "s5: " + s5 + "\r\n";

textBox3.Text = textBox3.Text + "s1: " + s1 + "\r\n";

2 Y ] ]

.

string s1 = "r R oSP ZR • RZR";

' ^] ^ . { -

, \ .

int ix = s1.LastIndexOf(" ") ;

ix ^ 12. 2 «0-

» ] 13. =, ] -

, ( .

string s2 = s1.Substring(ix+1);

s2 «0». h ]

s1 «0». h + ^ Substring – . '

, – -

.

s1 = s1.Substring(0, ix);

< «/ h =». = -

^] ( , \ Y . / ( ] , ] ] , - ] . ? , Y

«/». 9 ] (

, + -1.

ix = s1.LastIndexOf(" ");

=, + (-1 + 1 = 0),

, «/».

string s5 = s1.Substring(ix+1) ;

? :

s2: • RZR s3: ZR s4: oSP s5: r R s1: r R

3.2.6 9 # 7 2 2384"

' , ( -

) , . S Y + ' , ^

49

, ] , \ Split()

string. Split() ] . h -

Split()

. ' -

. ? Split() :

// Z P P TQ^

string s1 = "r R ,oSP, ZR, p Z QY Z^";

//QP P\< Z Q T ST R<S R P P TQ^ const char cspace = ' ';

const char ccomma = ',';

//S Zk\< <P RS Z Q T SRs R<S TS

//T T TiTP TQ^\< l _ S <P < P<R char[] delimiters = {cspace, ccomma};

string output = ""; int ctr = 1;

//SR T \< T Z R P ST Z Q T [ R TS

//T QY ZT_P\< Z Q^ [ P

foreach (string substring in s1.Split(delimiters))

{

output += ctr++; // < Z T Z R output += ": ";

output += substring; // T Z P

output += "\r\n"; // Z sT P S^ T Tk

}

// SRST Z Q^ [ P ^ textBox4.Text = output;

? :

1:r R

2:oSP

3:ZR

5:p

7:Z QY Z^

, \ 4 ] ( – -

.

string s1 = "r R ,oSP, ZR, p Z QY Z^";

s1, \ Y . -

] . / , Y – (,). ( «=» «?» Y ^ -

, .

h Y ^ , \ ^

.

const char cspace = ' '; const char ccomma = ',';

50

{ ] ^ .

char[] delimiters = {cspace, ccomma};

Split() ( -

, . Y -

]

. / (, Split() -

, (

-

.

foreach (string substring in s1.Split(delimiters))

1 foreach

^ , + Split(). ? ^ ,

( . ' ( '

. • ( Y, ( -

«=» «?» Y ^

— , . 9 Split() -

( «=» «?», . / ] \ . / (, ( Y ] ( ^ 0

-( , Split \ ] .

3.2.7 6 StringBuilder

9 StringBuilder ] -

, ,

. < Y (

StringBuilder ]:

Capacity – ] , -

StringBuilder;

Chars – 1 ;

Length – ] ( '] StringBuilder;

t – ] , -

StringBuilder;

Append() – ] '] StringBuilder; AppendFormat() – \ ] ^] +

StringBuilder;

Ensurecapacity() – ], \ StringBuilder ] ]

Y ;

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]