GeneralExtensions - Qkrisi/ktanemodkit GitHub Wiki
public static class GeneralExtensions
Provides extensions useful for modding.
public static bool EqualsAny(this object obj, params object[] targets)
Will return true
if targets
contains obj
, otherwise false
.
public static bool InRange(this int num, int min, int max)
Will return true if num
is in the range of min
and max
(both inclusive), otherwise false
.
public static string FormatTime(this float seconds)
Formats the time in seconds to the format the bomb's timer has
public static string Join<T>(this IEnumerable<T> values, string separator = " ")
Joins together an iteration of objects into a string separated by separator
public static string Wrap(this string str, int maxLength)
public static string Wrap(this string str, int maxLength, string prefix)
Forces the string to word wrap so that each line doesn't exced the maxLength
.
prefix
will be added to the beginning of each line.
public static int? TryParseInt(this string number)
Tries parsing the string into an integer, will return it if succeeded, otherwise null
.
public static bool ContainsIgnoreCase(this string str, string value)
Will return true
if str
contains value
ignoring letter case, otherwise false
.
public static bool EqualsIgnoreCase(this string str, string value)
Will return true
if str
equals value
ignoring letter case, otherwise false
.
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int N)
Returns the last N
elements of source
.
public static bool RegexMatch(this string str, params string[] patterns)
public static bool RegexMatch(this string str, out Match match, params string[] patterns)
Will return true
, if str
˙matches any of the regular expression patterns in patterns
, match
will be the first successful match.
public static double TotalSeconds(this DateTime datetime)
Returns the total number of seconds based on datetime
's ticks
public static bool TryEquals(this string str, string value)
public static bool TryEquals(this string str, string value, StringComparison comparisonType)
Will return true
if str
equals value
, otherwise false
(handling nulls and empty strings).
public static void AddSafe<K, V>(this IDictionary<K, List<V>> dic, K key, V value)
Adds an element to a List<V>
stored in the current IDictionary<K, List<V>>
. If the specified key does not exist in the current IDictionary, a new List is created.
public static T Shuffle<T>(this T list) where T : IList
Brings the elements of the given list into a random order.
public static T PickRandom<T>(this IEnumerable<T> src)
Returns a random element from the specified collection.
public static bool TryParseTime(this string timeString, out float time)
Will return true
, if timeString
is a valid time (H:M:S), otherwise false
.
public static Color Color(int red, int green, int blue, int alpha = 255)
Create color from the specified RGBA channels.
public static Color Color(this string colorString)
Create a color from the specified HTML color string.
public static void AppendLineFormat(this StringBuilder builder, string message, params object[] args)
Append a line to builder
, where line is message
formatted by args
.
public static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
Returns the index of the first element in source
that predictate
applies to.