Zhomfr.Helpers.ScalarTypes
0.7.1
dotnet add package Zhomfr.Helpers.ScalarTypes --version 0.7.1
NuGet\Install-Package Zhomfr.Helpers.ScalarTypes -Version 0.7.1
<PackageReference Include="Zhomfr.Helpers.ScalarTypes" Version="0.7.1" />
<PackageVersion Include="Zhomfr.Helpers.ScalarTypes" Version="0.7.1" />
<PackageReference Include="Zhomfr.Helpers.ScalarTypes" />
paket add Zhomfr.Helpers.ScalarTypes --version 0.7.1
#r "nuget: Zhomfr.Helpers.ScalarTypes, 0.7.1"
#:package Zhomfr.Helpers.ScalarTypes@0.7.1
#addin nuget:?package=Zhomfr.Helpers.ScalarTypes&version=0.7.1
#tool nuget:?package=Zhomfr.Helpers.ScalarTypes&version=0.7.1
Helpers for Scalar Types
⚠️ Warning: Active Development (< 1.0.0)
Prior to the release of stable version
1.0.0, this library follows SemVer for initial development. This means breaking changes may occur even in minor releases (0.x).We strongly recommend pinning the package version in your
.csprojto avoid unexpected build errors.
A collection of convenient and productive extension methods for some types in C# (string, decimal, IEnumerable),
designed to simplify everyday data processing and formatting tasks.
📦 Installation
Install package:
dotnet add package Zhomfr.Helpers.ScalarTypes
After add namespace in using section:
using Zhomfr.Helpers.ScalarTypes;
using Zhomfr.Helpers.ScalarTypes.Strings;
Available Methods
String Methods
<table> <tr valign="top"> <td>
- After
- AfterLast
- Before
- BeforeLast
- Between
- BetweenFirst
- BetweenLast
- CharAt
- ChopEnd
- ChopStart
- Deduplicate
- EndWith
- Excerpt
- FromBase64
</td> <td>
- Initials
- LcFirst
- Limit
- Mask
- PadBoth
- Remove
- RemoveIgnoreCase
- Replace
- ReplaceFirst
- ReplaceLast
- ReplaceMatches
- Reverse
- Squish
- StartWith
</td> <td>
- SubstringCount
- SubstringReplace
- Swap
- ToBase64
- ToCamelCase
- ToKebabCase
- ToPascalCase
- ToSnakeCase
- UcFirst
- UcSplit
- UcWord
- WordCount
</td> </tr> </table>
Decimal Methods
<table> <tr valign="top"> <td>
</td> </tr> </table>
Enumerable Methods
<table> <tr valign="top"> <td>
</td> <td>
</td> <td>
</td> </tr> </table>
🛠️ Features and Examples
After
Returns everything after the given value in a string, or the entire string if the value is not found.
string value = "one/two/three";
string result = value.After("/"); // two/three
AfterLast
Returns everything after the last occurrence of the given value in a string, or the entire string if the value is not found.
string value = "one/two/three";
string result = value.AfterLast("/"); // three
Before
Returns everything before the given value in a string.
string value = "one/two/three";
string result = value.Before("/"); // one
BeforeLast
Returns everything before the last occurrence of the given value in a string.
string value = "one/two/three";
string result = value.BeforeLast("/"); // one/two
Between
Returns the portion of a string between two specified values.
string value = "[hello] [world]";
string result = value.Between("[", "]"); // hello] [world
BetweenFirst
Returns the smallest possible portion of a string between two specified values.
string value = "[hello] [world]";
string result = value.BetweenFirst("[", "]"); // hello
BetweenLast
Returns the last smallest possible portion of a string between two specified values.
string value = "[hello] [world]";
string result = value.BetweenLast("[", "]"); // world
CharAt
Returns the character at the specified index, or null if the index is out of bounds.
string value = "hello";
char? result = value.CharAt(1); // e
ChopEnd
Removes the first occurrence of any of the specified values from the end of the string.
string value = "example.com/";
string result1 = value.ChopEnd("/"); // example.com
string result2 = value.ChopEnd("/", "."); // example.com
ChopStart
Removes the first occurrence of any of the specified values from the start of the string.
string value = "https://example.com";
string result1 = value.ChopStart("https://"); // example.com
string result2 = value.ChopStart("https://", "http://"); // example.com
CountIsBetween
Determines whether the number of elements in the collection is within the specified range (inclusive).
public static class EnumerableStub
{
public static IEnumerable GetValue()
{
yield return 1;
yield return 2;
yield return 3;
}
}
EnumerableStub.GetValue().CountIsBetween(1,3) // true
EnumerableStub.GetValue().CountIsGreatThan(3,5) // true
EnumerableStub.GetValue().CountIsGreatThan(4,6) // false
EnumerableStub.GetValue().CountIsGreatThan(1,2) // false
CountIsGreatThan
Determines whether the number of elements in the collection is greater than the specified minimum value.
public static class EnumerableStub
{
public static IEnumerable GetValue()
{
yield return 1;
yield return 2;
yield return 3;
}
}
EnumerableStub.GetValue().CountIsGreatThan(2) // true
EnumerableStub.GetValue().CountIsGreatThan(3) // false
EnumerableStub.GetValue().CountIsGreatThan(4) // false
CountIsGreatThanOrEqual
Determines whether the number of elements in the collection is greater than or equal to the specified minimum value.
public static class EnumerableStub
{
public static IEnumerable GetValue()
{
yield return 1;
yield return 2;
yield return 3;
}
}
EnumerableStub.GetValue().CountIsGreatThanOrEqual(2) // true
EnumerableStub.GetValue().CountIsGreatThanOrEqual(3) // true
EnumerableStub.GetValue().CountIsGreatThanOrEqual(4) // false
CountIsLessThan
Determines whether the number of elements in the collection is less than the specified maximum value.
public static class EnumerableStub
{
public static IEnumerable GetValue()
{
yield return 1;
yield return 2;
yield return 3;
}
}
EnumerableStub.GetValue().CountIsLessThan(2) // false
EnumerableStub.GetValue().CountIsLessThan(3) // false
EnumerableStub.GetValue().CountIsLessThan(4) // true
CountIsLessThanOrEqual
Determines whether the number of elements in the collection is less than or equal to the specified maximum value.
public static class EnumerableStub
{
public static IEnumerable GetValue()
{
yield return 1;
yield return 2;
yield return 3;
}
}
EnumerableStub.GetValue().CountIsLessThanOrEqual(2) // false
EnumerableStub.GetValue().CountIsLessThanOrEqual(3) // true
EnumerableStub.GetValue().CountIsLessThanOrEqual(4) // true
Deduplicate
Replaces consecutive instances of the specified substring with a single instance in the string. By default, deduplicates spaces.
string value = "hello world";
string result = value.Deduplicate(); // hello world
EndWith
Ends the string with a single instance of the specified value if it does not already end with it.
string value = "example";
string result = value.EndWith(".com"); // example.com
Excerpt
Extracts an excerpt from the string that matches the first instance of the specified phrase.
string value = "The quick brown fox jumps over the lazy dog";
string result = value.Excerpt("fox", radius: 5); // ...own fox jump...
FromBase64
Decodes the given Base64 string, or returns null if the string is not a valid Base64 encoding.
string value = "SGVsbG8=";
string result = value.FromBase64(); // Hello
GetDecimal
Gets the number of digits after the decimal separator.
decimal value = 123.45m;
int result = value.GetDigits(); // 5
GetDigits
Gets the total number of digits before and after the decimal separator.
decimal value = 123.45m;
int result = value.GetDigits(); // 5
GetDigitsBeforeSeparator
Gets the number of digits in the integer part of the decimal value.
decimal value = 123.45m;
int result = value.GetDigitsBeforeSeparator(); // 3
Initials
Returns the initials of the string, optionally capitalizing them.
string value = "John Ronald Reuel Tolkien";
string result = value.Initials(); // JRRT
LcFirst
Returns the string with its first character converted to lowercase.
string value = "HELLO";
string result = value.LcFirst(); // hELLO
Limit
Truncates the string to the specified length, optionally appending a custom string to the end of the truncated result. Can preserve complete words at the nearest word boundary when specified.
string value = "The quick brown fox";
string result = value.Limit(12); // The quick ...
Mask
Masks a portion of a string with a repeated character, which can be used to obfuscate segments of sensitive strings such as email addresses and phone numbers.
string value = "john@example.com";
string result = value.Mask(4, 4); // john*********e.com
PadBoth
Pads both sides of a string with another string until the final string reaches the specified length.
string value = "title";
string result = value.PadBoth(9, "-"); // --title--
Remove
Removes the specified values from the string in a case-sensitive manner.
string value = "a-b-c";
string result = value.Remove("-"); // abc
RemoveIgnoreCase
Removes the specified values from the string in a case-insensitive manner.
string value = "Hello HELLO World";
string result = value.RemoveIgnoreCase("hello"); // World
Replace
Replaces a given value in the string sequentially using an array of replacement values.
string value = "a-b-c";
string result = value.Replace("-", ["1", "2"]); // a1b2c
ReplaceFirst
Replaces the first occurrence of a specified string with another string, with an option to control case sensitivity.
string value = "foo foo";
string result = value.ReplaceFirst("foo", "bar"); // bar foo
ReplaceLast
Replaces the last occurrence of a specified string with another string, with an option to control case sensitivity.
string value = "foo foo";
string result = value.ReplaceLast("foo", "bar"); // foo bar
ReplaceMatches
Replaces all portions of a string matching a regular expression pattern with the specified replacement string.
string value = "foo123 bar456";
string result = value.ReplaceMatches(@"\d+", "#"); // foo# bar#
Reverse
Reverses the given string.
string value = "hello";
string result = value.Reverse(); // olleh
Squish
Removes all extraneous white space from the string, including extra white space between words.
string value = " hello world ";
string result = value.Squish(); // hello world
StartWith
Begins the string with a single instance of the specified value if it does not already start with it.
string value = "example.com";
string result = value.StartWith("https://"); // https://example.com
SubstringCount
Returns the number of occurrences of a given value in the string.
string value = "one two one";
int result = value.SubstringCount("one"); // 2
SubstringReplace
Replaces text within a portion of a string, starting at the specified position and replacing the number of characters specified by the length. Passing 0 for the length inserts the string without replacing any existing characters.
string value = "hello world";
string result = value.SubstringReplace("C#", 6, 5); // hello C#
Swap
Replaces multiple values in the string.
string value = "hello world";
string result = value.Swap(new() { ["hello"] = "hi", ["world"] = "there" }); // hi there
ToBase64
Converts the given string to its Base64-encoded representation.
string value = "Hello";
string result = value.ToBase64(); // SGVsbG8=
ToCamelCase
Converts the given string to camelCase.
string value = "hello_world";
string result = value.ToCamelCase(); // helloWorld
ToKebabCase
Converts the string to kebab-case.
string value = "hello world";
string result = value.ToKebabCase(); // hello-world
ToPascalCase
Converts the given string to PascalCase.
string value = "hello world";
string result = value.ToKebabCase(); // hello-world
ToSnakeCase
Converts the given string to snake_case.
string value = "helloWorld";
string result = value.ToSnakeCase(); // hello_world
UcFirst
Capitalizes the first character of the given string.
string value = "hello";
string result = value.UcFirst(); // Hello
UcSplit
Splits the string into a list by uppercase characters.
string value = "hello";
string result = value.UcFirst(); // Hello
UcWord
Converts the first character of each word in the given string to uppercase.
string value = "hello world";
string result = value.UcWord(); // Hello World
WordCount
Returns the number of words that the string contains.
string value = "hello world";
int result = value.WordCount(); // 2
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Zhomfr.Helpers.ScalarTypes:
| Package | Downloads |
|---|---|
|
Zhomfr.Validation.Rules
A powerful, intuitive collection of custom validation attributes built directly on top of the standard .NET System.ComponentModel.DataAnnotations infrastructure. |
GitHub repositories
This package is not used by any popular GitHub repositories.