Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.5K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
using System;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace QiHe.CodeLib
{
    /// <summary>
    /// StringHelper ��ժҪ˵����
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// Concats two strings with a delimiter.
        /// </summary>
        /// <param name="s1">string 1</param>
        /// <param name="delim">delimiter</param>
        /// <param name="s2">string 2</param>
        /// <returns></returns>
        public static string AddWithDelim(string s1, string delim, string s2)
        {
            if (string.IsNullOrEmpty(s1)) return s2;
            else return s1 + delim + s2;
        }
        public static string ContactWithDelim(string str1, string delim, string str2)
        {
            if (string.IsNullOrEmpty(str1)) return str2;
            else if (string.IsNullOrEmpty(str2)) return str1;
            else return str1 + delim + str2;
        }

        /// <summary>
        /// Contact with delim, delim is used after the first not Empty item
        /// </summary>
        /// <param name="items"></param>
        /// <param name="delim"></param>
        /// <returns></returns>
        public static string ContactWithDelimSkipEmpty(IEnumerable<string> items, string delim)
        {
            return ContactWithDelimSkipSome(items, delim, string.Empty);
        }

        /// <summary>
        /// Contact with delim, delim is used after the first not null item
        /// </summary>
        /// <param name="items"></param>
        /// <param name="delim"></param>
        /// <returns></returns>
        public static string ContactWithDelimSkipNull(IEnumerable<string> items, string delim)
        {
            return ContactWithDelimSkipSome(items, delim, null);
        }

        public static string ContactWithDelimSkipSome(IEnumerable<string> items, string delim, string skip)
        {
            StringBuilder text = new StringBuilder();
            bool isFirst = true;
            foreach (string item in items)
            {
                if (item == skip) continue;
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    text.Append(delim);
                }
                text.Append(item);
            }
            return text.ToString();
        }

        public static string ContactWithDelim(IEnumerable<string> items, string delim)
        {
            return ContactWithDelim(items, delim, null, null);
        }
        public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue)
        {
            return ContactWithDelim(items, delim, initialValue, null);
        }
        public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue, string endValue)
        {
            StringBuilder text = new StringBuilder(initialValue);
            bool isFirst = true;
            foreach (string item in items)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    text.Append(delim);
                }
                text.Append(item);
            }
            text.Append(endValue);
            return text.ToString();
        }

        public static string[] SplitWithSpaces(string text)
        {
            string pattern = "[^ ]+";
            Regex rgx = new Regex(pattern);
            MatchCollection mc = rgx.Matches(text);
            string[] items = new string[mc.Count];
            for (int i = 0; i < items.Length; i++)
            {
                items[i] = mc[i].Value;
            }
            return items;
        }


        public static string ContactWithDelim<T>(IEnumerable<T> items, string delim)
        {
            return ContactWithDelim<T>(items, delim, null, null);
        }

        public static string ContactWithDelim<T>(IEnumerable<T> items, string delim, string initialValue, string endValue)
        {
            StringBuilder text = new StringBuilder();
            text.Append(initialValue);
            bool isFirst = true;
            foreach (T item in items)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    text.Append(delim);
                }
                text.Append(item.ToString());
            }
            text.Append(endValue);
            return text.ToString();
        }

        public static string GetHeader(string text, int length)
        {
            if (text.Length <= length)
            {
                return text;
            }
            else
            {
                return text.Substring(0, length);
            }
        }
    }
}

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
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions