You can truncate a string using the Truncate method:
1
"Long text to truncate".Truncate(10) => "Long text…"
By default the ‘…’ character is used to truncate strings. The advantage of using the ‘…’ character instead of “…” is that the former only takes a single character and thus allows more text to be shown before truncation. If you want, you can also provide your own truncation string:
1
"Long text to truncate".Truncate(10, "---") => "Long te---"
The default truncation strategy, Truncator.FixedLength, is to truncate the input string to a specific length, including the truncation string length. There are two more truncator strategies available: one for a fixed number of (alpha-numerical) characters and one for a fixed number of words. To use a specific truncator when truncating, the two Truncate methods shown in the previous examples all have an overload that allow you to specify the ITruncator instance to use for the truncation. Here are examples on how to use the three provided truncators:
1 2 3 4 5 6 7 8
"Long text to truncate".Truncate(10, Truncator.FixedLength) => "Long text…" "Long text to truncate".Truncate(10, "---", Truncator.FixedLength) => "Long te---"
"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters) => "Long t…" "Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters) => "Lon---"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords) => "Long text…" "Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords) => "Long text---"
Note that you can also use create your own truncator by implementing the ITruncator interface.
There is also an option to choose whether to truncate the string from the beginning (TruncateFrom.Left) or the end (TruncateFrom.Right). Default is the right as shown in the examples above. The examples below show how to truncate from the beginning of the string:
1 2 3 4 5 6 7 8
"Long text to truncate".Truncate(10, Truncator.FixedLength, TruncateFrom.Left) => "… truncate" "Long text to truncate".Truncate(10, "---", Truncator.FixedLength, TruncateFrom.Left) => "---runcate"
"Long text to truncate".Truncate(10, Truncator.FixedNumberOfCharacters, TruncateFrom.Left) => "…o truncate" "Long text to truncate".Truncate(16, "---", Truncator.FixedNumberOfCharacters, TruncateFrom.Left) => "---ext to truncate"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords, TruncateFrom.Left) => "…to truncate" "Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords, TruncateFrom.Left) => "---to truncate"
Digits and Char
1 2 3
"5".ConvertToPersianDigit(); // result: ۵ "y".ConvertToPersianChar(); // result: غ "۵".ConvertToEnglishDigit(); // result 5
ChangeEncoding
Change the Encoding of a string
1 2
string test = "Hello"; var text = test.ChangeEncoding(Encoding.ASCII, Encoding.UTF8);
GetBytes
Get bytes from string
1 2
string test = "Hello"; var text = test.GetBytes();
ToFileInfo
Get string file path as FileInfo
1
var fileInfo = @"D:\Documents\image.jpg".ToFileInfo();
ToDirectoryInfo
Get string directory path as DirectoryInfo
1
var directoryInfo = @"D:\Documents".ToDirectoryInfo();
IsNullOrEmpty
Check if string is null or empty
1
var isNUll = "hello".IsNullOrEmpty();
ToColor
Creates a Color from a XAML color string.
1
var color = "#3260a8".ToColor();
DateTime
Name
Type
Remark
ToShamsiDate
DateTime
Convert GregorianDate to ShamsiDate
ToShortShamsiDate
DateTime
Convert GregorianDate to ShamsiDate (Short Format)
ToLongShamsiDate
DateTime
Convert GregorianDate to ShamsiDate (Long Format)
GetDiffrenceToNow
DateTime
Get Diffrence between DateTime and Current DateTime