The TextHelper
class provides utility methods for common text manipulation tasks such as cleaning, substringing, replacing characters, and extracting keywords.
This library is designed to simplify text processing in .NET applications.
Include the X.Extensions.Text
namespace in your project to use the TextHelper
class.
using X.Extensions.Text;
Retrieves a substring starting from the beginning of the given text, limited to the specified length.
string text = "This is a long text.";
string result = TextHelper.Substring(text, 10);
Console.WriteLine(result); // Output: "This is a "
Retrieves a substring with the option to append a custom ending (e.g., "...") if the text is truncated.
string text = "This is a long text.";
string result = TextHelper.Substring(text, 10, "...");
Console.WriteLine(result); // Output: "This is..."
Cleans system characters and replaces them with hyphens. Ensures no double spaces and trims the result.
string text = "Hello, world! This is a test.";
string result = TextHelper.CleanCharacters(text);
Console.WriteLine(result); // Output: "hello-world-this-is-a-test"
Replaces all occurrences of the specified target strings in the input text with the replacement string.
string text = "Hello, world!";
string result = TextHelper.Replace(text, new[] { "Hello", "world" }, "Hi");
Console.WriteLine(result); // Output: "Hi, Hi!"
Converts HTML to plain text by removing tags and special symbols.
string html = "<p>This is <b>HTML</b>.</p>";
string result = TextHelper.ToPlainText(html);
Console.WriteLine(result); // Output: "This is HTML."
Converts HTML to plain text, preserving line breaks if specified.
string html = "<p>This is a line.<br/>Another line.</p>";
string result = TextHelper.ToPlainText(html, true);
Console.WriteLine(result); // Output: "This is a line.<br />Another line."
Extracts the top unique keywords from the text, ordered by frequency.
string text = "This is a sample text. Sample text is important.";
string keywords = TextHelper.GetKeywords(text, 3);
Console.WriteLine(keywords); // Output: "sample, text, important"
Cuts the text to the specified length, attempting to preserve logical blocks by stopping at the nearest sentence-ending dot.
string text = "This is a long text. It has multiple sentences.";
string result = TextHelper.CutText(text, 20);
Console.WriteLine(result); // Output: "This is a long text."
Removes line break placeholders from the beginning of the text.
string text = "[[LINE_BREAK]][[LINE_BREAK]]Hello!";
string result = TextHelper.TrimLineBreaksFromStart(text);
Console.WriteLine(result); // Output: "Hello!"
Contributions to the X.Extensions.Text
library are welcome. Please ensure to follow the contributing guidelines specified in the repository for submitting issues, feature requests, or pull requests.
The X.Extensions.Text
library is released under MIT license.