StringHelper.cs :  » Game » SokoSolve-Sokoban » SokoSolve » Common » 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 » Game » SokoSolve Sokoban 
SokoSolve Sokoban » SokoSolve » Common » StringHelper.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

namespace SokoSolve.Common{
    /// <summary>
    /// This basic delegate allows the return of a string from an object
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="item"></param>
    /// <returns></returns>
    public delegate string ToString<T>(T item);


    /// <summary>
    /// Rudimentary string helper functions
    /// </summary>
    public static class StringHelper
    {
        
        /// <summary>
        /// Find the total number of occurances of achar in source
        /// </summary>
        public static int Count(string source, char achar)
        {
            int cc = 0;
            foreach (char c in source)
            {
                if (c == achar) cc++;
            }
            return cc;
        }

        /// <summary>
        /// Get a substring based on two index (normal is one index and one count)
        /// </summary>
        public static string SubStringOrdinal(string source, int startPos, int endPos)
        {
            if (endPos < 0) endPos = source.Length;
            if (endPos > source.Length) endPos = source.Length;
            return source.Substring(startPos, endPos - startPos);
        }

        /// <summary>
        /// Seperate a string based on a seperator
        /// </summary>
        public static string[] Split(string source, string sep)
        {
            List<string> result = new List<string>();
            int iSep;
            while ((iSep = source.IndexOf(sep)) > 0)
            {
                result.Add(source.Substring(0, iSep));
                source = source.Remove(0, iSep + sep.Length);
            }

            // Add the last line
            if (source != null && source.Length > 0) result.Add(source);

            return result.ToArray();
        }

        /// <summary>
        /// Split string at fixed intervals
        /// </summary>
        /// <param name="source"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string[] SplitOnLength(string source, int length)
        {
            if (string.IsNullOrEmpty(source)) return new string[] { source };

            List<string> result = new List<string>();

            while (source.Length > length)
            {
                result.Add(source.Substring(0, length));
                source = source.Remove(0, length);
            }

            result.Add(source);

            return result.ToArray();
        }

        /// <summary>
        /// Join a list of string using a seperator
        /// </summary>
        public static string Join(IList<string> source, string sep)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in source)
            {
                if (!object.ReferenceEquals( s,  source[0]))
                {
                    sb.Append(sep);
                }
                sb.Append(s);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Join a list of string using a seperator
        /// </summary>
        public static string Join<T>(IEnumerable<T> source, ToString<T> toStringDelegate, string sep)
        {
            if (source == null) return string.Empty;

            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (T item in source)
            {
                if (!first)
                {
                    sb.Append(sep);
                }
                if (toStringDelegate != null)
                {
                    string result = toStringDelegate(item);
                    if (result != null) sb.Append(result);
                }
                else
                {
                    sb.Append(item.ToString());
                }

                first = false;
            }
            return sb.ToString();
        }

    /// <summary>
    /// Create a string report for an Exception. Supports nesting.
    /// </summary>
    /// <param name="ex"></param>
    /// <returns></returns>
    public static string Report(Exception ex)
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Exception: {0}{1}", ex.GetType(), Environment.NewLine);
      sb.AppendFormat("Message: {0}{1}", ex.Message, Environment.NewLine);
      sb.AppendFormat("Stack:{1} {0}{1}", ex.StackTrace, Environment.NewLine);
      if (ex.InnerException != null)
      {
        sb.Append(Report(ex.InnerException).Replace(Environment.NewLine, Environment.NewLine + "\t"));
      }
      return sb.ToString();
    }

        /// <summary>
        /// Use reflection to retrieve all properties and show them as strings
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Report(object obj)
        {
            StringBuilder sb = new StringBuilder();

            Type objType = obj.GetType();
            PropertyInfo[] props = objType.GetProperties();
            foreach(PropertyInfo prop in props)
            {
                sb.Append(prop.Name);
            }

            return sb.ToString();
        }
        
        /// <summary>
        /// Remove all occurances on char from Source
        /// </summary>
        public static string RemoveCharsIn(string Source, string RemoveChars)
        {
            StringBuilder sb = new StringBuilder(Source);
            int pos = 0;
            while(pos < sb.Length)
            {
                while (pos < sb.Length && RemoveChars.IndexOf(sb[pos]) >= 0)
                {
                    sb.Remove(pos, 1);
                }
                pos++;
            }
            return sb.ToString();
        }

        /// <summary>
        /// Remove from Source all chars that are not contained in KeepChars
        /// </summary>
        public static string RemoveCharsNotIn(string Source, string KeepChars)
        {
            StringBuilder sb = new StringBuilder(Source);
            int pos = 0;
            while(pos < sb.Length)
            {
                while (pos < sb.Length && KeepChars.IndexOf(sb[pos]) < 0)
                {
                    sb.Remove(pos, 1);
                }
                pos++;
            }
            return sb.ToString();
        }

        /// <summary>
        /// Provide a clean, intelligent string representation
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToString(float value)
        {
            return value.ToString("0.00");
        }

        public static string ToString(TimeSpan value)
        {
            if (value.TotalMilliseconds  < 1000)
            {
                return value.TotalMilliseconds.ToString("0")+ " ms";
            }
            if (value.TotalSeconds < 60)
            {
                return value.TotalSeconds.ToString("0.00") + " sec";
            }
            if (value.TotalMinutes < 60)
            {
                return string.Format("{0:00}m{1:00}s",  value.Minutes, value.Seconds);
            }
            if (value.TotalHours < 60)
            {
                return string.Format("{0:00}h{1:00}m", value.Hours, value.Minutes, value.Seconds);
            }
            return value.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.