Table of Contents

DougWare.StringExtensionMethods

Source on Github

DougWare.StringExtensionMethods is a set of C# extension methods for the string class, providing functionality to extract substrings and lists of substrings between specified delimiters. These methods are particularly useful for text parsing tasks such as extracting content from HTML fragments or other structured text.

Installation

Install the NuGet package using the following command:

dotnet add package DougWare.StringExtensionMethods

Usage

Methods

GetInnerText

Retrieves the first substring between a specified preamble and postscript.

Signature:

public static string GetInnerText(this string input, string preamble, string postscript)

Parameters:

  • input (string): The input string.
  • preamble (string): The start substring.
  • postscript (string): The end substring.

Returns:

  • (string): The substring between the preamble and postscript, or an empty string if they are not found.

Example:

string input = "preambleHello, World!postscript";
string result = input.GetInnerText("preamble", "postscript");
// result: "Hello, World!"

GetInnerText with Case Sensitivity

Retrieves the first substring between a specified preamble and postscript with an option for case sensitivity.

Signature:

public static string GetInnerText(this string input, string preamble, string postscript, bool ignoreCase)

Parameters:

  • input (string): The input string.
  • preamble (string): The start substring.
  • postscript (string): The end substring.
  • ignoreCase (bool): Boolean flag to indicate if the search should be case insensitive.

Returns:

  • (string): The substring between the preamble and postscript, or an empty string if they are not found.

Example:

string input = "PREAMBLEHello, World!POSTSCRIPT";
string result = input.GetInnerText("preamble", "postscript", true);
// result: "Hello, World!"

ToByteArrayUtf8

Converts a string into a byte array using UTF-8 encoding.

Signature:

public static byte[] ToByteArrayUtf8(this string input)

Parameters:

  • input (string): The input string.

Returns:

  • (byte[]): The byte array representation of the input string.

Example:

string input = "Hello, World!";
byte[] result = input.ToByteArrayUtf8();
// result: [byte array of the string]

GetInnerTextList

Retrieves a list of substrings between specified preambles and postscripts.

Signature:

public static List<string> GetInnerTextList(this string input, string preamble, string postscript)

Parameters:

  • input (string): The input string.
  • preamble (string): The start substring.
  • postscript (string): The end substring.

Returns:

  • (List): A list of substrings between the preamble and postscript.

Example:

string input = "preambleHello, World!postscript preambleHello Again!postscript";
List<string> result = input.GetInnerTextList("preamble", "postscript");
// result: ["Hello, World!", "Hello Again!"]

GetInnerTextList with Case Sensitivity

Retrieves a list of substrings between specified preambles and postscripts with an option for case sensitivity.

Signature:

public static List<string> GetInnerTextList(this string input, string preamble, string postscript, bool ignoreCase)

Parameters:

  • input (string): The input string.
  • preamble (string): The start substring.
  • postscript (string): The end substring.
  • ignoreCase (bool): Boolean flag to indicate if the search should be case insensitive.

Returns:

  • (List): A list of substrings between the preamble and postscript.

Example:

string input = "PREAMBLEHello, World!POSTSCRIPT PREAMBLEHello Again!POSTSCRIPT";
List<string> result = input.GetInnerTextList("preamble", "postscript", true);
// result: ["Hello, World!", "Hello Again!"]

Unit Tests

GetInnerText

[TestMethod]
public void GetInnerText_ReturnsCorrectInnerText()
{
    var input = "preambleHello, World!postscript";
    var result = input.GetInnerText("preamble", "postscript");
    Assert.AreEqual("Hello, World!", result);
}

GetInnerTextList

[TestMethod]
public void GetInnerTextList_ReturnsCorrectList()
{
    var input = "preambleHello, World!postscript preambleHello Again!postscript";
    var result = input.GetInnerTextList("preamble", "postscript");
    var expected = new List<string> { "Hello, World!", "Hello Again!" };
    CollectionAssert.AreEqual(expected, result);
}

ToByteArrayUtf8

[TestMethod]
public void ToByteArrayUtf8_ReturnsCorrectByteArray()
{
    var input = "Hello, World!";
    var result = input.ToByteArrayUtf8();
    var expected = Encoding.UTF8.GetBytes(input);
    CollectionAssert.AreEqual(expected, result);
}

For more examples and detailed documentation, please refer to the source code and unit tests provided.

License

This project is licensed under the MIT License. See the LICENSE file for more details.


Feel free to contribute by submitting issues or pull requests to the GitHub repository. Happy coding!