Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# String Manipulation: Examples
01-27-2010, 04:38 PM
Post: #1
C# String Manipulation: Examples
Code:
/*
* Created by SharpDevelop.
* User: Back_Track
* Date: 1/26/2010
* Time: 11:20 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Text;

namespace String_Mainpulation
{
    class Program
    {
        public static void Main(string[] args)
        {
            //String To Int - Begin
            Console.WriteLine("Enter In Number: ");
            int Our_r;
            string Data = Console.ReadLine();//Make the variable Data = The imput of the console
            Int32.TryParse(Data, out Our_r);//Parse the Data of the String Data into the Int Out_r
            //String To Int - Done
            
            //String Builder Example - Begin
            string begin = "Hello P9!";
            StringBuilder SB = new StringBuilder(begin);
            Console.WriteLine("At what point do you want to insert the Character '#'?");
            int num = Int32.Parse(Console.ReadLine());
            if(begin.Length < num)
            {
                Console.WriteLine("I'm Sorry, but there are not enough characters in this string for that");
            }
            else{
            SB.Insert(num,"#");
            }
            Console.WriteLine(SB);
            // String Builder Example - End
            
            //Replacement of chars in a string - Begin
            StringBuilder Strb = new StringBuilder();
            String Alphabet = "abcdefghijlmnopqrstuvwxyz";
            Strb.Append(Alphabet);
            Strb.Replace("a","");//bad way to do it i tried replacing with a char array with all the vowels but didn't work
            Strb.Replace("e","");//also with string[]
            Strb.Replace("i","");//Helping with this code would be nice
            Strb.Replace("o","");//i tried
            Strb.Replace("u","");//Char[] Vowels = {'a','e','i','o','u'};
            //Strb.Replace(Vowels,""); but the thing is the two parameters have to be strings so it ended up like this
            //Strb.Replace(Vowels.ToString(),""); and did not work
            Console.WriteLine("\n"+Strb);
            Console.ReadLine();
            //Replacement of chars in a string - End
            
            //String Length's - Begin
            string MyLength = "Absdgbifd gffghf ui";
            string YourLength = "gx fhi iufdhg";
                int Difference_Me, Difference_You;
            Difference_Me = MyLength.Length   -  YourLength.Length;
            Difference_You = YourLength.Length - MyLength.Length;
                if(MyLength.Length > YourLength.Length)
            {
                
                Console.WriteLine("MyLength Legth is bigger than yours by : {0}",Difference_Me);
            }
                else{
                    Console.WriteLine("Your Length is bigger than mine by : {0}", Difference_You);
                
                }
            
            //String Length's - End
            Console.ReadLine();
        }
    }
}

Just some string manipulation i practiced with , could have a good use for people completely new to C#.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
01-27-2010, 04:41 PM
Post: #2
RE: C# String Manipulation: Examples
Very good string cutting and manipulation. C# has really good functions for that. I like your character insert and vowels. Great job !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-27-2010, 09:39 PM
Post: #3
RE: C# String Manipulation: Examples
What is the user entered a negative number?
int num = Int32.Parse(Console.ReadLine());
if(begin.Length < num)
{
Console.WriteLine("I'm Sorry, but there are not enough characters in this string for that");
}
Visit this user's website Find all posts by this user
Quote this message in a reply
01-27-2010, 09:51 PM (This post was last modified: 01-28-2010 06:16 AM by Back_track.)
Post: #4
RE: C# String Manipulation: Examples
The program would crash...
i'll just add
if(begin.length < 0)
{
//Code here
}

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
01-27-2010, 09:54 PM
Post: #5
RE: C# String Manipulation: Examples
d;0) lol
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: