Csharp/CSharp Tutorial/String/String Insert Replace

Материал из .Net Framework эксперт
Версия от 12:16, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Replace characters

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); 
 
  } 
}
Original string: This is a test
This is X test

Replace string

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); 
 
  } 
}
Original string: This test
Thwas test

Replace substring

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

Reversing a String

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);
      }
  }
}

Reversing Characters

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();
        }
    }

String Inserting

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); 
 
  } 
}
Original string: This test
This is a test