StringExtensions.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Library » ExtensionMethod » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Library » ExtensionMethod » StringExtensions.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Everest.Library.ExtensionMethod{
    public static class StringExtensions
    {
        #region Join
        /// <summary>
        /// Removes the last specified char.
        /// </summary>
        /// <param name="stringBuilder">The string builder.</param>
        /// <param name="c">The c.</param>
        public static StringBuilder RemoveLastSpecifiedChar(this StringBuilder stringBuilder, char c)
        {
            if (stringBuilder.Length == 0)
                return stringBuilder;
            if (stringBuilder[stringBuilder.Length - 1] == c)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder;
        }

        /// <summary>
        /// Joins the string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable">The enumerable.</param>
        /// <param name="separator">The separator.</param>
        /// <returns></returns>
        public static string JoinString<T>(this IEnumerable<T> enumerable, string separator)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var item in enumerable)
            {
                stringBuilder.AppendFormat("{0},", item.ToString());
            }
            stringBuilder.RemoveLastSpecifiedChar(',');
            return stringBuilder.ToString();
        }
        #endregion

        #region Strip Tags
        /// <summary>
        /// Html Xml         
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        public static string StripHtmlXmlTags(this string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return content;
            }
            return Regex.Replace(content, "<[^>]+>?", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringToStrip">The string to strip.</param>
        /// <returns></returns>
        public static string StripAllTags(this string stringToStrip)
        {
            if (string.IsNullOrEmpty(stringToStrip))
            {
                return stringToStrip;
            }
            // paring using RegEx
            //
            stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            stringToStrip = StripHtmlXmlTags(stringToStrip);

            return stringToStrip;
        }
        #endregion

        /// <summary>
        /// Truncates the specified s.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <param name="wordsLimit">The words limit.</param>
        /// <returns></returns>
        public static string Truncate(this string s, int wordsLimit)
        {
            if (s.Length < wordsLimit)
            {
                return s;
            }
            return s.Substring(0, wordsLimit);
        }

        #region Cast type
        /// <summary>
        /// To the int.
        /// </summary>
        /// <param name="s">The s ,for example:"1,2,3" .</param>
        /// <param name="spliter">The spliter. for example:','</param>
        /// <returns></returns>
        public static IEnumerable<int> ToInt(this string s, params char[] spliter)
        {
            string[] strArr = s.Split(spliter);
            List<int> ints = new List<int>();
            foreach (var str in strArr)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    ints.Add(int.Parse(str));
                }
            }
            return ints;
        }

        /// <summary>
        /// Gets the nullable bool.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static bool? GetNullableBool(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                return bool.Parse(value);
            }
            return null;
        }
        /// <summary>
        /// Gets the nullable int.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static int? GetNullableInt(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                return int.Parse(value);
            }
            return null;
        }

        /// <summary>
        /// Gets the nullable GUID.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static Guid? GetNullableGuid(this string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                return new Guid(value);
            }
            return null;
        }
        #endregion

        #region Trim
        /// <summary>
        /// Trims the specified s.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static string TryTrim(this string s)
        {
            if (s != null)
            {
                return s.Trim();
            }
            return s;
        }
        #endregion

        static Regex unasscii = new Regex(@"[^a-z|^_|^\d|^\u4e00-\u9fa5]+", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
        /// <summary>
        /// Replaces to valid URL.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static string ReplaceToValidUrl(this string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return unasscii.Replace(s, "-");
            }
            return s;
        }

        /// <summary>
        /// Replaces the specified s.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
        /// <returns></returns>
        public static string Replace(this string s, string oldValue, string newValue, bool ignoreCase)
        {
            if (ignoreCase)
            {
                return Microsoft.VisualBasic.Strings.Replace(s, oldValue, newValue, 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
            }
            else
            {
                return s.Replace(oldValue, newValue);
            }
        }

        /// <summary>
        /// Determines whether [is null or empty trim] [the specified s].
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns>
        ///   <c>true</c> if [is null or empty trim] [the specified s]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmptyTrim(string s)
        {
            if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Trims the specified s.
        /// </summary>
        /// <param name="s">The s.</param>
        public static string Trim(string s)
        {
            if (s == null)
            {
                return null;
            }
            return s.Trim();
        }

        /// <summary>
        /// Appends the HTML line.
        /// </summary>
        /// <param name="stringBuilder">The string builder.</param>
        /// <param name="s">The s.</param>
        public static void AppendHtmlLine(this StringBuilder stringBuilder, string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                stringBuilder.AppendFormat("{0}<br/>", s);
            }
        }


        /// <summary>
        /// Determines whether the specified s is true.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns>
        ///   <c>true</c> if the specified s is true; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsTrue(string s)
        {
            if (IsNullOrEmptyTrim(s))
            {
                return false;
            }
            s = s.ToLower();
            return s == "on" || s == "true";
        }

        private static Regex chineseRegex = new Regex("[\u4e00-\u9fa5]", RegexOptions.Compiled);
        /// <summary>
        /// Chinese2s the pin yin.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        public static string Chinese2PinYin(this string s)
        {
            StringBuilder stringBuilder = new StringBuilder(s);
            var matches = chineseRegex.Matches(s);
            foreach (Match match in matches)
            {
                stringBuilder.Replace(match.Value, ChineseConvertor.ToPinyin(match.Value));
            }

            return stringBuilder.ToString();
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.