แปลงอาร์เรย์ของวัตถุเป็นสตริงค่าที่คั่นด้วยเครื่องหมายจุลภาค (CSV) ที่มีเฉพาะ columns ที่ระบุ
// TODO // TODO
↑ย้อนกลับไปด้านบน
ใช้วัตถุหรือวัตถุที่ทำซ้ำจำนวนเท่าใดก็ได้ที่มีคุณสมบัติ length และส่งคืนหนึ่งที่ยาวที่สุด หากวัตถุหลายชิ้นมีความยาวเท่ากันวัตถุแรกจะถูกส่งคืน ส่งคืน -1 หากไม่มีข้อโต้แย้ง
// TODO // TODO
↑ย้อนกลับไปด้านบน
ส่งคืนองค์ประกอบสูงสุด n จากอาร์เรย์ที่ให้ไว้ หาก n มากกว่าหรือเท่ากับความยาวของอาร์เรย์ที่ให้ไว้ให้ส่งคืนอาร์เรย์ดั้งเดิม (เรียงลำดับตามลำดับจากมากไปน้อย)
// TODO // TODO
↑ย้อนกลับไปด้านบน
ส่งคืนองค์ประกอบขั้นต่ำ n จากอาร์เรย์ที่ให้ไว้ หาก n มากกว่าหรือเท่ากับความยาวของอาร์เรย์ที่ให้ไว้ให้ส่งคืนอาร์เรย์ดั้งเดิม (เรียงลำดับตามลำดับจากน้อยไปมาก)
// TODO // TODO
↑ย้อนกลับไปด้านบน
ส่งคืน true หากฟังก์ชั่นเพรดิเคตที่ให้จะส่งกลับ false สำหรับองค์ประกอบทั้งหมดในคอลเลก 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
↑ย้อนกลับไปด้านบน
รับองค์ประกอบสุ่ม n ที่คีย์ที่ไม่ซ้ำกันตั้งแต่ array จนถึงขนาดของ array
// TODO // TODO
↑ย้อนกลับไปด้านบน
วิธีนี้จะเปลี่ยนเนื้อหาของอาร์เรย์โดยการลบองค์ประกอบที่มีอยู่และ/หรือเพิ่มองค์ประกอบใหม่ คล้ายกับ 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.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
↑ย้อนกลับไปด้านบน
สร้างรายการที่มีลำดับ Fibonacci จนถึงเทอม 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
↑ย้อนกลับไปด้านบน
การดำเนินการตามอัลกอริทึม 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
↑ย้อนกลับไปด้านบน
ตรวจสอบว่าสตริงจะจบลงด้วยสตริงย่อยที่กำหนดโดยใช้ 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 ตัวอักษรตัวแรกของประโยคจะเป็นตัวพิมพ์ใหญ่และจะเพิ่ม DOT ในตอนท้าย (ค่าเริ่มต้นเป็นจริง)
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 ถ้าสตริงที่กำหนดเป็น palindrome, 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!"
↑ย้อนกลับไปด้านบน
แยกสตริง multiline ออกเป็นอาร์เรย์ของเส้น
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"
↑ย้อนกลับไปด้านบน
สร้างสีหกเหลี่ยมแบบสุ่ม
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
↑ย้อนกลับไปด้านบน
ส่งคืน true ถ้าสตริงคือ y / yes หรือ false ถ้าสตริงคือ 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
↑ย้อนกลับไปด้านบน
คุณยินดีที่จะมีส่วนร่วมในโครงการนี้เสมอ โปรดอ่านคู่มือการบริจาค
| Jonas Schubert | Denis Biondic |
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.