Learning C# 3.0 by MacDonald Brian Liberty Jesse & Brian MacDonald

Learning C# 3.0 by MacDonald Brian Liberty Jesse & Brian MacDonald

Author:MacDonald, Brian, Liberty, Jesse & Brian MacDonald [Jesse Liberty]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C#
ISBN: 9780596554200
Publisher: O'Reilly Media
Published: 2008-11-17T16:00:00+00:00


The output looks like this:

s5 copied from s2: ABCD String s3 is 94 characters long. The 5th character is r s3:Liberty Associates, Inc. provides custom .NET development, on-site Training and Consulting Ends with Training?: False Ends with Consulting?: True The first occurrence of Training in s3 is 71 s10: Liberty Associates, Inc. provides custom .NET development, on-site excellent Training and Consulting s11: Liberty Associates, Inc. provides custom .NET development, on-site excellent Training and Consulting

The Length property returns the length of the entire string, and the index operator ([]) is used to access a particular character within a string. Just like arrays, the index numbering in a string starts at zero.

Console.WriteLine( "\nString s3 is {0} characters long. ", s5.Length); Console.WriteLine( "The 5th character is {0}\n", s3[4]);

Here's the output:

String s3 is 4 characters long. The 5th character is r

The EndsWith( ) method asks a string whether a substring is found at the end of the string. Thus, you might first ask whether s3 ends with the word Training (which it does not), and then whether it ends with the word Consulting (which it does):

Console.WriteLine("s3:{0}\nEnds with Training?: {1}\n", s3, s3.EndsWith("Training") ); Console.WriteLine( "Ends with Consulting?: {0}", s3.EndsWith("Consulting"));

The output reflects that the first test fails and the second succeeds:

Ends with Training?: False Ends with Consulting?: True

The IndexOf( ) method locates a substring within a string, and the Insert( ) method inserts a new substring into a copy of the original string. The following code locates the first occurrence of Training in s3:

Console.WriteLine("\nThe first occurrence of Training "); Console.WriteLine ("in s3 is {0}\n", s3.IndexOf("Training"));

The output indicates that the offset is 71:

The first occurrence of Training in s3 is 71

Then use that value to insert the word excellent, followed by a space, into that string.

Actually, the insertion is into a copy of the string returned by the Insert( ) method and assigned to s10:

string s10 = s3.Insert(71,"excellent "); Console.WriteLine("s10: {0}\n",s10);

Here's the output:

s10: Liberty Associates, Inc. provides custom .NET development, on-site excellent Training and Consulting

Finally, you can combine these operations to make a more efficient insertion statement:

string s11 = s3.Insert(s3.IndexOf("Training"),"excellent "); Console.WriteLine("s11: {0}\n",s11);

with the identical result:

s11: Liberty Associates, Inc. provides custom .NET development, on-site excellent Training and Consulting



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.