String Copy, End With and Insert : String « Data Types « C# / C Sharp






String Copy, End With and Insert

String Copy, End With and Insert
  
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace StringManipulation
 {
    public class TesterStringCopyEndWithInsert
    {
       public void Run()
       {
           string s1 = "abcd";
           string s2 = "ABCD";
           string s3 = @"Liberty Associates, Inc.
                 provides custom .NET development,
                 on-site Training and Consulting";

           // the string copy method
           string s5 = string.Copy(s2);
           Console.WriteLine(
               "s5 copied from s2: {0}", s5);

           // Two useful properties: the index and the length
           Console.WriteLine(
               "\nString s3 is {0} characters long. ",
               s5.Length);

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

           // test whether a string ends with a set of characters
           Console.WriteLine("s3:{0}\nEnds with Training?: {1}\n",
               s3,
               s3.EndsWith("Training") );
           Console.WriteLine(
               "Ends with Consulting?: {0}",
               s3.EndsWith("Consulting"));

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

           // insert the word excellent before "training"
           string s10 = s3.Insert(101,"excellent ");
           Console.WriteLine("s10: {0}\n",s10);

           // you can combine the two as follows:
           string s11 = s3.Insert(s3.IndexOf("Training"),
               "excellent ");
           Console.WriteLine("s11: {0}\n",s11);
       }

       [STAThread]
       static void Main()
       {
          TesterStringCopyEndWithInsert t = new TesterStringCopyEndWithInsert();
          t.Run();
       }
    }
 }


           
         
    
  








Related examples in the same category

1.Is Palindrome
2.create some strings
3.use the Concat() method to concatenate strings
4.use the addition operator (+) to concatenate strings
5.use the Copy() method to copy a string
6.use the Join() method to join strings
7.use the StartsWith() and EndsWith() methods to check if a string contains a specified substring at the start and end
8.use the Substring() method to retrieve substrings
9.use the Trim(), TrimStart(), and TrimEnd() methods to trim strings
10.use the PadLeft() and PadRight() methods to align strings
11.Decoding a Base64-encoded BinaryDecoding a Base64-encoded Binary
12.From Base 64 Decode StringFrom Base 64 Decode String
13.Using Strings
14.Creating Strings
15.string: Changing Charactersstring: Changing Characters
16.Removing CharactersRemoving Characters
17.Padding StringsPadding Strings
18.Joining StringsJoining Strings
19.Extracting SubstringsExtracting Substrings
20.String Concatenation 2String Concatenation 2
21.Substring demoSubstring demo
22.Trimming String SpacesTrimming String Spaces
23.Introduce stringIntroduce string
24.Some string operationsSome string operations
25.Display the digits of an integer using wordsDisplay the digits of an integer using words
26.Use Substring() 1Use Substring() 1
27.A string can control a switch statementA string can control a switch statement
28.Illustrates the use of strings 1Illustrates the use of strings 1
29.String ManipulationString Manipulation
30.String Manipulation ConcatenateString Manipulation Concatenate
31.String copyString copy
32.Demonstrate escape sequences in strings. Demonstrate escape sequences in strings.
33.Demonstrate verbatim literal stringsDemonstrate verbatim literal strings
34.Display a string in reverse by using recursionDisplay a string in reverse by using recursion
35.Demonstrate Concat()Demonstrate Concat()
36.Demonstrate Concat() 2Demonstrate Concat() 2
37.Trimming and paddingTrimming and padding
38.Use Substring() 2Use Substring() 2
39.Strings: Regular ExpressionsStrings: Regular Expressions
40.String InterningString Interning
41.Lexical DetailsLexical Details