將對像數組轉換為僅包含指定columns的逗號分隔值(CSV)字符串。
// TODO // TODO
↑回到頂部
採用任何數量的具有length屬性的峰值對象,並返回最長的對象。如果多個對象具有相同的長度,則將返回第一個對象。返回-1如果沒有提供參數。
// TODO // TODO
↑回到頂部
從提供的數組返回n最大元素。如果n大於或等於提供的數組的長度,則返回原始數組(按降序排序)。
// TODO // TODO
↑回到頂部
從提供的數組返回n最低元素。如果n大於或等於提供的數組的長度,則返回原始數組(按升序排序)。
// TODO // TODO
↑回到頂部
如果提供的謂詞函數返回集合中所有元素的false ,則返回true ,否則為false 。
namespace JonasSchubert . Snippets . Enumerable
{
public static partial class Enumerable
{
public static bool None < T > ( this IEnumerable < T > enumerable , Func < T , bool > predicate )
{
try
{
return enumerable . First ( predicate ) == null ;
}
catch ( Exception )
{
return true ;
}
}
}
} new List < int > { 3 , 2 , 0 } . None ( x => x == 1 ) ; # true
new string [ ] { "Hello" , "World" } . None ( x => x . Length == 6 ) # true
new bool [ ] { true , false } . None ( x => ! x ) ; # false
↑回到頂部
返回數組的第n個元素。
// TODO // TODO
↑回到頂部
將指定量的元素移至數組末端。
// TODO // TODO
↑回到頂部
分類數組的集合。
// TODO // TODO
↑回到頂部
將元素分為兩個數組,這取決於每個元素提供的函數的真實性。
// TODO // TODO
↑回到頂部
生成數組元素的所有排列(包含重複)。
// TODO // TODO
↑回到頂部
檢索給定鍵的所有值。
// TODO // TODO
↑回到頂部
突變原始數組以濾除指定的值。
// TODO // TODO
↑回到頂部
突變原始數組以濾除指定索引處的值。
// TODO // TODO
↑回到頂部
突變原始數組以濾除指定的值。返回刪除的元素。
// TODO // TODO
↑回到頂部
根據給定的迭代函數,將原始數組突變以濾除指定的值。
// TODO // TODO
↑回到頂部
根據條件過濾一個對像數組,同時還過濾未指定的密鑰。
// TODO // TODO
↑回到頂部
對累加器和數組中的每個元素應用功能(從左到右),返回一系列依次降低的值。
// TODO // TODO
↑回到頂部
將提供的功能應用於設置比較規則後,返回數組的最小值/最大值。
// TODO // TODO
↑回到頂部
採用謂詞和數組,例如Array.prototype.filter() ,但僅保留x如果pred(x) === false 。
// TODO // TODO
↑回到頂部
從給定函數返回false數組中刪除元素。
// TODO // TODO
↑回到頂部
從數組返回一個隨機元素。
// TODO // TODO
↑回到頂部
從array到array的大小,在唯一的鍵處獲取n隨機元素。
// TODO // TODO
↑回到頂部
此方法通過刪除現有元素和/或添加新元素來更改數組的內容。類似於JavaScript版本Array.prototype.splice()
// TODO // TODO
↑回到頂部
隨機返回新數組的數組值的順序。
// TODO // TODO
↑回到頂部
返回兩個數組中出現的一系列元素。
// TODO // TODO
↑回到頂部
返回Direction.Ascending如果以升序順序排序枚舉, Direction.Descending ,如果以降序或Direction.NotSorted進行排序。
namespace JonasSchubert . Snippets . Enumerable
{
public static partial class Enumerable
{
public static Direction SortedDirection < T > ( this IEnumerable < T > enumerable )
{
if ( enumerable == null )
{
throw new ArgumentNullException ( nameof ( enumerable ) ) ;
}
if ( enumerable . Count ( ) <= 1 )
{
return Direction . NotSorted ;
}
var direction = enumerable . GetDirection ( 0 , 1 ) ;
if ( enumerable . Count ( ) > 2 )
{
for ( var index = 2 ; index < enumerable . Count ( ) ; index ++ )
{
var currentDirection = enumerable . GetDirection ( index - 1 , index ) ;
direction = direction == Direction . NotSorted ? currentDirection : direction ;
if ( direction != currentDirection )
{
return Direction . NotSorted ;
}
}
}
return direction ;
}
private static Direction GetDirection < T > ( this IEnumerable < T > enumerable , int indexStart , int indexEnd )
{
var compareResult = Comparer < T > . Default . Compare ( enumerable . ElementAt ( indexStart ) , enumerable . ElementAt ( indexEnd ) ) ;
return compareResult < 0 ? Direction . Ascending : compareResult > 0 ? Direction . Descending : Direction . NotSorted ;
}
}
}使用Enum Jonasschubert.Snippets.Snippets.Enumerable.Direction。
public enum Direction
{
NotSorted ,
Ascending ,
Descending
} new List < uint > { 1 , 2 , 3 , 4 , 5 } . SortedDirection ( ) ; # Direction . Ascending
new string [ ] { "C" , "B" , "A" } . SortedDirection ( ) ; # Direction . Descending
new List < TestStruct > ( ) { new TestStruct { Byte = 0 } , new TestStruct { Byte = 1 } , new TestStruct { Byte = 0 } } . SortedDirection ( ) ; # Direction . NotSorted
↑回到頂部
返回應將值插入數組的最低索引以維護其排序順序。
// TODO // TODO
↑回到頂部
返回應根據提供的迭代函數將值插入數組中的最低索引,以維持其排序順序。
// TODO // TODO
↑回到頂部
返回最高的索引,應將值插入數組以維護其排序順序。
// TODO // TODO
↑回到頂部
返回最高的索引,應根據提供的迭代器函數將值插入數組中以維持其排序順序。
// TODO // TODO
↑回到頂部
執行數組的穩定排序,並在其值相同時保留項目的初始索引。不會突變原始數組,而是返回一個新數組。
// TODO // TODO
↑回到頂部
返回兩個陣列之間的對稱差,而無需過濾重複值。
// TODO // TODO
↑回到頂部
在將提供的函數應用於兩個陣列元素之後,返回兩個數組之間的對稱差。
// TODO // TODO
↑回到頂部
使用提供的函數作為比較器,返回兩個數組之間的對稱差。
// TODO // TODO
↑回到頂部
返回數組中的所有元素,除了第一個元素。
// TODO // TODO
↑回到頂部
從一開始就返回帶有n個元素的數組。
// TODO // TODO
↑回到頂部
從末端返回帶有n個元素的數組。
// TODO // TODO
↑回到頂部
從數組的末尾刪除元素,直到傳遞功能返回true 。返回刪除的元素。
// TODO // TODO
↑回到頂部
刪除數組中的元素,直到傳遞的功能返回true 。返回刪除的元素。
// TODO // TODO
↑回到頂部
將2D枚舉轉換為逗號分隔值(CSV)字符串。
namespace JonasSchubert . Snippets . Enumerable
{
public static partial class Enumerable
{
public static string ToCsv < T > ( this IEnumerable < IEnumerable < T > > enumerable , string delimiter = "," )
{
if ( enumerable == null )
{
throw new ArgumentNullException ( nameof ( enumerable ) ) ;
}
return string . Join ( " n " , enumerable . Select ( subEnumerable => string . Join ( delimiter , subEnumerable . Select ( value => typeof ( T ) . IsNumericType ( ) ? value . ToString ( ) . Replace ( "," , "." ) : value . ToString ( ) ) ) ) ) ;
}
}
} new List < List < bool > > { new List < bool > { true , true } , new List < bool > { true , false } } . ToCsv ( ) ; # "True,True n True,False"
new double [ ] [ ] { new double [ ] { 1.1 , 2.2 , 3.3 } , new double [ ] { 4.4 , 5.5 , 6.6 } } . ToCsv ( ) # "1.1,2.2,3.3 n 4.4,5.5,6.6"
new List < List < TestStruct >> { new List < TestStruct > { new TestStruct { Byte = 0 } } , new List < TestStruct > { new TestStruct { Byte = 1 } , new TestStruct { Byte = 2 } } } . ToCsv ( "-" ) # "Byte: 0 n Byte: 1-Byte: 2"
↑回到頂部
將給定陣列的樣子縮小到值哈希(鍵入數據存儲)中。
// TODO // TODO
↑回到頂部
返回兩個數組中任何一個中存在的每個元素。
// TODO // TODO
↑回到頂部
將提供的函數應用於兩個數組元素後,返回兩個數組中任何一個中存在的每個元素。
// TODO // TODO
↑回到頂部
使用提供的比較器函數,返回兩個陣列中任何一個中存在的每個元素。
// TODO // TODO
↑回到頂部
返回數組的所有唯一值。
// TODO // TODO
↑回到頂部
基於提供的比較器函數返回數組的所有唯一值。
// TODO // TODO
↑回到頂部
基於提供的比較器函數返回數組的所有唯一值。
// TODO // TODO
↑回到頂部
返回兩個陣列之間的唯一對稱差,不包含兩個數組中的重複值。
// TODO // TODO
↑回到頂部
濾除具有指定值之一的數組的元素。
// TODO // TODO
↑回到頂部
通過從陣列中創建每個可能的對來創建兩個提供的新數組。
// TODO // TODO
↑回到頂部
檢查兩個數字是否彼此相等。
// TODO // TODO
↑回到頂部
返回兩個或多個數字的平均值。
該方法異常數字作為參數,並因此返回平均值。
LINQ文檔在這裡。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static double Average ( this uint [ ] elements )
{
if ( elements . Length == 0 ) return 0 ;
return elements . Aggregate ( 0.0 , ( current , element ) => current + element ) / elements . Length ;
}
}
} { 4 , 5 , 9 , 1 , 0 } . Average ( ) # 3.8
↑回到頂部
在使用提供的函數將每個元素映射到值之後,返回數組的平均值。
// TODO // TODO
↑回到頂部
評估兩個整數n和k的二項式係數。
// TODO // TODO
↑回到頂部
將一個角度從學位轉換為弧度。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static double DegToRad ( this decimal degree ) => ( double ) degree * System . Math . PI / 180.0 ;
public static double DegToRad ( this double degree ) => degree * System . Math . PI / 180.0 ;
public static double DegToRad ( this float degree ) => degree * System . Math . PI / 180.0 ;
public static double DegToRad ( this int degree ) => degree * System . Math . PI / 180.0 ;
public static double DegToRad ( this uint degree ) => degree * System . Math . PI / 180.0 ;
}
} 270.0 . DegToRad ( ) ; # ~ 4.71
- 90u . DegToRad ( ) ; # ~ 1.57
720 . DegToRad ( ) ; # ~ 12.57
↑回到頂部
將數字轉換為數字數組。
// TODO // TODO
↑回到頂部
返回兩個點之間的距離。
// TODO // TODO
↑回到頂部
計算數字的階乘。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static uint Factorial ( uint number )
{
var result = 1u ;
for ( var index = number ; index > 0 ; index -- )
{
result *= index ;
}
return result ;
}
}
} Math . Factorial ( 0 ) ; # 1
Math . Factorial ( 3 ) ; # 6
Math . Factorial ( 6 ) ; # 720
↑回到頂部
生成一個包含斐波那契序列的列表,直到第n項。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static List < int > Fibonaci ( int length )
{
var list = new List < int > ( ) ;
for ( var index = 0 ; index < length ; index ++ )
{
list . Add ( index <= 1 ? index : list [ index - 1 ] + list [ index - 2 ] ) ;
}
return list ;
}
}
} Math . Fibonaci ( 2 ) ; # n ew List < int > ( ) { 0 , 1 }
Math . Fibonaci ( 7 ) ; # n ew List < int > ( ) { 0 , 1 , 1 , 2 , 3 , 5 , 8 }
↑回到頂部
計算兩個或多個數字/數組之間的最大常見分隔線。
// TODO // TODO
↑回到頂部
初始化一個包含指定範圍內數字的數組,其中start和end是包容性的,並且兩個術語之間的比率是step 。如果step等於1 ,則返回錯誤。
// TODO // TODO
↑回到頂部
檢查給定的數字是否在給定範圍內。
// TODO // TODO
↑回到頂部
檢查A號是否可除其他數字排除。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static bool IsDivisibleBy ( this decimal value , decimal divider ) => divider == 0 ? throw new DivideByZeroException ( ) : value % divider == 0 ;
public static bool IsDivisibleBy ( this double value , double divider ) => divider == 0 ? throw new DivideByZeroException ( ) : value % divider == 0 ;
public static bool IsDivisibleBy ( this float value , float divider ) => divider == 0 ? throw new DivideByZeroException ( ) : value % divider == 0 ;
public static bool IsDivisibleBy ( this int value , int divider ) => divider == 0 ? throw new DivideByZeroException ( ) : value % divider == 0 ;
public static bool IsDivisibleBy ( this uint value , uint divider ) => divider == 0 ? throw new DivideByZeroException ( ) : value % divider == 0 ;
}
} 1 . IsDivisibleBy ( 2 ) ; # true
- 2.0 . IsDivisibleBy ( 2.0 ) ; # true
1.0f . IsDivisibleBy ( 2.0f ) ; # false
2u . IsDivisibleBy ( 2u ) ; # true
↑回到頂部
如果給定的數字為偶數,則返回true ,否則為false 。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static bool IsEven ( this decimal value ) => value % 2 == 0 ;
public static bool IsEven ( this double value ) => value % 2 == 0 ;
public static bool IsEven ( this float value ) => value % 2 == 0 ;
public static bool IsEven ( this int value ) => value % 2 == 0 ;
public static bool IsEven ( this uint value ) => value % 2 == 0 ;
}
} 0 . IsEven ( ) ; # true
1u . IsEven ( ) ; # false
- 2.0 . IsEven ( ) ; # true
↑回到頂部
檢查提供的整數是否是質數。
// TODO // TODO
↑回到頂部
如果給定的數字奇怪,則返回為true ,否則為false 。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static bool IsOdd ( this decimal value ) => value % 2 == 1 ;
public static bool IsOdd ( this double value ) => value % 2 == 1 ;
public static bool IsOdd ( this float value ) => value % 2 == 1 ;
public static bool IsOdd ( this int value ) => value % 2 == 1 ;
public static bool IsOdd ( this uint value ) => value % 2 == 1 ;
}
} 0 . IsOdd ( ) ; # false
1u . IsOdd ( ) ; # true
- 2.0 . IsOdd ( ) ; # false
↑回到頂部
返回兩個或多個數字中最不常見的倍數。
// TODO // TODO
↑回到頂部
用於驗證各種標識號的Luhn算法的實施,例如信用卡號,IMEI編號,國家提供商標識符編號等。
// TODO // TODO
↑回到頂部
從提供的枚舉中返回最大值。
// TODO // TODO
↑回到頂部
返回數字數組的中位數。
// TODO // TODO
↑回到頂部
從提供的枚舉中返回最小值。
// TODO // TODO
↑回到頂部
使用Eratosthenes的篩子生成最高給定數字的素數。
// TODO // TODO
↑回到頂部
將一個角度從弧度轉換為程度。
namespace JonasSchubert . Snippets . Math
{
public static partial class Math
{
public static double RadToDeg ( this decimal radians ) => ( double ) radians * 180.0 / System . Math . PI ;
public static double RadToDeg ( this double radians ) => radians * 180.0 / System . Math . PI ;
public static double RadToDeg ( this float radians ) => radians * 180.0 / System . Math . PI ;
public static double RadToDeg ( this int radians ) => radians * 180.0 / System . Math . PI ;
public static double RadToDeg ( this uint radians ) => radians * 180.0 / System . Math . PI ;
}
} ( System . Math . PI / 2 ) . RadToDeg ( ) # 90
( System . Math . PI * - 2 ) . RadToDeg ( ) # - 360
↑回到頂部
返回指定範圍內n個隨機整數的數組。
// TODO // TODO
↑回到頂部
返回指定範圍內的隨機整數。
// TODO // TODO
↑回到頂部
返回指定範圍內的隨機數。
// TODO // TODO
↑回到頂部
將一個數字回合到指定數量的數字。
// TODO // TODO
↑回到頂部
將輸入字符串放入整數中。
// TODO // TODO
↑回到頂部
返回數字數組的標準偏差。
// TODO // TODO
↑回到頂部
返回兩個或多個數字/數組的總和。
// TODO // TODO
↑回到頂部
在使用提供的函數將每個元素映射到值之後,返回數組的總和。
// TODO // TODO
↑回到頂部
返回每秒執行功能的次數。 hz是hertz的單位,頻率單位定義為每秒一個週期。
namespace JonasSchubert . Snippets . Method
{
public static partial class Method
{
public static long Hz (
Action action ,
uint iterations = 100000 )
{
var watch = Stopwatch . StartNew ( ) ;
for ( var iteration = 0 ; iteration < iterations ; iteration ++ )
{
action . Invoke ( ) ;
}
watch . Stop ( ) ;
return watch . ElapsedMilliseconds > 0
? ( iterations * 1000 ) / watch . ElapsedMilliseconds
: long . MaxValue ;
}
.. .
}
} # w ill return time depending on your PC power
int randomInt ( ) => new Random ( ) . Next ( 0 , 1000000 ) ;
Method . Hz ( randomInt ) ;
char [ ] charArrayFunc ( string test ) => test . ToCharArray ( ) . Select ( x => ( char ) ( x * 2 ) ) . Where ( x => x > 0 ) . ToArray ( ) ;
Method . Hz ( charArrayFunc ) ;
↑回到頂部
通過回調n次迭代
namespace JonasSchubert . Snippets . Method
{
public static partial class Method
{
public static IList < T1 > Times < T1 > ( Func < T1 > func , uint times )
{
var list = new List < T1 > ( ) ;
for ( var index = 0 ; index < times ; index ++ )
{
list . Add ( func ( ) ) ;
}
return list ;
}
}
} Method . Times ( ( ( ) => true ) , 3 ) # list of size 3 , all values true
Method . Times ( ( ( int start , int end ) => new Random ( ) . Next ( start , end ) ) , 6 , 0 , 100 ) # list of size 6 with 6 random integers between 0 and 100
↑回到頂部
返回字節中的字符串長度。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static int ByteSize ( this string input ) => System . Text . Encoding . Default . GetByteCount ( input ) ;
public static int ByteSizeAscii ( this string input ) => System . Text . Encoding . ASCII . GetByteCount ( input ) ;
public static int ByteSizeBigEndianUnicode ( this string input ) => System . Text . Encoding . BigEndianUnicode . GetByteCount ( input ) ;
public static int ByteSizeUnicode ( this string input ) => System . Text . Encoding . Unicode . GetByteCount ( input ) ;
public static int ByteSizeUtf7 ( this string input ) => System . Text . Encoding . UTF7 . GetByteCount ( input ) ;
public static int ByteSizeUtf8 ( this string input ) => System . Text . Encoding . UTF8 . GetByteCount ( input ) ;
public static int ByteSizeUtf32 ( this string input ) => System . Text . Encoding . UTF32 . GetByteCount ( input ) ;
}
} // TODO
↑回到頂部
返回提供的字符串中的元音數。
"" . ByteSize ( ) ; # 0
"Hello World" . ByteSize ( ) ; # 11
"Hello World" . ByteSizeUnicode ( ) ; # 22
"Hello World" . ByteSizeUtf32 ( ) ; # 44
"This is 30 seconds of C." . ByteSizeBigEndianUnicode ( ) ; # 48 // TODO
↑回到頂部
將逗號分隔值(CSV)字符串轉換為2D數組。
// TODO // TODO
↑回到頂部
將逗號分隔值(CSV)字符串轉換為對象的2D數組。字符串的第一行用作標題行。
// TODO // TODO
↑回到頂部
檢查字符串是否使用以下方面的給定子字符串結束。
該方法除了測試的字符串和要測試的子字符串。
大多數其他檢查已經集成。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool EndsWithRegex ( this string input , string substring ) => new Regex ( $ " { substring } $" ) . IsMatch ( input ) ;
}
} "Hello World" . EndsWithRegex ( @"[dolrwDOLRW]{5}$" ) # true
"Hello World, this is it" . EndsWithRegex ( @"[dolrwDOLRW]{5}$" ) # false
↑回到頂部
從駱駝轉換一個字符串。使所有單詞小寫,並使用提供的分離器將它們結合在一起(默認是一個空格)。在paramisentence == true中,句子的第一個字母將是大寫,最後將添加一個點(默認值為true)。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string FromCamelCase ( this string input , string separator = " " , bool isSentence = true )
{
var value = string
. Join ( separator , Regex . Matches ( input , @"/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g" ) )
. ToLower ( ) ;
return isSentence ? $ " { char . ToUpperInvariant ( value [ 0 ] ) } { value . Substring ( 1 ) } ." : value ;
}
}
} "someDatabaseFieldName" . FromCamelCase ( ) ; # "Some database field name."
"someLabelThatNeedsToBeCamelized" . FromCamelCase ( "-" , false ) ; # "some-label-that-needs-to-be-camelized"
"someJavascriptProperty" . FromCamelCase ( "_" , false ) ; # "some_javascript_property"
↑回到頂部
檢查字符串是否是另一個字符串(不敏感)的字符。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool IsAnagramOf ( this string input , string compare ) => input . TransformToCompare ( ) == compare . TransformToCompare ( ) ;
private static string TransformToCompare ( this string input ) => string . Join ( string . Empty , input . ToLower ( ) . OrderBy ( x => x ) ) ;
}
} "iceman" . IsAnagramOf ( "cinema" ) ; # true
"icemAn" . IsAnagramOf ( "cinema" ; # true
"icem an" . IsAnagramOf ( "cinema" ; # false
"ic.EMan" . IsAnagramOf ( "cinema" ; # false
"icman" . IsAnagramOf ( "cinema" ; # false
↑回到頂部
檢查字符串是否較低。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool IsLower ( this string input ) => input == input . ToLower ( ) ;
}
} "abc" . IsLower ( ) ; # true
"a3@$" . IsLower ( ) ; # true
"Ab4" . IsLower ( ) ; # false
↑回到頂部
如果給定的字符串是回文,則返回true ,否則為false 。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool IsPalindrome ( this string input ) => input . ToLower ( ) == string . Join ( string . Empty , input . ToCharArray ( ) . Reverse ( ) ) . ToLower ( ) ;
}
} "tacocat" . IsPalindrome ( ) ; # true
"tAcocat" . IsPalindrome ( ) ; # true
"tacoca" . IsPalindrome ( ) ; # false
↑回到頂部
檢查字符串是否為上情況。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool IsUpper ( this string input ) => input == input . ToUpper ( ) ;
}
} "ABC" . IsUpper ( ) ; # true
"A3@$" . IsUpper ( ) ; # true
"aB4" . IsUpper ( ) ; # false
↑回到頂部
用指定的mask字符替換字符的最後一個長度以外的所有length 。省略第二個參數, length ,以保持4字符的默認值。如果length為負,則未掩蓋的字符將在字符串的開始。省略第三個參數, mask ,以使用'*'的默認字符。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string Mask ( this string input , int length = 4 , char mask = '*' ) =>
length >= input . Length
? new string ( mask , input . Length )
: length >= 0
? input . Remove ( 0 , input . Length - length ) . Insert ( 0 , new string ( mask , input . Length - length ) )
: - length >= input . Length
? input
: input . Remove ( - length , input . Length + length ) . Insert ( - length , new string ( mask , input . Length + length ) ) ;
}
} "1234567890" . Mask ( ) ; # "******7890"
"1234567890" . Mask ( 3 ) ; # "*******890"
"1234567890" . Mask ( 0 ) ; # "**********"
"1234567890" . Mask ( - 4 ) ; # "1234******"
"1234567890" . Mask ( 20 , '-' ) ; # "----------"
"1234567890" . Mask ( - 20 , '-' ) ; # "1234567890"
↑回到頂部
如果比指定的長度短,則用指定的字符在兩側填充字符串。使用PadLeft()和PadRight()將給定字符串的兩側墊板。省略第三個參數char ,將witepsace字符用作默認填充字符。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string Pad ( this string input , int length , char pad = ' ' ) => input . PadLeft ( ( input . Length + length ) / 2 , pad ) . PadRight ( length , pad ) ;
}
} "Hello World." . Pad ( 20 ) ; # " Hello World. "
"Hello World." . Pad ( 5 , '-' ) ; # "Hello World."
"Dog" . Pad ( 8 , ' ' ) ; # " Dog "
"42" . Pad ( 6 , '0' ) ; # "004200"
↑回到頂部
刪除不可打印的ASCII字符。使用正則表達式刪除不可打印的ASCII字符。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string RemoveNonAscii ( this string input ) => Regex . Replace ( input , "[^ x20 - x7E ]" , "" ) ;
}
} "äÄçÇéÉêlorem ipsumöÖÐþúÚ" . RemoveNonAscii ( ) ; # "lorem ipsum"
↑回到頂部
逆轉字符串。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string Reverse ( this string input ) => string . Join ( string . Empty , input . ToCharArray ( ) . Reverse ( ) ) ;
}
} "My name is Jonas Schubert" . Reverse ( ) ; # "trebuhcS sanoJ si eman yM"
"!This is, maybe not, but important..." . Reverse ( ) ; # "...tnatropmi tub ,ton ebyam ,si sihT!"
↑回到頂部
將多行字符串拆分為一條線數。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string [ ] SplitLines ( this string input ) => Regex . Split ( input , " r ? n " ) ;
}
} "This n is a n multiline n string. n " . SplitLines ( ) ; # n ew string [ ] { "This" , "is a" , "multiline" , "string." , "" }
↑回到頂部
檢查字符串是否使用正則劃線以給定的子字符串開始。
該方法除了測試的字符串和要測試的子字符串。
大多數其他檢查已經集成。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static bool StartsWithRegex ( this string input , string substring ) => new Regex ( $ "^ { substring } " ) . IsMatch ( input ) ;
}
} "Hello World" . StartsWithRegex ( @"[ehloEHLO]{5}$" ) # true
"Well, hello World" . StartsWithRegex ( @"[ehloEHLO]{5}$" ) # false
↑回到頂部
從字符串中刪除HTML/XML標籤。使用正則表達式從字符串中刪除HTML/XML標籤。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string StripHtmlTags ( this string input ) => Regex . Replace ( input , "<[^>]*>" , "" ) ;
}
} "<p><em>lorem</em> <strong>ipsum</strong></p>" . StripHtmlTags ( ) ; # "lorem ipsum"
"<div><br/>Hello <br />World</div>" . StripHtmlTags ( ) ; # "Hello World"
↑回到頂部
將字符串轉換為駱駝。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string ToCamelCase ( this string input )
{
var value = string . Join ( string . Empty , Regex . Matches ( input , @"/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g" )
. Select ( x => $ " { x . Value . First ( ) . ToString ( ) . ToUpper ( ) } { x . Value . Substring ( 1 ) . ToLower ( ) } " ) ) ;
return char . ToLowerInvariant ( value [ 0 ] ) + value . Substring ( 1 ) ;
}
}
} "some_database_field_name" . ToCamelCase ( ) ; # "someDatabaseFieldName"
"Some label that needs to be camelized" . ToCamelCase ( ) ; # "someLabelThatNeedsToBeCamelized"
"some-javascript-property" . ToCamelCase ( ) ; # "someJavascriptProperty"
"some-mixed_string with spaces_underscores-and-hyphens" . ToCamelCase ( ) ; # "someMixedStringWithSpacesUnderscoresAndHyphens"
↑回到頂部
將字符串轉換為烤肉串。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string ToKebabCase ( this string input ) =>
string . Join ( "-" , Regex . Matches ( input , @"/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g" ) . Select ( x => x . Value . ToLower ( ) ) ) ;
}
} "camelCase" . ToKebabCase ( ) ; # "camel-case"
"some text" . ToKebabCase ( ) ; # "some-text"
"some-mixed_string With spaces_underscores-and-hyphens" . ToKebabCase ( ) ; # "some-mixed-string-with-spaces-underscores-and-hyphens"
"AllThe-small Things" . ToKebabCase ( ) ; # "all-the-small-things"
"IAmListeningToFmWhileLoadingDifferentUrlOnMyBrowserAndAlsoEditingXmlAndHtml" . ToKebabCase ( ) ; # "i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html"
↑回到頂部
將字符串轉換為蛇案。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string ToSnakeCase ( this string input ) =>
string . Join ( "_" , Regex . Matches ( input , @"/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g" ) . Select ( x => x . Value . ToLower ( ) ) ) ;
}
} "camelCase" . ToSnakeCase ( ) ; # "camel_case"
"some text" . ToSnakeCase ( ) ; # "some_text"
"some-mixed_string With spaces_underscores-and-hyphens" . ToSnakeCase ( ) ; # "some_mixed_string_with_spaces_underscores_and_hyphens"
"AllThe-small Things" . ToSnakeCase ( ) ; # "all_the_small_things"
"IAmListeningToFmWhileLoadingDifferentUrlOnMyBrowserAndAlsoEditingXmlAndHtml" . ToSnakeCase ( ) ; # "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_xml_and_html"
↑回到頂部
將字符串轉換為標題案例。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string ToTitleCase ( this string input ) =>
string . Join ( " " , Regex . Matches ( input , @"/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g" )
. Select ( x => $ " { x . Value . First ( ) . ToString ( ) . ToUpper ( ) } { x . Value . Substring ( 1 ) . ToLower ( ) } " ) ) ;
}
} "some_database_field_name" . ToTitleCase ( ) ; # "Some Database Field Name"
"Some label that needs to be title-cased" . ToTitleCase ( ) ; # "Some Label That Needs To Be Title Cased"
"some-package-name" . ToTitleCase ( ) ; # "Some Package Name"
"some-mixed_string with spaces_underscores-and-hyphens" . ToTitleCase ( ) ; # "Some Mixed String With Spaces Underscores And Hyphens"
↑回到頂部
將字符串截斷為指定的長度。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static string Truncate ( this string input , int maxLength ) =>
input . Length > maxLength ? $ " { input . Substring ( 0 , maxLength > 3 ? maxLength - 3 : maxLength ) } ..." : input ;
}
} "Hello World" . Truncate ( 4 ) ; # "H..."
"Hello World" . Truncate ( 12 ) ; # "Hello World"
↑回到頂部
將給定的字符串轉換為單詞列表。
namespace JonasSchubert . Snippets . String
{
public static partial class String
{
public static List < string > Words ( this string input , string pattern = @"w+[^s]*w+|w" ) =>
Regex . Matches ( input , pattern ) . Select ( x => x . Value ) . ToList ( ) ;
}
} "Hello World" . Words ( ) ; # n ew List < string > { "Hello" , "World" }
"Hello" . Words ( ) ; # n ew List < string > { "Hello" }
" " . Words ( ) ; # n ew List < string > ( )
↑回到頂部
檢查提供的類型是否為數字類型。
namespace JonasSchubert . Snippets . Type2
{
public static partial class Type2
{
public static bool IsNumericType ( this Type type )
{
switch ( Type . GetTypeCode ( type ) )
{
case TypeCode . Byte :
case TypeCode . SByte :
case TypeCode . UInt16 :
case TypeCode . UInt32 :
case TypeCode . UInt64 :
case TypeCode . Int16 :
case TypeCode . Int32 :
case TypeCode . Int64 :
case TypeCode . Decimal :
case TypeCode . Double :
case TypeCode . Single :
return true ;
default :
return false ;
}
}
}
} typeof ( sbyte ) . IsNumericType ( ) ; # true
typeof ( short ) . IsNumericType ( ) ; # true
typeof ( float ) . IsNumericType ( ) ; # true
typeof ( string ) . IsNumericType ( ) ; # false
typeof ( int [ ] ) . IsNumericType ( ) ; # false
↑回到頂部
將3位顏色代碼擴展到6位顏色代碼。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static string ExtendHex ( this string hex ) =>
$ " { string . Join ( "" , ( hex . StartsWith ( '#' ) ? hex : $ "# { hex } " ) . Select ( x => x == '#' ? $ " { x } " : $ " { x } { x } " ) ) } " ;
}
} "#03f" . ExtendHex ( ) ; # "#0033ff"
"05a" . ExtendHex ( ) ; # "#0055aa"
↑回到頂部
如果提供了alpha值,則將顏色代碼轉換為rgb()或rgba()字符串。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static string HexToRgb ( string value )
{
value = value . Replace ( "#" , "" ) ;
var hasAlpha = value . Length == 8 ;
value = value . Length == 3 ? string . Join ( "" , value . Select ( x => $ " { x } { x } " ) ) : value ;
var valueAsInt = int . Parse ( value , NumberStyles . HexNumber ) ;
var red = valueAsInt >> ( hasAlpha ? 24 : 16 ) ;
var green = ( valueAsInt & ( hasAlpha ? 0x00ff0000 : 0x00ff00 ) ) >> ( hasAlpha ? 16 : 8 ) ;
var blue = ( valueAsInt & ( hasAlpha ? 0x0000ff00 : 0x0000ff ) ) >> ( hasAlpha ? 8 : 0 ) ;
var alpha = hasAlpha ? $ " { valueAsInt & 0x000000ff } " : null ;
return $ "rgb { ( hasAlpha ? "a" : "" ) } ( { red } , { green } , { blue } { ( hasAlpha ? $ ", { alpha } " : "" ) } )" ;
}
}
} Utility . HexToRgb ( "#fff" ) ; # "rgb(255, 255, 255)"
Utility . HexToRgb ( "#27ae60" ) ; # "rgb(39, 174, 96)"
Utility . HexToRgb ( "#27ae60ff" ) ; # "rgba(39, 174, 96, 255)"
↑回到頂部
將字節中的數字轉換為人類可讀字符串。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static string PrettyBytes ( ulong bytes )
{
var units = new string [ ] { "B" , "KB" , "MB" , "GB" , "TB" , "PB" , "EB" , "ZB" , "YB" } ;
var stringArray = units
. Select ( ( unit , index ) =>
Math . Floor ( bytes / Math . Pow ( 1e3 , index ) % 1e3 ) > 0
? $ " { Math . Floor ( bytes / Math . Pow ( 1e3 , index ) % 1e3 ) } { unit } { ( Math . Floor ( bytes / Math . Pow ( 1e3 , index ) % 1e3 ) > 1 ? "s" : string . Empty ) } "
: string . Empty )
. Where ( x => ! string . IsNullOrEmpty ( x ) )
. Reverse ( )
. ToArray ( ) ;
return stringArray . Length > 0
? string . Join ( ", " , stringArray )
: "0 B" ;
}
}
} Utility . PrettyBytes ( 0ul ) ; # "0 B"
Utility . PrettyBytes ( 1001ul ) ; # "1 KB, 1 B"
Utility . PrettyBytes ( 20000000000000000ul ) ; # "20 PBs"
Utility . PrettyBytes ( 1001001001ul ) ; # "1 GB, 1 MB, 1 KB, 1 B"
↑回到頂部
生成隨機的十六進制顏色。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static string RandomHexColor ( ) =>
$ "# { ( new Random ( ) . Next ( ) * 0xFFFFFF * 1000000 ) . ToString ( "X" ) . PadLeft ( 6 , '0' ) . Substring ( 0 , 6 ) } " ;
}
} Utility . RandomHexColor ( ) ; # "#01A5FF" ( e . g . )
↑回到頂部
將RGB組件的值轉換為顏色代碼。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static string RgbToHex ( int red , int green , int blue ) =>
$ "# { ( ( red << 16 ) + ( green << 8 ) + blue ) . ToString ( "X" ) . PadLeft ( 6 , '0' ) } " ;
}
} Utility . RgbToHex ( 0 , 0 , 0 ) ; # "#000000"
Utility . RgbToHex ( 1 , 165 , 255 ) ; # "#01A5FF"
Utility . RgbToHex ( 255 , 255 , 255 ) ; # "#FFFFFF"
↑回到頂部
衡量函數執行的時間。
秒錶文檔在這裡。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static ( long , T1 ) TimeTaken < T1 > ( Func < T1 > func )
{
var watch = Stopwatch . StartNew ( ) ;
T1 result = func . Invoke ( ) ;
watch . Stop ( ) ;
return ( watch . ElapsedMilliseconds , result ) ;
}
}
} Utility . TimeTaken ( ( ) => true ) # 13.37m s , true
↑回到頂部
如果字符串為n / no則返回true如果字符串為y / yes或false 。
namespace JonasSchubert . Snippets . Utility
{
public static partial class Utility
{
public static bool YesNo ( this string test , bool defaultVal = false ) =>
new Regex ( @"^(y|yes)$" , RegexOptions . IgnoreCase ) . IsMatch ( test )
? true
: new Regex ( @"^(n|no)$" , RegexOptions . IgnoreCase ) . IsMatch ( test )
? false
: defaultVal ;
}
} var empty = "" . YesNo ( ) ; # false
var yes = "yes" . YesNo ( ) ; # true
var y = "y" . YesNo ( ) ; # true
var NO = "NO" . YesNo ( ) ; # false
var nO = "nO" . YesNo ( ) ; # false
↑回到頂部
您總是歡迎您為這個項目做出貢獻。請閱讀貢獻指南。
| 喬納斯·舒伯特 | Denis Biondic |
C#的30秒根據MIT許可分配。有關詳細信息,請參見許可證。
MIT License
Copyright (c) 2018 - 2020 JonasSchubert
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.