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 tool
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()
IsValidRegex()
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.
ParseRegex()
and TryParseRegex()
ParseRegex()
and TryParseRegex()
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
.
String syntax attribute
First introduced in .NET 7, you can use the string syntax attribute, called StringSyntaxAttribute
, to give your strings in the function context, but it's only available in the source code level. This depends on your IDE and how it supports this attribute.
Here's a simple example of how to use it:
You can use this syntax attribute in not only .NET 7.0+ apps, but you can also use it in .NET Standard and .NET Framework applications.
When trying to use this attribute in .NET 7.0+ apps while using Textify, you'll notice this error message appearing in the error list:
This is intentional, because Visual Studio only highlights the string based on context defined by this attribute when it is found in the System.Diagnostics.CodeAnalysis
namespace.
To solve this error, you must make an alias of the Textify.Offline
NuGet package by clicking on the Properties button in the right-click menu of the package and writing global, TextifyAlias
to the Aliases
property. Then, write the extern alias statement at the top of the source code file and modify the using
statement for the CodeAnalysis
namespace, such as:
After that, you should be able to use this attribute.
If everything works properly, you should be able to see that parts of your string, such as regex in the below screenshot, are highlighted in different colors, and warnings should work, too.
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()
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()
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()
FindDifferences()
This function returns a JSON object that consists of collected differences between two different JSON tokens. If the JSON objects are the same, it returns an empty JSON object. Otherwise, it returns either 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, or an array of the whole object difference if the target object type is something else. 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
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
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