Tuesday, February 28, 2012

Gentrate Equivalent Regular Expression From String

public static String CreateRegexFromString(String inString) { 
        StringBuilder newString = new StringBuilder(); 
        for(int i=0; i<inString.length(); ++i) { 
            char eachCharacher = inString.charAt(i); 
            if ("\\.^$|?*+[]{}()".indexOf(eachCharacher) != -1) 
                newString.append('\\').append(eachCharacher); 
            else if (Character.isLetter(eachCharacher))
               
                newString.append("["+Character.toLowerCase(eachCharacher)+Character.toUpperCase(eachCharacher)+"]"); 
            else if (Character.isDigit(eachCharacher)) 
                newString.append("\\d"); 
            else 
                newString.append(eachCharacher); 
        } 
        return newString.toString(); 
    }

Sunday, February 5, 2012

What are the access Modifiers in C# ?

Access modifier specify the accessibility of a variable or and object. That means who can access the object and how can it be accessed.which controls whether they can be used from other code in your assembly or other assemblies. You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

public : 
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private:
The type or member can be accessed only by code in the same class or struct.

Saturday, February 4, 2012

How a thread is created ?


First, create a new Thread Start delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.Start method to run your method.


In Detail :


1. Create a System.Threading.Thread object.
2. Create the call back function
3. Starting the Thread




Thread newThread= new Thread(new ThreadStart (MyCallbackFunction));


                 /  \                                                      /  \
                   |         (1)                                            |          (2)




newThread.Start()


        /  \
          |        (3)







C# get file name from file path

  String filePath = @"c:\file\neme\file.txt";

=> filePath.Split('\\')[filePath.Split('\\').Length-1]     ---->   Will give file name with extension

=> (filePath.Split('\\')[filePath.Split('\\').Length-1]).Split('.')[0] --> Will give file name without extension 


=> filePath.Split('\\')[0]  --> Will give drive letter 


Lets Discuss Deligates


A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners. It encapsulates a reference of a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Basically it is similar like the old "C" age function pointer, where functions can be assigned like a variable and called in the run time based on dynamic conditions. C# delegate is the smarter version of function pointer which helps software architects a lot, specially while utilizing design patterns.

At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.



Syntax :

Friday, February 3, 2012

Setup passwordless ssh in Ubuntu, ssh localhost


1. ssh-keygen -t rsa -P "" 
 
-> this will generate key pair file in the current user .ssh folder
-> which is in home folder of user just press ctrl + h to show hidden files 
-> after giving this command just keep pressing enter dont enter file name or so
 
it will show    ->   Generating public/private rsa key pair.
 
Enter file in which to save the key    -> just press enter on this.
 
after this you will get the shell prompt back there you type
 
2. cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys 
 
-> this will create authorized_keys in .ssh folder and put your public key in that so that
-> you just type ssh localhost it will login to localhost without asking the password.
 
 

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...