Helper functions to find pattern contaned in string from pattern List

Below are a few helper functions for List<string> generic class.

They could be modified to be extension methods.

 

   //See also StringArrayHelper.cs, CollectionsHelper.cs

        public static class ListOfStringsHelper

        {

            public static bool StringContainsAnyFromList(List<string> patternsList, string sMsg)

            {

                bool bFound = patternsList.Exists(

                    delegate(string pattern)

                    {

                        return sMsg.Contains(pattern);

                    }

                    );

                return bFound;

            }

            public static string FindFirstListItemContainedInString(List<string> patternsList, string sMsg)

            {

                string sFound = patternsList.Find(

                    delegate(string pattern)

                    {

                        return sMsg.Contains(pattern);

                    }

                   );

                return sFound;

            }

        }

ToString function for Generic List

Below is the method to output Generic list of objects as string.

 

UPDATE: I've replaced my original function with the code suggested by James Curran (see his comment below).

 

    public static string ToString<T>(IEnumerable<T> messages, string sComment)
    {
        StringBuilder sRet = new StringBuilder(sComment);
        if (messages != null)
        {
            foreach (T msg in messages)
            {
                sRet.AppendLine(msg.ToString());
            }
        }
        return sRet.ToString();
    }

 

 

       /// <summary>

        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="messages"></param>
        /// <param name="separator"></param>
        /// <param name="sComment"></param>
        /// <returns></returns>
        /// See also http://www.codemeit.com/linq/c-array-delimited-tostring.html
        public static string ToString<T>(this IEnumerable<T> messages, string separator, string sComment)
        {
            if (messages == null)
                throw new ArgumentException("source can not be null.");
            StringBuilder sb = new StringBuilder(sComment);
            if (string.IsNullOrEmpty(separator))
            {
                separator = Environment.NewLine;
            }
            if (messages != null)
            {
                foreach (T msg in messages)
                {
                    if (msg != null)         
{

sb.Append(msg.ToString());

sb.Append(separator);

}
}

            }
            string sRet = StringHelper.TrimEnd(sb.ToString(), separator);
            return sRet;
        }
 

Similar function implemented as an extension method described in post:

Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable

 

Access to Relative To Application Root File

Sometimes you have a file relative to the root of application, that you want to read from your code. However when your code is build on development machine, the binary files are located in "\bin\debug","\bin\release" or just "\bin"(for VB or Web projects). And you need to make copy of files relative to bin/debug folder.

The function below helps to find the file, even if it was not copied to bin subfolder. (Should I name it ResolvePath smilar to Page.ResolveUrl?)

 

Note, that in most cases Visual Studio file "Copy to Output Directory" Property will be sufficient to achieve the same result.

        /// <summary>

        /// Function to find file in a root, even if bin directory is passed, which actually can be @"\bin\debug",@"\bin\release" or just @"\bin"

        /// </summary>

        /// <param name="sBinDirectory"></param>

        /// <param name="sRelativeFilePath"></param>

        /// <param name="bThrowExceptionIfNotExist"></param>

        /// <returns></returns>

        public static string GetPathForRelativeToRootFile(string sBinDirectory, string sRelativeFilePath, bool bThrowExceptionIfNotExist)

        {

            string sOriginalFullName = Path.Combine(sBinDirectory, sRelativeFilePath);

            string sFullName = sOriginalFullName;

            bool bExists = false;

            //Check if templateFileDirectory exists, if not and is not debug version, going up two levels to find template folder

            if (!File.Exists(sOriginalFullName))

            {

                sBinDirectory = StringHelper.TrimEnd(sBinDirectory, @"\").ToLower();

                //string sNewDir = "";

                string sEndBin = @"\bin\debug";

                bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                if (bExists == false)

                {

                    sEndBin = @"\bin\release";

                    bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                }

                if (bExists == false)

                {

                    sEndBin = @"\bin";

                    bExists = IsFileInParentDirExist(sBinDirectory, sEndBin, sRelativeFilePath, ref sFullName);

                }

            }

            else

            {

                bExists = true;

            }

            if (bExists == false)

            {

                if (bThrowExceptionIfNotExist == true)

                {

                    throw new FileNotFoundException("File not found in directory " + sBinDirectory, sRelativeFilePath);

                }

                else

                {

                    return sOriginalFullName;

 

                }

            }

            return sFullName;

        }

        //        ///

        /// <summary>

        /// helper method to be called from GetPathForRelativeToRootFile with different sEndBin parameters

        /// </summary>

        /// <param name="sBinDirectory"></param>

        /// <param name="sEndBin"></param>

        /// <param name="sRelativeFilePath"></param>

        /// <param name="sFullName"></param>

        /// <returns></returns>

        private static bool IsFileInParentDirExist(string sBinDirectory, string sEndBin, string sRelativeFilePath, ref string sFullName)

        {

            bool bExists = false;

            if (sBinDirectory.EndsWith(sEndBin))

            {

                string sNewDir = StringHelper.TrimEnd(sBinDirectory, sEndBin);

                sFullName = Path.Combine(sNewDir, sRelativeFilePath);

                if (File.Exists(sFullName))

                {

                    bExists = true;

                }

            }

            return bExists;

        }

Slightly related functions for ASP.NET discussed in ResolveUrl() without Page Rick Strahl's post.

«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910