オブジェクトの配列を、指定されたcolumnsのみを含むコンマセパートされた値(CSV)文字列に変換します。
// TODO // TODO
↑上に戻ります
lengthプロパティを持つオブジェクトまたはオブジェクトを任意の数の任意の数を採取し、最長のオブジェクトを返します。複数のオブジェクトの長さが同じ場合、最初のオブジェクトが返されます。引数が提供されていない場合、-1返品-1 。
// TODO // TODO
↑上に戻ります
提供された配列からn最大要素を返します。 nが提供された配列の長さ以上の場合は、元の配列(下降順にソートされた)を返します。
// TODO // TODO
↑上に戻ります
提供された配列からn最小要素を返します。 nが提供された配列の長さ以上の場合は、元の配列(昇順でソートされた)を返します。
// TODO // TODO
↑上に戻ります
提供された述語関数がコレクション内のすべてのfalseのfalseを返す場合、 trueを返します。
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
↑上に戻ります
要素を各要素に対する提供された関数の真実性に応じて、要素を2つの配列にグループ化します。
// TODO // TODO
↑上に戻ります
配列の要素のすべての順列を生成します(重複を含みます)。
// TODO // TODO
↑上に戻ります
特定のキーのすべての値を取得します。
// TODO // TODO
↑上に戻ります
元の配列を変異させて、指定された値を除外します。
// TODO // TODO
↑上に戻ります
元の配列を変異させて、指定されたインデックスで値を除外します。
// TODO // TODO
↑上に戻ります
元の配列を変異させて、指定された値を除外します。削除された要素を返します。
// TODO // TODO
↑上に戻ります
特定のイテレータ関数に基づいて、指定された値を除外するために元の配列を変異させます。
// TODO // TODO
↑上に戻ります
不特定のキーをフィルタリングしながら、条件に基づいてオブジェクトの配列をフィルタリングします。
// TODO // TODO
↑上に戻ります
アキュムレータとアレイ内の各要素に対して関数を適用し(左から右)、一連の連続した値を返します。
// TODO // TODO
↑上に戻ります
提供された関数を適用してルールの比較を設定した後、配列の最小値/最大値を返します。
// TODO // TODO
↑上に戻ります
Array.prototype.filter()などの述語と配列を取得しますが、 pred(x) === false場合にのみxを保持します。
// 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.NotSorted Direction.Descendingに並べられている場合。
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.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
↑上に戻ります
重複した値をフィルタリングすることなく、2つの配列間の対称差を返します。
// TODO // TODO
↑上に戻ります
提供された関数を両方の各配列要素に適用した後、2つの配列間の対称差を返します。
// TODO // TODO
↑上に戻ります
提供された関数をコンパレータとして使用して、2つの配列間の対称差を返します。
// TODO // TODO
↑上に戻ります
最初の要素を除くすべての要素を配列内のすべての要素を返します。
// TODO // TODO
↑上に戻ります
最初からN要素が削除されたArrayを返します。
// 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
↑上に戻ります
2つの配列のいずれかに存在するすべての要素を1回返します。
// TODO // TODO
↑上に戻ります
提供された関数を両方の各配列要素に適用した後、2つの配列のいずれかに存在するすべての要素を1回返します。
// TODO // TODO
↑上に戻ります
提供されたコンパレータ関数を使用して、2つの配列のいずれかに1回存在するすべての要素を返します。
// TODO // TODO
↑上に戻ります
配列のすべての一意の値を返します。
// TODO // TODO
↑上に戻ります
提供されたコンパレータ関数に基づいて、配列のすべての一意の値を返します。
// TODO // TODO
↑上に戻ります
提供されたコンパレータ関数に基づいて、配列のすべての一意の値を返します。
// TODO // TODO
↑上に戻ります
どちらの配列からの重複値を含むのではなく、2つの配列間の一意の対称差を返します。
// TODO // TODO
↑上に戻ります
指定された値の1つを持つ配列の要素をフィルターします。
// TODO // TODO
↑上に戻ります
アレイから各可能なペアを作成することにより、提供された2つのうちの新しい配列を作成します。
// TODO // TODO
↑上に戻ります
2つの数値が互いにほぼ等しいかどうかを確認します。
// TODO // TODO
↑上に戻ります
平均2つ以上の数値を返します。
この方法は、数値をパラメーションとして除き、結果として平均を返します。
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
↑上に戻ります
2つの整数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
↑上に戻ります
2つのポイント間の距離を返します。
// 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
↑上に戻ります
NTH項まで、フィボナッチシーケンスを含むリストを生成します。
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 }
↑上に戻ります
2つ以上の数値/配列の間で最大の一般的な除数を計算します。
// TODO // TODO
↑上に戻ります
startとendが包括的で、2つの項間の比率が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
↑上に戻ります
与えられた数値が偶数である場合、 false以外の場合はtrueを返します。
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
↑上に戻ります
指定された数値がfalseな場合はtrueを返します。
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
↑上に戻ります
2つ以上の数値の最も一般的な倍数を返します。
// TODO // TODO
↑上に戻ります
クレジットカード番号、IMEI番号、ナショナルプロバイダー識別子番号など、さまざまな識別番号を検証するために使用されるLuhnアルゴリズムの実装。
// 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
↑上に戻ります
2つ以上の数値/配列の合計を返します。
// TODO // TODO
↑上に戻ります
提供された関数を使用して各要素を値にマッピングした後、配列の合計を返します。
// TODO // TODO
↑上に戻ります
実行された関数が1秒あたりの回数を返します。 hzはhertzの単位であり、1秒あたり1サイクルとして定義される周波数の単位です。
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
↑上に戻ります
CommaSeparated値(CSV)文字列を2D配列に変換します。
// TODO // TODO
↑上に戻ります
CommaSeparated値(CSV)文字列を2D配列のオブジェクトに変換します。文字列の最初の行は、タイトル行として使用されます。
// TODO // TODO
↑上に戻ります
regexを使用して、文字列が特定のサブストリングで終了するかどうかを確認します。
このメソッドは、テストする文字列とテストするサブストリングを除きます。
他のほとんどのチェックはすでに統合されています。
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
↑上に戻ります
CamelCaseから文字列を変換します。すべての単語を小文字にし、提供されたセパレーターを使用してそれらを組み合わせます(デフォルトは1つの空白です)。 PARAM発行== 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
↑上に戻ります
文字の最後のlengthを除くすべてを指定されたmask文字に置き換えます。 2番目の引数、 length省略して、 4文字のマスクされていないデフォルトを維持します。 lengthが負の場合、マスクされていない文字が文字列の開始時になります。 3番目の引数を省略して、 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()を使用して、指定された文字列の両側をパッドします。 3番目の引数charを省略して、Whitespace文字をデフォルトのパディング文字として使用します。
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." , "" }
↑上に戻ります
regexを使用して、特定のサブストリングで文字列が開始されるかどうかを確認します。
このメソッドは、テストする文字列とテストするサブストリングを除きます。
他のほとんどのチェックはすでに統合されています。
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"
↑上に戻ります
文字列をCamelCaseに変換します。
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"
↑上に戻ります
アルファ値が提供されている場合、カラーコードを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"
↑上に戻ります
ランダムな16進数色を生成します。
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
↑上に戻ります
文字列がy / yesまたはfalse場合はtrueを返します。文字列がn / no 。
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
↑上に戻ります
このプロジェクトに貢献することはいつでも大歓迎です。寄付ガイドをお読みください。
| ジョナス・シューベルト | デニス・バイオンディック |
30秒のC#が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.