andrew4582

StringHelpers - Clifton

Jul 6th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. /*
  2. Copyright (c) 2005, Marc Clifton
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7.  
  8. * Redistributions of source code must retain the above copyright notice, this list
  9.   of conditions and the following disclaimer.
  10.  
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12.   list of conditions and the following disclaimer in the documentation and/or other
  13.   materials provided with the distribution.
  14.  
  15. * Neither the name of MyXaml nor the names of its contributors may be
  16.   used to endorse or promote products derived from this software without specific
  17.   prior written permission.
  18.  
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  23. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30. */
  31.  
  32. using System;
  33.  
  34. namespace Clifton.Tools.Strings
  35. {
  36.     /// <summary>
  37.     /// Helpers for string manipulation.
  38.     /// </summary>
  39.     public class StringHelpers
  40.     {
  41.         /// <summary>
  42.         /// Left of the first occurance of c
  43.         /// </summary>
  44.         /// <param name="src">The source string.</param>
  45.         /// <param name="c">Return everything to the left of this character.</param>
  46.         /// <returns>String to the left of c, or the entire string.</returns>
  47.         public static string LeftOf(string src, char c)
  48.         {
  49.             string ret=src;
  50.             int idx=src.IndexOf(c);
  51.             if (idx != -1)
  52.             {
  53.                 ret=src.Substring(0, idx);
  54.             }
  55.             return ret;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Left of the n'th occurance of c.
  60.         /// </summary>
  61.         /// <param name="src">The source string.</param>
  62.         /// <param name="c">Return everything to the left n'th occurance of this character.</param>
  63.         /// <param name="n">The occurance.</param>
  64.         /// <returns>String to the left of c, or the entire string if not found or n is 0.</returns>
  65.         public static string LeftOf(string src, char c, int n)
  66.         {
  67.             string ret=src;
  68.             int idx=-1;
  69.             while (n > 0)
  70.             {
  71.                 idx=src.IndexOf(c, idx+1);
  72.                 if (idx==-1)
  73.                 {
  74.                     break;
  75.                 }
  76.                 --n;
  77.             }
  78.             if (idx != -1)
  79.             {
  80.                 ret=src.Substring(0, idx);
  81.             }
  82.             return ret;
  83.         }
  84.  
  85.         /// <summary>
  86.         /// Right of the first occurance of c
  87.         /// </summary>
  88.         /// <param name="src">The source string.</param>
  89.         /// <param name="c">The search char.</param>
  90.         /// <returns>Returns everything to the right of c, or an empty string if c is not found.</returns>
  91.         public static string RightOf(string src, char c)
  92.         {
  93.             string ret=String.Empty;
  94.             int idx=src.IndexOf(c);
  95.             if (idx != -1)
  96.             {
  97.                 ret=src.Substring(idx+1);
  98.             }
  99.             return ret;
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Right of the n'th occurance of c
  104.         /// </summary>
  105.         /// <param name="src">The source string.</param>
  106.         /// <param name="c">The search char.</param>
  107.         /// <param name="n">The occurance.</param>
  108.         /// <returns>Returns everything to the right of c, or an empty string if c is not found.</returns>
  109.         public static string RightOf(string src, char c, int n)
  110.         {
  111.             string ret=String.Empty;
  112.             int idx=-1;
  113.             while (n > 0)
  114.             {
  115.                 idx=src.IndexOf(c, idx+1);
  116.                 if (idx==-1)
  117.                 {
  118.                     break;
  119.                 }
  120.                 --n;
  121.             }
  122.  
  123.             if (idx != -1)
  124.             {
  125.                 ret=src.Substring(idx+1);
  126.             }
  127.  
  128.             return ret;
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Returns everything to the left of the righmost char c.
  133.         /// </summary>
  134.         /// <param name="src">The source string.</param>
  135.         /// <param name="c">The search char.</param>
  136.         /// <returns>Everything to the left of the rightmost char c, or the entire string.</returns>
  137.         public static string LeftOfRightmostOf(string src, char c)
  138.         {
  139.             string ret=src;
  140.             int idx=src.LastIndexOf(c);
  141.             if (idx != -1)
  142.             {
  143.                 ret=src.Substring(0, idx);
  144.             }
  145.             return ret;
  146.         }
  147.  
  148.         /// <summary>
  149.         /// Returns everything to the right of the rightmost char c.
  150.         /// </summary>
  151.         /// <param name="src">The source string.</param>
  152.         /// <param name="c">The seach char.</param>
  153.         /// <returns>Returns everything to the right of the rightmost search char, or an empty string.</returns>
  154.         public static string RightOfRightmostOf(string src, char c)
  155.         {
  156.             string ret=String.Empty;
  157.             int idx=src.LastIndexOf(c);
  158.             if (idx != -1)
  159.             {
  160.                 ret=src.Substring(idx+1);
  161.             }
  162.             return ret;
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Returns everything between the start and end chars, exclusive.
  167.         /// </summary>
  168.         /// <param name="src">The source string.</param>
  169.         /// <param name="start">The first char to find.</param>
  170.         /// <param name="end">The end char to find.</param>
  171.         /// <returns>The string between the start and stop chars, or an empty string if not found.</returns>
  172.         public static string Between(string src, char start, char end)
  173.         {
  174.             string ret=String.Empty;
  175.             int idxStart=src.IndexOf(start);
  176.             if (idxStart != -1)
  177.             {
  178.                 ++idxStart;
  179.                 int idxEnd=src.IndexOf(end, idxStart);
  180.                 if (idxEnd != -1)
  181.                 {
  182.                     ret=src.Substring(idxStart, idxEnd-idxStart);
  183.                 }
  184.             }
  185.             return ret;
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Returns the number of occurances of "find".
  190.         /// </summary>
  191.         /// <param name="src">The source string.</param>
  192.         /// <param name="find">The search char.</param>
  193.         /// <returns>The # of times the char occurs in the search string.</returns>
  194.         public static int Count(string src, char find)
  195.         {
  196.             int ret=0;
  197.             foreach(char s in src)
  198.             {
  199.                 if (s==find)
  200.                 {
  201.                     ++ret;
  202.                 }
  203.             }
  204.             return ret;
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Returns the rightmost char in src.
  209.         /// </summary>
  210.         /// <param name="src">The source string.</param>
  211.         /// <returns>The rightmost char, or '\0' if the source has zero length.</returns>
  212.         public static char Rightmost(string src)
  213.         {
  214.             char c='\0';
  215.             if (src.Length>0)
  216.             {
  217.                 c=src[src.Length-1];
  218.             }
  219.             return c;
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment