Thursday 30 June 2011

C# - String Extensions - Various

Usage


Various fairly self-explanatory short string extensions.

Snippet


using System;
using System.Collections.Generic;
using System.Linq;

namespace My.Helpers
{
    // String extension methods.

    public static class StringExtensions
    {
        public static int CompareTrimmed(this string text, string str, bool ignoreCase = true)
        {
            return String.Compare(text.Trim(), str.Trim(), ignoreCase);
        }

        public static bool ContainsIgnoreCase(this string text, string value)
        {
            return (text.IndexOf(value, System.StringComparison.OrdinalIgnoreCase) > 0);
        }

        public static bool ContainsIgnoreCase(this string text, string value, out int index)
        {
            index = text.IndexOf(value, System.StringComparison.OrdinalIgnoreCase);
            return (index >= 0);
        }

        public static bool EndsWithIgnoreCase(this string text, string value)
        {
            return text.EndsWith(value, System.StringComparison.CurrentCultureIgnoreCase);
        }

        public static string Left(this string text, int maxLength)
        {
            if (maxLength <= 0)
                return "";

            int count = Math.Min(maxLength, text.Length);

            return text.Substring(0, count);
        }

        public static string ReplaceIgnoreCase(this string text, string value1, string value2)
        {
            string work = text;
            int start = 0;
            var caseInsensitive = System.StringComparison.CurrentCultureIgnoreCase;

            while (true)
            {
                int i = work.IndexOf(value1, start, caseInsensitive);
                if (i < 0) break;

                work = work.Left(i) + value2 + work.Right(work.Length - i - value1.Length);

                start += value2.Length;
            }

            return work;
        }

        public static string Right(this string text, int maxLength)
        {
            if (maxLength <= 0)
                return "";

            int start = Math.Max(text.Length - maxLength, 0);
            int count = Math.Min(maxLength, text.Length - start);
            
            return text.Substring(start, count);
        }

        public static bool StartsWithIgnoreCase(this string text, string value)
        {
            return text.StartsWith(value, System.StringComparison.CurrentCultureIgnoreCase);
        }

        public static string StripNonNumeric(this string text)
        {
            return text.StripNonNumeric(false);
        }

        public static string StripNonNumeric(this string text, bool stopAtFirst)
        {
            string result = "";

            foreach (var c in text)
            {
                if ((c.CompareTo('0') >= 0) && (c.CompareTo('9') <= 0))
                    result += c;
                else
                {
                    if (stopAtFirst)
                        break;
                }
            }

            return result;
        }

        public static byte[] ToByteArray(this string text)
        {
            int count = text.Length;
            byte[] result = new byte[count];

            for (int i = 0; i < count; i++)
                result[i] = (byte)text[i];

            return result;
        }

        public static string ToCsv(this string text)
        {
            // Converts comma-delimited or space-delimited to comma-delimited.
            string[] fields;

            if (text.Contains(",") || !text.Contains(" "))
                return text;

            // 'text' contains one or more spaces.
            string result = "";
            fields = text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string field in fields)
            {
                if (result != "")
                    result += ",";

                result += field.Trim();
            }

            return result;
        }

        public static int[] ToIntArray(this string text)
        {
            return Array.ConvertAll<string, int>(text.ToCsv().ToStringArray(), int.Parse);
        }

        public static List<int> ToListInt(this string text)
        {
            return text.ToIntArray().ToList<int>();
        }

        public static string[] ToStringArray(this string text)
        {
            string[] result = text.ToCsv().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < result.Count(); i++)
                result[i] = result[i].Trim();

            return result;
        }

        public static List<string> ToListString(this string text)
        {
            return text.ToStringArray().ToList<string>();
        }
    }
}

No comments:

Post a Comment