Introduction to DotNet Library - Viyi.Strings

Viyi.Strings Is a. NET library that supports. Net Standard 2.0 and. Net 5. As can be seen from the name, the main purpose of this library is to solve some operations on strings and text. In general, the current version of Viyi.Strings provides the following functions:

  • Text based encoding / decoding supports Base64 and Hex (hexadecimal) encoding / decoding, but more than that;
  • Provide extension methods to quickly process empty strings and blank strings;
  • Provides a string case conversion framework, and provides camelCase, PascalCase, kebab case and snake by default_ Case conversion method;
  • 2 ~ 36 hexadecimal conversion between integer and string, even if you only want to use hexadecimal, it will be very convenient;
  • Expand the parsing of Boolean values, allow on/off and yes/no, and allow flexible custom conversion;
  • If you have other requirements, you are welcome to put forward and discuss them in Issue

How to install?

The Viyi.Strings library has been published to NuGet and can be quickly installed from NuGet.

Text based encoding / decoding framework

The Viyi.Strings.Codec namespace of the Viyi.String library provides a text-based encoding / decoding framework. This namespace not only provides common encoding / decoding methods such as Base64 and Hex, but also provides a unified encoding / decoding interface design to facilitate the unified replacement of encoding / decoding methods in some scenarios. In addition, easy-to-use extension methods are provided under this namespace.

Fast Base64/Hex codec

The Viyi.Stings.Codec.Base64 namespace provides a series of extension methods of EncodeBase64() and DeocdeBase64() for byte [], which can quickly perform codec conversion between byte [] and Base64 strings:

// Randomly generate 20 bytes of data
byte[] data = new byte[20];
Random r = new Random();

string base64;
Console.WriteLine("Without parameters:");
Console.WriteLine(base64 = data.EncodeBase64());
// Output: RdFVrmsY45kkFKkUULem1LfCP6Y=

Compare(data, base64.DecodeBase64());  // perfect match

Console.WriteLine("The specified row width is 16:");
Console.WriteLine(base64 = data.EncodeBase64(16));
// Output: RdFVrmsY45kkFKkU
// Output: ULem1LfCP6Y=

Compare(data, base64.DecodeBase64());  // perfect match

Even if Base64 lacks the last = sign, it can be decoded successfully, because the = sign originally represents empty data.

Console.WriteLine(base64 = data.EncodeBase64());
Compare(data, base64.DecodeBase64());  // perfect match

// According to the above example, the last character of base64 is the = sign, which is removed before decoding
Compare(data, base64.Substring(0, base64.Length - 1).DecodeBase64()); // perfect match

The above demonstrates that Base64 can quickly encode / decode through the extension method. Encoding and decoding by Hex (hexadecimal) is similar, except that you need to use the Viyi.Strings.Codec.Hex namespace.

Complete Base64/Hex codec

The extension method only provides fast encoding / decoding methods for byte [] and string types respectively. If you need more complex encoding / decoding, such as Stream encoding / decoding, you can use Encoder and Decoder. Through the Viyi.String.Codec.TextCodec class, you can get the preset Base64Codec and HexCodec objects. Both of them implement the ITextCodec interface, which can

  • Use CreateEncoder() to create a corresponding encoder object for encoding byte data
  • Use CreateDecoder() to create a corresponding decoder object for decoding string data

For example, you want to convert the data in the binary file data.bin into hexadecimal and output it to data_ In hexdump.txt, you can use the following code:

var hexEncoder = TextCodec.Hex.CreateEncoder(
    // The option property is read-only and needs to be built using the builder mode
        // Configure line feed: 16 bytes per line, that is, 32 hexadecimal characters
        .SetLineWidth(16 * 2)

using Stream inStream = File.OpenRead("data.bin");
using StreamWriter writer = new StreamWriter("data_hexdump.txt");
hexEncoder.Encode(writer, inStream);

If you don't want to write the encoding result to a file, but save it in a string, use StringWriter as the output, or use another overload of Encode:

string hex = hexEncoder.Encode(inStream);

For more information on encoding / decoding, you can read: Text encoding and decoding (Viyi.Strings.Codec))

Naming style (case) conversion

In the environment of multi terminal, multi language and multi technology joint implementation of application software / services, it is necessary and commonly used to change the naming style in order to keep all parts comply with their own syntax and data specifications.

Viyi.Strings provides naming style conversion in the Viyi.Strings.CaseConverters namespace, and provides camelCase, PascalCase, kebab case and snake by default_ Case conversion tool. These tool classes implement the ICaseConverter interface to facilitate quick selection or replacement when necessary.

At the same time, the Viyi.Strings namespace also provides processing calls to the extension methods of strings. For example:

Console.WriteLine("Hello James Fan".KebabCase());
// Output: Hello James fan

For more information, read: Named style conversion CaseConvert

Empty string and empty string

This part of the function mainly extends the ability of string to handle empty string "" and blank string (string containing only blank characters).

For example, we know that if a string is null, it will become the default "default_value", which can be written as follows:

s = s ?? "default_value";
// Or s= "default_value";

However, if you want to replace a string when it is judged to be empty, you need to write this without using the viyi.struts extension:

// If it is a null or empty string, replace with the default content
s = string.IsNullOrEmpty(s) ? "default_value" : s;

// If only the blank string is replaced with the default content, null remains unchanged
s = string.IsNullOrWhiteSpace(s) ? "" : s;

But it's much easier to use extensions:

// If it is a null or empty string, replace with the default content
s = s.EmptyAs("default_value");

// If only the blank string is replaced with the default content, null and '' remain unchanged
s = s?.SpacesAs("");

There is no need to use the ternary operator, which is more concise in complex expressions.

In addition, IsEmpty(this string, true) and IsSpaces(this string, true) are provided to accurately judge that the string is only an empty string and an empty string (null is included).

For more details, please read: Empty string and empty string

Parse Boolean

The default Boolean parsing of. Net can only process "true" and "false", but in actual use, such as on/off and yes/no can also be converted to Boolean data. Viyi.Strings provides multiple ToBoolean() overloads to implement various requirements, such as

// If s is true, return true; S is false and returns false; Otherwise, null is returned.

// Matching with the first parameter returns true; false is returned if it matches the second parameter; Otherwise, null is returned.
s.ToBoolean("yes", "no");

// Return true in the first array; Return false in the second array; null is returned in other cases.
s.ToBoolean(new String[] { "yes", "on" }, new String[] { "no", "off" });

// If it matches any parameter after the first parameter, it returns the Boolean value represented by the first parameter, otherwise it returns the opposite value
s.ToBoolean(false, "no", "off", "false", "down");

Read more: Resolve to boolean type (bool))

Binary conversion of integers

Viyi.Strings provides 2-32 binary conversion tool functions in the Viyi.Strings.Extensions namespace, which can perform binary conversion between strings and int, uint, long and ulong. such as

"3ae".ToInt32(16); // 942
"3ae".ToInt32(16).ToString(8); // 1656
(36 + 35).ToString(36); // 1z

Please read: Binary conversion of integers


That's all for the introduction of Viyi.Strings. Interested friends are welcome to participate in this open source tool library!

The source code is hosted on Gitee: Border town / Viyi.String

Tags: .NET string base64 hex

Posted on Mon, 22 Nov 2021 09:53:15 -0500 by