XenocodeRCE

Untitled

Jan 13th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. internal static class StringFunctions
  2. {
  3.     //------------------------------------------------------------------------------------
  4.     //  This method allows replacing a single character in a string, to help convert
  5.     //  C++ code where a single character in a character array is replaced.
  6.     //------------------------------------------------------------------------------------
  7.     internal static string ChangeCharacter(string sourcestring, int charindex, char changechar)
  8.     {
  9.         return (charindex > 0 ? sourcestring.Substring(0, charindex) : "")
  10.             + changechar.ToString() + (charindex < sourcestring.Length - 1 ? sourcestring.Substring(charindex + 1) : "");
  11.     }
  12.  
  13.     //------------------------------------------------------------------------------------
  14.     //  This method simulates the classic C string function 'isxdigit' (and 'iswxdigit').
  15.     //------------------------------------------------------------------------------------
  16.     internal static bool IsXDigit(char character)
  17.     {
  18.         if (char.IsDigit(character))
  19.             return true;
  20.         else if ("ABCDEFabcdef".IndexOf(character) > -1)
  21.             return true;
  22.         else
  23.             return false;
  24.     }
  25.  
  26.     //------------------------------------------------------------------------------------
  27.     //  This method simulates the classic C string function 'strchr' (and 'wcschr').
  28.     //------------------------------------------------------------------------------------
  29.     internal static string StrChr(string stringtosearch, char chartofind)
  30.     {
  31.         int index = stringtosearch.IndexOf(chartofind);
  32.         if (index > -1)
  33.             return stringtosearch.Substring(index);
  34.         else
  35.             return null;
  36.     }
  37.  
  38.     //------------------------------------------------------------------------------------
  39.     //  This method simulates the classic C string function 'strrchr' (and 'wcsrchr').
  40.     //------------------------------------------------------------------------------------
  41.     internal static string StrRChr(string stringtosearch, char chartofind)
  42.     {
  43.         int index = stringtosearch.LastIndexOf(chartofind);
  44.         if (index > -1)
  45.             return stringtosearch.Substring(index);
  46.         else
  47.             return null;
  48.     }
  49.  
  50.     //------------------------------------------------------------------------------------
  51.     //  This method simulates the classic C string function 'strstr' (and 'wcsstr').
  52.     //------------------------------------------------------------------------------------
  53.     internal static string StrStr(string stringtosearch, string stringtofind)
  54.     {
  55.         int index = stringtosearch.IndexOf(stringtofind);
  56.         if (index > -1)
  57.             return stringtosearch.Substring(index);
  58.         else
  59.             return null;
  60.     }
  61.  
  62.     //------------------------------------------------------------------------------------
  63.     //  This method simulates the classic C string function 'strtok' (and 'wcstok').
  64.     //  Note that the .NET string 'Split' method cannot be used to simulate 'strtok' since
  65.     //  it doesn't allow changing the delimiters between each token retrieval.
  66.     //------------------------------------------------------------------------------------
  67.     private static string activestring;
  68.     private static int activeposition;
  69.     internal static string StrTok(string stringtotokenize, string delimiters)
  70.     {
  71.         if (stringtotokenize != null)
  72.         {
  73.             activestring = stringtotokenize;
  74.             activeposition = -1;
  75.         }
  76.  
  77.         //the stringtotokenize was never set:
  78.         if (activestring == null)
  79.             return null;
  80.  
  81.         //all tokens have already been extracted:
  82.         if (activeposition == activestring.Length)
  83.             return null;
  84.  
  85.         //bypass delimiters:
  86.         activeposition++;
  87.         while (activeposition < activestring.Length && delimiters.IndexOf(activestring[activeposition]) > -1)
  88.         {
  89.             activeposition++;
  90.         }
  91.  
  92.         //only delimiters were left, so return null:
  93.         if (activeposition == activestring.Length)
  94.             return null;
  95.  
  96.         //get starting position of string to return:
  97.         int startingposition = activeposition;
  98.  
  99.         //read until next delimiter:
  100.         do
  101.         {
  102.             activeposition++;
  103.         } while (activeposition < activestring.Length && delimiters.IndexOf(activestring[activeposition]) == -1);
  104.  
  105.         return activestring.Substring(startingposition, activeposition - startingposition);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment