Monday, April 20, 2009

Find Longest Repeated and Most Occured string part in comma seperated values[CSV]

        private string findRepeat(string strfilenames)
        {
            string[] strfiles = Regex.Split(strfilenames, ",");
            string keystr = "";
            DataTable dt = new DataTable("StrList");
            DataColumn dcCount = new DataColumn("count", System.Type.GetType("System.Int32"));
            DataColumn dcKey = new DataColumn("keystr");
            dt.Columns.Add(dcCount);
            dt.Columns.Add(dcKey);
            foreach (string file in strfiles)
            {
                keystr = file;
                for (int i = 0; i <>
                {
                    if (keystr.Length == 0) break;
                    Regex exp = new Regex(keystr + "+",RegexOptions.IgnoreCase);
                    Match mymatch = exp.Match(strfilenames);
                    MatchCollection mycoll = exp.Matches(strfilenames);
                    if (mycoll.Count > 1)
                    {
                        DataRow dr;
                        dr=dt.NewRow();
                        dr["count"] =(mycoll.Count + keystr.Length);
                        dr["keystr"] = keystr;
                        dt.Rows.Add(dr);
                    }
                    keystr = keystr.Substring(0, keystr.Length - 1);
                }
            }
            DataRow[] drs = dt.Select("count > 1", "count DESC");
            if (drs.Length>1)
            {
                return drs[0]["keystr"].ToString();
            }
            return "";
        }

Wednesday, April 1, 2009

?? Operator (C# Reference)

?? Operator (C# Reference): "?? Operator (C# Reference)
The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand."

eg:
varXThatNotAcceptsNull = varYWhichMayContainNull ?? VarZThatHasADefaultValueForVarX ;