Eki 21 2009

Short way of converting file lines to Array by regex. Also pay attention to usage of HashSet

Category: C#Latif @ 23:57

 

Hi again,

 

You can see the code below that simply how to convert txt file lines to array by means of regex. Also give attention to usage of HashSet.

 

Before running the code, create a txt file named myTxtFile.txt.

 

Code with comment:

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Text.RegularExpressions;

 

namespace File2Array

{

    class Program

    {

        //Convert file lines to Array

        public static string[] FileLines2Array(string filename)

        {

            string[] returnArray;

            using (TextReader textReader = new StreamReader(new FileStream(filename, FileMode.Open)))

            {

                Regex regexForSplit = new Regex(Environment.NewLine);

                returnArray = regexForSplit.Split(textReader.ReadToEnd());

            }

            return returnArray;

        }

 

        //Convert file lines to HashSet

        public static HashSet<string> FileLines2HashSet(string filename)

        {

            HashSet<string> array2HashSet = new HashSet<string>();

            foreach (var item in FileLines2Array(filename))

            {

                array2HashSet.Add(item);

            }

            return array2HashSet;

        }

 

 

        static void Main(string[] args)

        {

            Console.WriteLine("Convert file lines to array");

            string[] testArray = FileLines2Array("myTxtFile.txt");

            foreach (var item in testArray)

            {

                Console.WriteLine(item);

            }

 

            Console.WriteLine("Delete duplicate lines automatically if any. Also see usage of HashSet");

            HashSet<string> testHashSet = FileLines2HashSet("myTxtFile.txt");

            foreach (var item in testHashSet)

            {

                Console.WriteLine(item);

            }

 

            Console.ReadKey(true);

        }

    }

}

 

 

See you soon...

Tags: , , ,

Ağu 26 2009

How to Capitalize the First Letter of Each Words in a string in C#

Category: C#Latif @ 16:24

Hi,

İf you wonder as to how to capitalize simply first letter of each words in a string, you can use ToTitleCase.

Check the sample below,

using System;

using System.Globalization;

 

namespace ToTitleCaseApp

{

    class Program

    {

        static void Main(string[] args)

        {

            string capitalizedFirstLetter = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("shoot for the moon. even if you miss, you'll land among the stars.");

            Console.WriteLine(capitalizedFirstLetter);

            Console.ReadKey();

        }

    }

}

 

Result :

Shoot For The Moon. Even If You Miss, You'll Land Among The Stars.

I think, it is a handy method.

have a good time!

Tags: ,

Haz 27 2009

[C#] Use a key string as an array index - Dictionary, SortedDictionary, Hashtable classes

Category: C#Latif @ 12:39

 

Hi,

   I will try to explain the differences between Dictionary, SortedDictionary, Hashtable and

at the same time,we will answer how to use a key string as an array index?

 

Namespace:  System.Collections.Generic

Dictionary(TKey, TValue) Class

SortedDictionary (TKey, TValue) Class

 

Namespace:  System.Collections

Hashtable

 

Code with explanation:

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Collections;

 

namespace genericTypes2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            //System.Collections.Generic.Dictionary

 

            Dictionary<string, string> dictionaryNew = new Dictionary<string, string>();

            //Use Add() to assign value

            dictionaryNew.Add("company", "TRIODOR");

            dictionaryNew.Add("grup", "MD INFO");

            dictionaryNew.Add("latif", "ozturk");

            dictionaryNew.Add("recep", "guzel");

            dictionaryNew.Add("talha", "turan");

            dictionaryNew.Add("pinar", "alasli");

            dictionaryNew.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNew["volkan"] = "cakar";

            dictionaryNew["bugra"] = "postaci";

            dictionaryNew["Erkan"] = "BALABAN";

 

            listBox7.Items.Add("DICTIONARY <>");

            listBox7.Items.Add(" ");

 

            //to list dictionaryNew in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNew)

            {

                listBox7.Items.Add(obj.Key + " " + obj.Value);

                //other way of getting value

                //listBox7.Items.Add(dictionaryNew["" + obj.Key + ""]);

            }

 

            //System.Collections.Generic.SortedDictionary

 

            SortedDictionary<string, string> dictionaryNewSorted = new SortedDictionary<string, string>();

            //Use Add() to assign value

            dictionaryNewSorted.Add("company", "TRIODOR");

            dictionaryNewSorted.Add("grup", "MD INFO");

            dictionaryNewSorted.Add("latif", "ozturk");

            dictionaryNewSorted.Add("recep", "guzel");

            dictionaryNewSorted.Add("talha", "turan");

            dictionaryNewSorted.Add("pinar", "alasli");

            dictionaryNewSorted.Add("elif", "onder");

            //You can assign value like that as well

            dictionaryNewSorted["volkan"] = "cakar";

            dictionaryNewSorted["bugra"] = "postaci";

            dictionaryNewSorted["Erkan"] = "BALABAN";

 

            listBox8.Items.Add("SORTED DICTIONARY<>");

            listBox8.Items.Add(" ");

 

 

            //to list dictionaryNewSorted in the ListBox - use KeyValuePair

            foreach (KeyValuePair<string, string> obj in dictionaryNewSorted)

            {

                listBox8.Items.Add(obj.Key + " " + obj.Value);

            }

 

 

            //System.Collections.Hashtable

 

            Hashtable hashTableNew = new Hashtable();

            //Use Add() to assign value

            hashTableNew.Add("company", "TRIODOR");

            hashTableNew.Add("grup", "MD INFO");

            hashTableNew.Add("latif", "ozturk");

            hashTableNew.Add("recep", "guzel");

            hashTableNew.Add("talha", "turan");

            hashTableNew.Add("pinar", "alasli");

            hashTableNew.Add("elif", "onder");

            //You can assign value like that as well

            hashTableNew["volkan"] = "cakar";

            hashTableNew["bugra"] = "postaci";

            hashTableNew["Erkan"] = "BALABAN";

 

            listBox9.Items.Add("HASH TABLE<>");

            listBox9.Items.Add(" ");

           

            //to list hashTableNew in the ListBox - use DictionaryEntry

            foreach (DictionaryEntry obj in hashTableNew)

            {

                listBox9.Items.Add(obj.Key + " " + obj.Value);

            }

 

        }

    }

}

 

 

 

It is easy to see the differences between Dictionary, SortedDictionary, Hashtable in the picture above.

 

Dictionary -> uses FIFO

SortedDictionary -> sorting by array index

Hashtable -> sorting by hash algorithm

 

And also don’t forget that Dictionary is faster than others.

 

See you...

 

Tags: , , , ,