Skip to content

Commit

Permalink
Added RemoveAt extension method to StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaoticz committed May 23, 2024
1 parent 4fee12a commit 9295b41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Kotz.Extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ Defines the following extension methods:
- ToTitleCase: Converts the current string to the *Title Case* format.
- **StringBuilder Extensions**
- ReplaceAll: Replaces all instances of a substring with another substring, even if the new substring is a substring of the old substring.
- Remove: Removes the specified string chunks from the current StringBuilder.
- Remove: Removes the specified string chunks from the current string builder.
- RemoveAt: Removes the character at the specified index.
- ToStringAndClear: Returns the `string` value of the current builder and clears the builder.
- Trim: Removes all leading and trailing instances of a character from the current string builder.
- TrimEnd: Removes all trailing instances of a character from the current string builder.
Expand Down
15 changes: 13 additions & 2 deletions Kotz.Extensions/StringBuilderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ public static StringBuilder Remove(this StringBuilder stringBuilder, IEnumerable
return stringBuilder;
}

// TODO: unit tests for the methods above
// TODO: RemoveAt(int)
/// <summary>
/// Removes the character at the specified <paramref name="index"/> from this string builder.
/// </summary>
/// <param name="stringBuilder">This string builder.</param>
/// <param name="index">The index of the character to be removed.</param>
/// <returns>A reference to this instance with the character removed.</returns>
/// <exception cref="ArgumentOutOfRangeException">Occurs when the index is less than zero or greater than the length of the string builder.</exception>
public static StringBuilder RemoveAt(this StringBuilder stringBuilder, int index)
{
return (index >= stringBuilder.Length)
? throw new ArgumentOutOfRangeException(nameof(index), index, "Index cannot be less than zero or greater than the length of the StringBuilder.")
: stringBuilder.Remove(index, 1);
}

/// <summary>
/// Converts the value of this instance to a <see langword="string"/>, then clears its buffer.
Expand Down
19 changes: 19 additions & 0 deletions Kotz.Tests/Extensions/StringBuilderExtTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal void ContainsTest(string firstChunk, string secondChunk, string toSearc
[Theory]
[InlineData("Hello World", "Hello World")]
[InlineData("Hello World", "Hello World", "")]
[InlineData("Hello World", "HelloWorld", " ")]
[InlineData("Hello World", "Hell Wrld", "o")]
[InlineData("Hello World", "Hll Wrld", "o", "e")]
[InlineData("Hello World", "Hello ", "World")]
Expand All @@ -61,6 +62,24 @@ internal void RemoveTest(string input, string expected, params string[] toRemove
internal void RemoveExceptionTest()
=> Assert.Throws<ArgumentNullException>(() => new StringBuilder().Remove(null!));

[Theory]
[InlineData("Hello", "ello", 0)]
[InlineData("Hello", "Helo", 2)]
[InlineData("Hello", "Hell", 4)]
internal void RemoveAtTest(string input, string expected, int index)
{
var stringBuilder = new StringBuilder(input)
.RemoveAt(index);

Assert.Equal(expected, stringBuilder.ToString());
}

[Theory]
[InlineData("Hello", 5)]
[InlineData("Hello", -1)]
internal void RemoveAtException(string input, int index)
=> Assert.Throws<ArgumentOutOfRangeException>(() => new StringBuilder(input).RemoveAt(index));

[Theory]
[InlineData("")]
[InlineData("hello")]
Expand Down

0 comments on commit 9295b41

Please sign in to comment.