public void GetSubstringByKey_InputStringIsNotEmpty_KeyIsNotEmptyAndIsFound_SubstringOptionsIsNotDefined_ReturnOriginalSubstring()
        {
            // Arrange
            string           expectedResult  = "Alan follows Martin";
            string           key             = "follows";
            SubstringOptions optionIsInvalid = (SubstringOptions)2;
            // Act
            string actualResult = expectedResult.GetSubstringByKey(key, optionIsInvalid);

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="str"></param>
    /// <param name="key"></param>
    /// <param name="option"></param>
    /// <returns>Original string if it does not contain the key, otherwise a substring depending on the option specified.</returns>
    public static string GetSubstringByKey(this string str, string key, SubstringOptions option)
    {
      if (string.IsNullOrEmpty(str) == true || string.IsNullOrEmpty(key) == true || str.Contains(key) == false)
      {
        return str;
      }

      str = str.Trim();
      key = key.Trim();
      int keyIndex = str.IndexOf(key);

      switch (option)
      {
        case SubstringOptions.BeforeKey:
          return str.Substring(0, keyIndex);
        case SubstringOptions.AfterKey:            
         return str.Substring(keyIndex + key.Length);          
        default:
          //Log this???
          return str;
      }      
    }
        /// <summary>
        ///
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <param name="option"></param>
        /// <returns>Original string if it does not contain the key, otherwise a substring depending on the option specified.</returns>
        public static string GetSubstringByKey(this string str, string key, SubstringOptions option)
        {
            if (string.IsNullOrEmpty(str) == true || string.IsNullOrEmpty(key) == true || str.Contains(key) == false)
            {
                return(str);
            }

            str = str.Trim();
            key = key.Trim();
            int keyIndex = str.IndexOf(key);

            switch (option)
            {
            case SubstringOptions.BeforeKey:
                return(str.Substring(0, keyIndex));

            case SubstringOptions.AfterKey:
                return(str.Substring(keyIndex + key.Length));

            default:
                //Log this???
                return(str);
            }
        }
Esempio n. 4
0
        internal static string SubstringBetween(string source, string leftDelimiter, string rightDelimiter, int startingIndex, SubstringOptions options)
        {
            bool flag = (options & SubstringOptions.Backward) == SubstringOptions.Backward;
            int  num  = (leftDelimiter != null) ? (flag ? source.LastIndexOf(leftDelimiter, startingIndex) : source.IndexOf(leftDelimiter, startingIndex)) : 0;

            if (num == -1)
            {
                if ((options & SubstringOptions.IgnoreMissingLeftDelimiter) == SubstringOptions.None)
                {
                    return(null);
                }
                num = 0;
            }
            else if (leftDelimiter != null)
            {
                num += leftDelimiter.Length;
            }
            if (num < source.Length)
            {
                int num2 = (rightDelimiter != null) ? (flag ? source.LastIndexOf(rightDelimiter, num) : source.IndexOf(rightDelimiter, num)) : source.Length;
                if (num2 == -1)
                {
                    if ((options & SubstringOptions.IgnoreMissingRightDelimiter) == SubstringOptions.None)
                    {
                        return(null);
                    }
                    num2 = source.Length;
                }
                return(source.Substring(num, num2 - num));
            }
            if (rightDelimiter != null && (options & SubstringOptions.IgnoreMissingRightDelimiter) == SubstringOptions.None)
            {
                return(null);
            }
            return(string.Empty);
        }
Esempio n. 5
0
 internal static string SubstringBetween(string source, string leftDelimiter, string rightDelimiter, SubstringOptions options)
 {
     return(Util.SubstringBetween(source, leftDelimiter, rightDelimiter, ((options & SubstringOptions.Backward) == SubstringOptions.Backward) ? Math.Max(0, source.Length - 1) : 0, options));
 }