지정된 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() 와 같은 술어와 배열을 취하지 만 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.Descending 순서 또는 방향으로 정렬 된 경우 열거 가능한 순서, Direction.NotSorted 으로 정렬 된 경우 Direction.Ascending 합니다. 정렬되지 않았거나 값이 하나만 있으면 안개가 없습니다.
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
↑ 위로 돌아갑니다
중복 값을 필터링하지 않고 두 배열 사이의 대칭 차이를 반환합니다.
// 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
↑ 위로 돌아갑니다
평균 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
↑ 위로 돌아갑니다
두 정수 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
↑ 위로 돌아갑니다
숫자가 다른 숫자로 나눌 수 있는지 확인합니다.
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
↑ 위로 돌아갑니다
신용 카드 번호, 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
↑ 위로 돌아갑니다
둘 이상의 숫자/배열의 합을 반환합니다.
// TODO // TODO
↑ 위로 돌아갑니다
제공된 함수를 사용하여 각 요소를 값에 매핑 한 후 배열의 합을 반환합니다.
// TODO // TODO
↑ 위로 돌아갑니다
초당 실행 된 함수의 횟수를 반환합니다. hz 는 hertz 의 장치이며, 주파수 단위는 초당 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
↑ 위로 돌아갑니다
쉼표로 구분 된 값 (CSV) 문자열을 2D 배열로 변환합니다.
// TODO // TODO
↑ 위로 돌아갑니다
쉼표로 구분 된 값 (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에서 문자열을 변환합니다. 모든 단어를 소문자로 만들고 제공된 분리기를 사용하여 결합합니다 (기본값은 하나의 공백입니다). Param issentence == 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"
↑ 위로 돌아갑니다
문자열이 다른 문자열의 아나그램인지 확인합니다 (Case-Insensentitive).
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
↑ 위로 돌아갑니다
주어진 문자열이 Palindrome 인 경우 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 문자로 대체합니다. 두 번째 인수 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 를 생략하여 공백 문자를 기본 패딩 문자로 사용하십시오.
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 인 경우 true 반환합니다. 또는 문자열이 n / no 인 경우 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.