Replace char in a StringBuilder object

   
 
using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        StringBuilder greetingBuilder = new StringBuilder("www.kutayzorlu.com/java2s/com");

        for (int i = (int)'z'; i >= (int)'a'; i--) {
            char old1 = (char)i;
            char new1 = (char)(i + 1);
            greetingBuilder = greetingBuilder.Replace(old1, new1);
        }

        for (int i = (int)'Z'; i >= (int)'A'; i--) {
            char old1 = (char)i;
            char new1 = (char)(i + 1);
            greetingBuilder = greetingBuilder.Replace(old1, new1);
        }

        Console.WriteLine("Encoded:
" + greetingBuilder.ToString());
    }
}

    


Use StringBuilder to reverse a string

   
 

using System;
using System.Text;

class MainClass {
    public static string ReverseString(string str)
    {
       if (str == null || str.Length <= 1) 
       {
            return str;
       }

       StringBuilder revStr = new StringBuilder(str.Length);


       for (int count = str.Length - 1; count > -1; count--)
       {
           revStr.Append(str[count]);
       }

       return revStr.ToString();
   }
    public static void Main() {
        Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
    }
}

    


Length and Indexer

   
 

using System;
using System.Text;

class MainClass
{
    public static string ReverseString(string str)
    {
       if (str == null || str.Length <= 1) 
       {
            return str;
       }

       StringBuilder revStr = new StringBuilder(str.Length);


       for (int count = str.Length - 1; count > -1; count--)
       {
           revStr.Append(str[count]);
       }

       return revStr.ToString();
   }
   public static void Main(){
       Console.WriteLine(ReverseString("Madam Im Adam"));
       Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
   }
}

    


Get Text Element Enumerator


   


using System;
using System.Globalization;
using System.Threading;

class Class1 {
  static void Main(string[] args) {
         TextElementEnumerator Iter;
         String MyStr, OutBuf;

         MyStr = "The Quick programmer ran rings around the lazy manager";

         //Lets do the iterator thing
         Iter = StringInfo.GetTextElementEnumerator(MyStr);
         while (Iter.MoveNext())
         {
            OutBuf = "Character at position " + 
                     Iter.ElementIndex.ToString() + 
                     " = " + Iter.Current;
            Console.WriteLine(OutBuf);
         }
   }
}