Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / Windows Forms

Code First: A Practical Case

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
26 Feb 2012CPOL15 min read 72.4K   3.7K   67  
A code first real life data model case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Xah.MediaCatalogerLib
{
    public static class StringExtensions
    {
        public static string DecodeHtml(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        public static string EncodeHtml(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }

        public static string EncodeANSIUrl(this string str)
        {
            return HttpUtility.UrlEncode(str, Encoding.Default);
        }

        public static string FindFirstBetween(this string str, string begin, string end)
        {
            string[] matches = FindBetween(str, begin, end, true, false);
            return (matches.Length > 0) ? matches[0] : String.Empty;
        }

        public static string FindLastBetween(this string str, string begin, string end)
        {
            string[] matches = FindBetween(str, begin, end, false, false);
            return (matches.Length > 0) ? matches[matches.Length - 1] : String.Empty;
        }

        public static string[] FindBetween(this string str, string begin, string end)
        {
            return FindBetween(str, begin, end, false, false);
        }

        public static string[] FindBetweenTrim(this string str, string begin, string end)
        {
            return FindBetween(str, begin, end, false, true);
        }

        private static string[] FindBetween(this string str, string begin, string end, bool first, bool trim)
        {
            List<string> matches = new List<string>();

            int start = 0;
            while ((start = str.IndexOf(begin, start)) >= 0)
            {
                start += begin.Length;
                int stop = str.IndexOf(end, start);
                if (stop >= start)
                {
                    string match = str.Substring(start, stop - start);
                    if (trim) match = match.Trim();
                    matches.Add(match);
                }
                else
                {
                    stop = str.Length - end.Length;
                }

                start = stop + end.Length;

                if (first) break;
            }

            return matches.ToArray();
        }

        public static string RemoveBetween(this string str, string begin, string end)
        {
            string result = str;
            
            int start = 0;
            while ((start = result.IndexOf(begin, start)) >= 0)
            {
                int stop = result.IndexOf(end, start + begin.Length);
                if (stop == -1) break;

                result = result.Remove(start, stop + end.Length - start);
            }

            return result;
        }

        public static string UntilMatch(this string str, string match)
        {
            int stop = str.IndexOf(match);
            if (stop < 0) return str;

            return str.Substring(0, stop);
        }

        public static string FromMatch(this string str, string match)
        {
            int start = str.IndexOf(match);
            if (start < 0) return String.Empty;

            start += match.Length;
            return str.Substring(start, str.Length - start);
        }

        public static bool Contains(this string str, string match, StringComparison comparison)
        {
            return str.IndexOf(match, comparison) >= 0;
        }

        public static long SafeConvertToInt64(this string str)
        {
            long converted = 0;
            try
            {
                converted = Convert.ToInt64(str);
            }
            catch { }

            return converted;
        }

        public static int SafeConvertToInt32(this string str)
        {
            int converted = 0;
            try
            {
                converted = Convert.ToInt32(str);
            }
            catch { }

            return converted;
        }

        public static short SafeConvertToInt16(this string str)
        {
            short converted = 0;
            try
            {
                converted = Convert.ToInt16(str);
            }
            catch { }

            return converted;
        }

        public static byte SafeConvertToByte(this string str)
        {
            byte converted = 0;
            try
            {
                converted = Convert.ToByte(str);
            }
            catch { }

            return converted;
        }

        public static float SafeConvertToSingle(this string str)
        {
            float converted = 0;
            try
            {
                converted = Convert.ToSingle(str.Replace(',','.'), System.Globalization.CultureInfo.InvariantCulture);
            }
            catch { }

            return converted;
        }

        public static string NormalizeNewLines(this string str)
        {
            StringReader sr = new StringReader(str);
            StringWriter sw = new StringWriter();
            
            string line;
            while ((line = sr.ReadLine()) != null)
                sw.WriteLine(line);

            return sw.ToString();
        }

        public static string RemoveNewLines(this string str, bool trim)
        {
            StringReader sr = new StringReader(str);
            StringWriter sw = new StringWriter();

            string line;
            while ((line = sr.ReadLine()) != null)
                sw.Write(trim ? line.Trim() : line);

            return sw.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Spain Spain
I studied Telecommunication with spezialization in Sound & Image. I was always very interested in programming at university as well and that is how I earn a living.

Since some years ago, I am also succesfully involved in software architecture and design.

Comments and Discussions