コード例 #1
0
        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);
        }
コード例 #2
0
    /// <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;
      }      
    }
コード例 #3
0
        /// <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);
            }
        }
コード例 #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);
        }
コード例 #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));
 }