Thursday, October 9, 2008

Remove some characters from string in C#

namespace RemoveCharacters
{
class Program
{
static void Main(string[] args)
{
string[] htmlComments = new string[] {"",
"",
"",
""};
char[] commentChar = new char[] { '!','-','<','>'};

Original(htmlComments);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
RemoveCommentCharacter(htmlComments,commentChar);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
RemoveWithReplace(htmlComments,commentChar);
}

public static void Original(string[] str)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
Console.WriteLine("Comments: "+str[i]);
}
}

public static void RemoveCommentCharacter(string[] str,char[] ch)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
str[i] = str[i].Trim(ch);
str[i] = str[i].TrimEnd(ch);
Console.WriteLine("Comments: " + str[i]);
}
}

public static void RemoveWithReplace(string[] str,char[] ch)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
for (int j = 0; j < ch.Length; j++)
{
str[i] = str[i].Replace(ch[j].ToString(),"");
}
Console.WriteLine("Comments: " + str[i]);
}
}
}
}

Merge two strings with C#

static void Main(string[] args)
{
string str1 = "hello";
string str2 = "kevin";
string str = string.Concat(str1," ",str2);//this is a good method to merge strings
Console.WriteLine(str);
string str3 = str1 + " " + str2;
Console.WriteLine(str3);
Console.WriteLine();
}

PadLeft and PadRight to compose the string(C#)

namespace PadLeftAndPadRight
{
class Program
{
static void Main(string[] args)
{
string[,] name = new string[,] { {"1504","Marry","Ella","Steve","Bob"},{"1133","Elizabeth","Alex","David","Joe"},{"2624","Jeol","Chris","Craig","Bill"}};
Console.WriteLine("Original name list");
Original(name);
Console.WriteLine("Left pad name list");
Left(name);
Console.WriteLine("Right pad name list");
Right(name);
}

public static void Original(string[,] str)
{
for (int outer = 0; outer <= str.GetUpperBound(0); outer++)
{
for (int inner = 0; inner <= str.GetUpperBound(1); inner++)
{
Console.Write(str[outer, inner]+" ");
}
Console.WriteLine();
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
public static void Left(string[,] str)
{
for (int outer = 0; outer <= str.GetUpperBound(0); outer++)
{
for (int inner = 0; inner <= str.GetUpperBound(1); inner++)
{
Console.Write(str[outer, inner].PadLeft(10) + " ");
}
Console.WriteLine();
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}

public static void Right(string[,] str)
{
for (int outer = 0; outer <= str.GetUpperBound(0); outer++)
{
for (int inner = 0; inner <= str.GetUpperBound(1); inner++)
{
Console.Write(str[outer, inner].PadRight(10) + " ");
}
Console.WriteLine();
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
}

Insert,Remove and Replace method for string(C#)

there're many popular methods used in string class, here're some i want to share with you guys.
let's go!

namespace InsertRemoveAndReplace
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello, . Welcome to my class.";
string name = "kevin";
int pos = str1.IndexOf(",");
str1 = str1.Insert(pos + 2, name);
Console.WriteLine(str1);
str1 = str1.Remove(pos + 2, name.Length);
Console.WriteLine(str1);
Console.WriteLine("~~~~~~~~~~~~~~~~");
string[] words = new string[] { "recieve","decieve","reciept"};
for (int i = 0; i <= words.GetUpperBound(0); i++)
{
words[i] = words[i].Replace("cie", "cei");
Console.WriteLine(words[i]);
}
}
}
}

蝶恋花---浮萍

《蝶恋花》
平生几度映夕阳?
思绪万千,
愁酒断肠。
夜夜榻前独赏月,
怎奈窗外风凄凉。
檐下新草苔渐长,
为问新愁,
何事处处有?
独步异乡思满腔,
鬼蜮桃园两茫茫。

Matrix output with C#

namespace OutPut
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("input row number...");
int input = int.Parse(Console.ReadLine());
ShowNumber(input);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~");
}

public static void ShowNumber(int RowNumber)
{
int index = RowNumber - 1;
int[,] Arr = new int[RowNumber,RowNumber];

if (RowNumber <= 0)
{
Console.WriteLine("the row number must be bigger than 0");
}
else if (RowNumber == 1)
{
Arr[0, 0] = 1;
}
else if (RowNumber == 2)
{
Arr[0, 0] = 1;
Arr[1, 0] = 1;
Arr[1, 1] = 1;
}
else
{
Arr[0, 0] = 1;
Arr[1, 0] = 1;
Arr[1, 1] = 1;
for (int row = 0; row < RowNumber; row++)
{
for (int col = 0; col <= row; col++)
{
if (col == 0)
{
Arr[row, 0] = 1;
}
else if (col == row)
{
Arr[row, col] = 1;
}
else
{
Arr[row, col] = Arr[row - 1, col] + Arr[row - 1, col - 1];
}
}
}
}

for (int i = 0; i < RowNumber; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == i)
{
Console.WriteLine(Arr[i, j]);
}
else
{
Console.Write(Arr[i,j]+" ");
}
}
}
}
}
}

// belowing is the output with 6 input

Method for comparing string

//this is a sample for Equals method
static void Main(string[] args)
{
string s1 = "foobar";
string s2 = "foobara";
if (s1.Equals(s2))//Equals method will return true or false
{
Console.WriteLine("they are the same");
}
else
{
Console.WriteLine("thet are not the same");
}
}
/////////////////////////////////////

//this is a sample for CompareTo method
static void Main(string[] args)
{
//if two strings are equal,the CompareTo method return 0.
//if the passed-in string is "below" the method string,reutrn -1.
//if the passed-in string is "above" the method-calling,return 1.
string s1 = "foobar";
string s2 = "foobar";
string s3 = "foofoo";
string s4 = "fooaar";
Console.WriteLine(s1.CompareTo(s2));
Console.WriteLine(s1.CompareTo(s3));
Console.WriteLine(s1.CompareTo(s4));
}
//this is a Compare method
static void Main(string[] args)
{
//if two strings are equal,the CompareTo method return 0.
//if the passed-in string is "below" the method string,reutrn -1.
//if the passed-in string is "above" the method-calling,return 1.
string s1 = "foobar";
string s2 = "foobar";
string s3 = "foofoo";
string s4 = "fooaar";
Console.WriteLine(String.Compare(s1,s2));
Console.WriteLine(String.Compare(s1,s3));
Console.WriteLine(String.Compare(s1,s4));
}

//this is a compare method for StartsWith and EndsWith

using System.Collections;//
namespace CompareMethodAboutString
{
class Program
{
static void Main(string[] args)
{
string[] words = new string[] { "aaa","abc","abb","baa","bca"};
ArrayList StartWithA = new ArrayList();
ArrayList EndWithA = new ArrayList();
foreach (string word in words)
{
if (word.StartsWith("a"))
{
StartWithA.Add(word);
}
if (word.EndsWith("a"))
{
EndWithA.Add(word);
}
}
foreach (string word in StartWithA)
{
Console.WriteLine(word);
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~");
foreach (string word in EndWithA)
{
Console.WriteLine(word);
}
}
}
}

Split method And Join method About String class(C#)

this is a sample about split method and join method about string class.
any comments or advice are appreciated.


namespace SplitAndJoinAboutString
{
class Program
{
static void Main(string[] args)
{
string data = "name,age,gender,phone number,address,mail";
string[] sdata;
char[] delimiter = new char[] { ','};
sdata = data.Split(delimiter, data.Length);

foreach (string word in sdata)
{
Console.WriteLine(word);
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");
string joined;
joined = String.Join(",",sdata);//split string with char type variable, but join with string type
Console.WriteLine(joined);
}
}
}

A sample about string class

using System.Collections;//

namespace StringClass
{
class Program
{
static void Main(string[] args)
{
string input = " now is the good time to have a trip ";
ArrayList words = new ArrayList();
words = SplitWords(input);

foreach (string word in words)
{
Console.WriteLine(word);
}
Console.WriteLine("....");
}

public static ArrayList SplitWords(string astring)
{
ArrayList words = new ArrayList();
int pos;
string word;
astring = astring.Trim();

pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
if (pos < 0)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
}

return words;
}
}
}

event--delegate(C#)

namespace EventDelegate
{
public delegate void SubEventHandler();
public abstract class Subject
{
public event SubEventHandler SubEvent;
protected void FireAway()
{
if (this.SubEvent != null)
{
this.SubEvent();
}
}
}

public class Cat : Subject
{
public void Cry()
{
Console.WriteLine("Cat cry!");
this.FireAway();
}
}

public abstract class Observer
{
public Observer(Subject sub)
{
sub.SubEvent +=new SubEventHandler(Response);
}

public abstract void Response();
}

public class Mouse : Observer
{
private string name;
public Mouse(string name, Subject sub)
: base(sub)
{
this.name = name;
}

public override void Response()
{
Console.WriteLine(name+" attempt to escape!");
}
}

public class Master : Observer
{
public Master(Subject sub)
: base(sub)
{ }

public override void Response()
{
Console.WriteLine("master waked up!");
}
}

class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
Mouse mouse1 = new Mouse("mouse1", cat);
Mouse mouse2 = new Mouse("mouse2", cat);
Master master = new Master(cat);
cat.Cry();
}
}
}