andrew4582

ah_StringExtentions.snippet

Mar 5th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.26 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.     <CodeSnippet Format="1.0.0">
  4.         <Header>
  5.             <Title>AHString Extentions</Title>
  6.             <Shortcut>ahstring_Extentions</Shortcut>
  7.             <Description>Code snippet created on: 7/9/2009 1:44:00 PM</Description>
  8.             <Author>Andrew Hodgson</Author>
  9.             <SnippetTypes>
  10.                 <SnippetType>Expansion</SnippetType>
  11.             </SnippetTypes>
  12.         </Header>
  13.         <Snippet>
  14.             <Declarations>
  15.                 <Literal>
  16.                     <ID>type</ID>
  17.                     <ToolTip>Property type</ToolTip>
  18.                     <Default>string</Default>
  19.                 </Literal>
  20.                 <Literal>
  21.                     <ID>property</ID>
  22.                     <ToolTip>Property name</ToolTip>
  23.                     <Default>MyProperty</Default>
  24.                 </Literal>
  25.             </Declarations>
  26.             <Code Language="csharp">
  27.                 <![CDATA[
  28.     #region String Extensions class
  29.    
  30.     [System.Diagnostics.DebuggerStepThrough]
  31.    public static class StringExtensions {
  32.  
  33.        public static string ToString(this string s,params object[] args) {
  34.            return string.Format(s,args);
  35.        }
  36.        
  37.        public static string FormatWith(this string s,params object[] args) {
  38.            return string.Format(s,args);
  39.            
  40.        }
  41.  
  42.        public static bool IsNull(this string s) {
  43.  
  44.            return (s == null
  45.                || s.Length == 0
  46.                || string.IsNullOrWhiteSpace(s));
  47.        }
  48.        
  49.         public static string ValueIfNull(this string s, string value) {
  50.            if(IsNull(s))
  51.                return value;
  52.            else
  53.                return s;
  54.        }
  55.        
  56.         public static string ToStringEmpty(this object obj) {
  57.            return obj == null ? string.Empty : obj.ToString();
  58.        }
  59.        
  60.        public static string ToStringEmpty(this object obj,string valueIfNull) {
  61.            return obj == null ? valueIfNull : obj.ToString();
  62.        }
  63.        public static string ToTitleString(this string s) {
  64.            if(s == null)
  65.                return null;
  66.            string result = "";
  67.            for(int i = 0;i < s.Length;i++) {
  68.                char c = s[i];
  69.                if(char.IsUpper(c)) {
  70.                    if(i != 0) {
  71.                        int h = i - 1;
  72.                        if(h > -1) {
  73.                            char p = s[h];
  74.                            if(char.IsUpper(p)) {
  75.                                result += c.ToString();
  76.                                continue;
  77.                            }
  78.                        }
  79.                        result += " ";
  80.                    }
  81.                }
  82.                result += c.ToString();
  83.            }
  84.            return result;
  85.        }
  86.        
  87.        public static string Repeat(this string s,int num) {
  88.            return Repeat(s,num,string.Empty);
  89.        }
  90.        
  91.        public static string Repeat(this string s,int num,string delimeter) {
  92.            if(IsNull(s))
  93.                return s;
  94.            StringBuilder sb = new StringBuilder();
  95.            for(int i = 0;i < num;i++) {
  96.                sb.Append(s);
  97.                if(delimeter != null)
  98.                    sb.Append(delimeter);
  99.            }
  100.            return sb.ToString();
  101.        }
  102.    }
  103.    
  104.     #endregion
  105.     $end$]]>
  106.             </Code>
  107.         </Snippet>
  108.     </CodeSnippet>
  109. </CodeSnippets>
Advertisement
Add Comment
Please, Sign In to add comment