Click here to Skip to main content
12,628,768 members (35,396 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

40K views
1.4K downloads
71 bookmarked
Posted

Project Tool

, 23 Sep 2007 CPOL
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
ProjectTool.exe
bin
Release
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
CodeLib
Controls
ProjectTool
Items
Project
Propertys
Solution
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
ProjectTool.exe
Checksums
Compress
Deflate
Common
Decode
Encode
Formats
Zip
HuffmanCoding
LempelZivWelch
LempelZiv
Resources
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
ProjectTool.exe
cs.png
default.ico
dir.png
diro.png
dll.png
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
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)

Share

About the Author

Liu Junfeng
Software Developer (Senior) Beyondsoft SH
China China
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 23 Sep 2007
Article Copyright 2006 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid