Class StringExtensionMethods
- Namespace
- DougWare.StringExtensionMethods
- Assembly
- DougWare.StringExtensionMethods.dll
This class provides extension methods for strings: GetInnerText and GetInnerTextList. These methods are useful for parsing text to extract substrings or lists of substrings between any two string delimiters. GetInnerTest returns the first substring between the preamble and postscript. GetInnerTestList returns a list of substrings between the preamble and postscript, e.g. "<p>" and "</p>" to get paragraph text as a list from an html fragment.
public static class StringExtensionMethods
- Inheritance
-
StringExtensionMethods
- Inherited Members
Methods
GetInnerText(string, string, string)
Retrieves a substring from this instance. The substring starts at a specified character position of a preamble and ends at a specified character position of a postscript.
public static string GetInnerText(this string input, string preamble, string postscript)
Parameters
inputstringThe input string
preamblestringThe start substring
postscriptstringThe end substring
Returns
- string
The substring between the preamble and postscript, or empty string if they are not found.
Examples
string input = "preambleHello, World!postscript";
string result = input.GetInnerText("preamble", "postscript"); // Returns "Hello, World!"
Exceptions
- ArgumentNullException
Thrown when input, preamble or postscript is null or empty
GetInnerText(string, string, string, bool)
Retrieves a substring from this instance with case sensitive or insensitive option. The substring starts at a specified character position of a preamble and ends at a specified character position of a postscript.
public static string GetInnerText(this string input, string preamble, string postscript, bool ignoreCase)
Parameters
inputstringThe input string
preamblestringThe start substring
postscriptstringThe end substring
ignoreCaseboolBoolean flag to indicate if the search should be case insensitive
Returns
- string
The substring between the preamble and postscript, or empty string if they are not found.
Examples
string input = "PREAMBLEHello, World!POSTSCRIPT";
string result = input.GetInnerText("preamble", "postscript", true); // Returns "Hello, World!"
Exceptions
- ArgumentNullException
Thrown when input, preamble or postscript is null or empty
GetInnerTextList(string, string, string)
Retrieves a list of substrings from this instance. The substrings start at a specified character position of a preamble and end at a specified character position of a postscript.
public static List<string> GetInnerTextList(this string input, string preamble, string postscript)
Parameters
inputstringThe input string
preamblestringThe start substring
postscriptstringThe end substring
Returns
Examples
string input = "preambleHello, World!postscript preambleHello Again!postscript";
List<string> result = input.GetInnerTextList("preamble", "postscript"); // Returns ["Hello, World!", "Hello Again!"]
Exceptions
- ArgumentNullException
Thrown when input, preamble or postscript is null or empty
GetInnerTextList(string, string, string, bool)
Retrieves a list of substrings from this instance with case sensitive or insensitive option. The substrings start at a specified character position of a preamble and end at a specified character position of a postscript.
public static List<string> GetInnerTextList(this string input, string preamble, string postscript, bool ignoreCase)
Parameters
inputstringThe input string
preamblestringThe start substring
postscriptstringThe end substring
ignoreCaseboolBoolean flag to indicate if the search should be case insensitive
Returns
Examples
string input = "PREAMBLEHello, World!POSTSCRIPT PREAMBLEHello Again!POSTSCRIPT";
List<string> result = input.GetInnerTextList("preamble", "postscript", true); // Returns ["Hello, World!", "Hello Again!"]
Exceptions
- ArgumentNullException
Thrown when input, preamble or postscript is null or empty
ToByteArrayUtf8(string)
Converts a string into a byte array using UTF-8 encoding.
public static byte[] ToByteArrayUtf8(this string input)
Parameters
inputstringThe input string
Returns
- byte[]
The byte array representation of the input string
Examples
string input = "Hello, World!";
byte[] result = input.ToByteArrayUtf8(); // Returns the byte array of the string
Exceptions
- ArgumentNullException
Thrown when input is null or empty