UnitTestingSamplesCore.StringSample.GetStringDemo C# (CSharp) Method

GetStringDemo() public method

public GetStringDemo ( string first, string second ) : string
first string
second string
return string
        public string GetStringDemo(string first, string second)
        {
            if (first == null)
            {
                throw new ArgumentNullException(nameof(first));
            }
            if (string.IsNullOrEmpty(first))
            {
                throw new ArgumentException("empty string is not allowed", first);
            }
            if (second == null)
            {
                throw new ArgumentNullException(nameof(second));
            }
            if (second.Length > first.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(second),
                  "must be shorter than first");
            }

            int startIndex = first.IndexOf(second);
            if (startIndex < 0)
            {
                return $"{second} not found in {first}";
            }
            else if (startIndex < 5)
            {
                string result = first.Remove(startIndex, second.Length);
                return $"removed {second} from {first}: {result}";
            }
            else
            {
                return _init.ToUpperInvariant();
            }
        }
    }

Usage Example

 public void TestGetStringDemoExceptions()
 {
     var sample = new StringSample(string.Empty);
     Assert.Throws<ArgumentNullException>(() => sample.GetStringDemo(null, "a"));
     Assert.Throws<ArgumentNullException>(() => sample.GetStringDemo("a", null));
     Assert.Throws<ArgumentException>(() => sample.GetStringDemo(string.Empty, "a"));
 }
All Usage Examples Of UnitTestingSamplesCore.StringSample::GetStringDemo