Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, February 11, 2009

Sorting 2D string list in C#




/*
* In application code
*/
// Declare a variable for data
public static List recordList = new List();


// Set a list
// For example,
// 2/11/2009, apple, 20
// 2/10/2009, banana, 5
// 1/30/2009, pineapple, 3
recodeList.Add(new clsDataRecord(DateTimeVar1, TextVar1, IntVar1));
recodeList.Add(new clsDataRecord(DateTimeVar2, TextVar2, IntVar2));
recodeList.Add(new clsDataRecord(DateTimeVar3, TextVar3, IntVar3));


// Call sorting by date
clsDataRecord.CompType = clsDataRecord.ComparisonType.Date;
Program.altRecordList.Sort();
// Sorted result will be
// 1/30/2009, pineapple, 3
// 2/10/2009, banana, 5
// 2/11/2009, apple, 20


// Call sorting by text
clsDataRecord.CompType = clsDataRecord.ComparisonType.Text;
Program.altRecordList.Sort();
// Sorted result will be
// 2/11/2009, apple, 20
// 2/10/2009, banana, 5
// 1/30/2009, pineapple, 3


// Call sorting by number
clsDataRecord.CompType = clsDataRecord.ComparisonType.Number;
Program.altRecordList.Sort();
// Sorted result will be
// 1/30/2009, pineapple, 3
// 2/10/2009, banana, 5
// 2/11/2009, apple, 20




/*
* Class for clsDataRecord
*/
using System;
using System.Collections;


class clsDataRecord : IComparable
{
public enum ComparisonType
{
Date = 1,
Text = 2,
Number = 3
};


// This class has three different members.
// You can customize these with your own data.
private DateTime _date;
private string _text;
private int _number;


private static ComparisonType _compType;


public clsDataRecord(DateTime date, string text, int number)
{
_date = date;
_text = text;
_number = number;
}


public DateTime Date
{
get { return _date; }
set { _date = value; }
}


public string Text
{
get { return _text; }
set { _text = value; }
}


public int Number
{
get { return _number; }
set { _number = value; }
}


public static ComparisonType CompType
{
get { return _compType; }
set { _compType = value; }
}


public override string ToString()
{
return String.Format("{0} - {1} - {2}", _date.ToShortDateString(), _text, _number.ToString());
}


public int CompareTo(object obj)
{
if (obj is clsDataRecord)
{
clsDataRecord r2 = (clsDataRecord)obj;


switch (_compType)
{
case ComparisonType.Date:
return _date.CompareTo(r2.Date);
case ComparisonType.Text:
return _text.CompareTo(r2.Text);
case ComparisonType.Number:
default:
return _number.CompareTo(r2.Number);
}
}
else
throw new ArgumentException("Object is not an clsDataRecord.");
}
}

Friday, February 6, 2009

string to integer in C#

Example

string str = "09";
int year = int.Parse(str) + 2000;

Output
year == 2009

Split string in C#

Example code

class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}

Output

Original text: 'one two three:four,five six seven'
7 words in text:
one
two
three
four
five
six
seven

Reference: http://msdn.microsoft.com/en-us/library/ms228388(VS.80).aspx