DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    Func<string, string> RemoveDuplicate = delegate(string s)
            {
                BitArray _arr = new BitArray(256);
                StringBuilder _sb = new StringBuilder();
                s = s.ToLower();
                for (int i = 0; i < s.Length; i++)
                {
                    if (_arr[(int)s[i]])
                    {
                        continue;
                    }
                    else
                    {
                        _arr[(int)s[i]] = true;
                        _sb.Append(s[i]);
                    }
                }
                return _sb.ToString();
            };                
                    Func<string, string> ReverseString = delegate(string s)
            {
                char[] word = s.ToCharArray();
                for (int i = 0, j = (s.Length - 1); i < j; i++, j--)
                {
                    char _temp = word[i];
                    word[i] = s[j];
                    word[j] = _temp;
                }

                return new string(word);
            };                
                    
string strCheck = '';
bool checkString = String.IsNullOrEmpty(strText);
                
                    
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<string> query =
  Enumerable.Select (
    Enumerable.OrderBy (
      Enumerable.Where (
        names, n => n.Contains ("a")
      ), n => n.Length
    ), n => n.ToUpper()
  );
  
                
                    
string strReverse ="";
string straoriginal = "Test";
if ((string.Compare(strOriginal, strReverse , false)) < 0)
    {
        MessageBox.Show("Less");
    }
    else if ((string.Compare(strOriginal, strReverse , false)) > 0)
    {
        MessageBox.Show("Greater");
    }
    else if ((string.Compare(strOriginal, strReverse , false)) == 0)
    {
        MessageBox.Show("Equal");
    }
                
                    
string strReverse ="";
string straoriginal = "Test";
string strReverse= Microsoft.VisualBasic.Strings.StrReverse(strOriginal);
    MessageBox.Show(strReverse); 
                
                    
Progessive form 
IEnumerable<string> filtered   = names.Where      (n => n.Contains ("a"));
IEnumerable<string> sorted     = filtered.OrderBy (n => n.Length);
IEnumerable<string> finalQuery = sorted.Select    (n => n.ToUpper());
Output

Harry
Mary
Jay

Jay
Mary
Harry

JAY
MARY
HARRY
 
 
 

 
 

                
                    
string string1= "Split function";
string [] SplitString = string1.Split(new Char [] {' '});
MessageBox.Show(Convert.ToString(SplitString [0]));
MessageBox.Show(Convert.ToString(SplitString [1])); 
                
                    Fluent query - LINQ

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<string> query = names
	.Where   (n => n.Contains ("a"))
	.OrderBy (n => n.Length)
	.Select  (n => n.ToUpper());
                
                    Simple Filter Query 
from n in new[] { "Tom", "Dick", "Harry" }
where n.Contains ("a")
select n

Ouput: Harry