Csharp/CSharp Tutorial/String/String Insert Replace — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:16, 26 мая 2010

Replace characters

<source lang="csharp">using System;

class MainClass {

 public static void Main() {  
   string str = "This is a test"; 

   Console.WriteLine("Original string: " + str); 
    
   // Replace characters 
   str = str.Replace("a", "X"); 
   Console.WriteLine(str); 

 } 

}</source>

Original string: This is a test
This is X test

Replace string

<source lang="csharp">using System;

class MainClass {

 public static void Main() {  
   string str = "This test"; 

   Console.WriteLine("Original string: " + str); 
    
   // Replace string 
   str = str.Replace("is", "was"); 
   Console.WriteLine(str); 

 } 

}</source>

Original string: This test
Thwas test

Replace substring

<source lang="csharp">using System;

 class Class1
 {
   [STAThread]
   static void Main(string[] args)
   {
           string replaceStr = "1234";
           string dest = "ABCDEFGHWXYZ";
           dest = dest.Replace( "EFGH", replaceStr );
           Console.WriteLine( dest );
   }
   }</source>

Reversing a String

<source lang="csharp">class Palindrome {

 static void Main()
 {
     string reverse, palindrome;
     char[] temp;
     System.Console.Write("Enter a palindrome: ");
     palindrome = System.Console.ReadLine();
     reverse = palindrome.Replace(" ", "");
     reverse = reverse.ToLower();
     temp = reverse.ToCharArray();
     System.Array.Reverse(temp);
     if(reverse == new string(temp))
     {
         System.Console.WriteLine("\"{0}\" is a palindrome.",palindrome);
     }
     else
     {
         System.Console.WriteLine("\"{0}\" is NOT a palindrome.",palindrome);
     }
 }

}</source>

Reversing Characters

<source lang="csharp">using System; using System.Text;

   class Class1
   {
       [STAThread]
       static void Main(string[] args)
       {
           string str = "abcdefghijklmnopqrstuvwxyz";
           str = ReverseString( str );
           Console.WriteLine( str );
           str = ReverseStringEnum( str );
           Console.WriteLine( str );
       }
       static string ReverseString( string strIn )
       {
           StringBuilder sb = new StringBuilder(strIn.Length);
           for( int i = 0; i < strIn.Length; ++i )
           {
               sb.Append( strIn[(strIn.Length-1)-i] );
           }
           return sb.ToString();
       }
       static string ReverseStringEnum( string strIn )
       {
           StringBuilder sb = new StringBuilder( strIn.Length );
           foreach( char ch in strIn )
           {
               sb.Insert( 0, ch );
           }
           return sb.ToString();
       }
   }</source>

String Inserting

<source lang="csharp">using System;

class MainClass {

 public static void Main() {  
   string str = "This test"; 

   Console.WriteLine("Original string: " + str); 
    
   // Insert 
   str = str.Insert(5, "is a "); 
   Console.WriteLine(str); 

 } 

}</source>

Original string: This test
This is a test