Other Tools
Other text tools!
In addition to the text manipulation tools that we've uncovered, we have several other tools that you can use easily in your programs.
Regular expression tools
All regular expression tools can be found in the RegexTools class that you can find within the Textify.Tools namespace. However, they are not extensions to strings.
IsValidRegex()
public static bool IsValidRegex(string pattern) { }
public static bool IsValidRegex(Regex pattern) { }This function lets you determine whether the specified regular expression pattern contains a valid syntax. This is useful for easy verification of the regex pattern syntax to ensure that you can control the situation where the user code tried to provide invalid regular expression pattern. The Regex class by default doesn't validate the syntax until you attempt to match a string with it, so we've made a wrapper to simplify things by testing a specified pattern against an empty string.
^[\w.-]+$returnstrue^[\w.-"]+$returnsfalseEmpty regexes also return
true
ParseRegex() and TryParseRegex()
public static Regex ParseRegex(string pattern) { }
public static bool TryParseRegex(string pattern, out Regex? result) { }These two functions allow you to parse a regular expression, returning an instance of a Regex class once it's been validated using the above function, IsValidRegex().
ParseRegex()throws an exception when an invalid regular expression syntax is detected.TryParseRegex()returns a boolean indicating that either the operation succeeded (true) or failed (false). When the operation fails, the resulting value isnull.
IsMatch(), Match(), and Matches()
public static bool IsMatch(string text, string pattern) { }
public static Match Match(string text, string pattern) { }
public static MatchCollection Matches(string text, string pattern) { }These three functions allow you to check to see if the regular expression engine finds matches in the provided regular expression pattern tested against the text.
IsMatch()returnstrueif there is a match; otherwise,false.Match()returns the first match is there is a match.Matches()returns all matches that are found.
Filter()
public static string Filter(string text, string pattern) { }
public static string Filter(string text, string pattern, string replaceWith) { }The above functions allow you to replace all matches in the text with either the replacement string or the empty string.
The first function overload replaces all matches with an empty string
The second function overload replaces all matches with the provided replacement string
Split()
The above function allows you to split the text using the regular expression pattern matches as the delimiter.
JSON tools
All JSON tools can be found in the JsonTools class that you can find within the Textify.Tools namespace. However, they are not extensions to strings. They are wrapped against Newtonsoft.Json.
BeautifyJson() and MinifyJson()
Using these two functions, you can perform the following operations on a JSON file:
BeautifyJson()returns a string that consists of a beautified version of your JSON file that you've passed as an argument.MinifyJson()returns a string that consists of a minified version of your JSON file that you've passed as an argument.
BeautifyJsonText() and MinifyJsonText()
Using these two functions, you can perform the following operations on a JSON representation:
BeautifyJsonText()returns a string that consists of a beautified version of your JSON text that you've passed as an argument.MinifyJsonText()returns a string that consists of a minified version of your JSON text that you've passed as an argument.
FindDifferences()
This function returns a JSON object that consists of collected differences between two different JSON tokens. The below rules are defined:
If the JSON objects are the same, it returns an empty JSON object.
Otherwise, it returns one of the following:
A list of additions, deletions, or modifications if the target object type is an object.
An array of additions and deletions if the target object type is an array.
An array of the whole object difference if the target object type is something else.
Difference between two objects
To clarify this, here's an example of a difference between two objects:
A difference JSON object in this state can be described like this:
+<PropertyName>: Indicates an additionA JSON object with a single property,
+, with the value that holds the name of the property.
-<PropertyName>: Indicates a removalA JSON object with a single property,
-, with the value that holds the name of the property.
*<PropertyName>: Indicates a modificationA JSON object with a single property,
*, that has a JSON object that has the following two properties:source: Old value of the propertytarget: New value of the property
Difference between two arrays
Another example of a difference between two arrays:
A difference JSON object in this state is an object that contains two properties:
+: Indicates additions-: Indicates removals
Difference between two values
Another example of a difference between two values "test" and "test2":
A difference JSON object in this state is an object that contains two properties:
+: Indicates a value of the target-: Indicates a value of the source
Last updated