public static bool Intersects(this ITimeInterval time1, ITimeInterval time2) { return time1 != null && time2 != null && (time1.Contains(time2.From) || time1.Contains(time2.To)); }
/// <summary> /// Converts <paramref name="folderStructureOptions"/> to string array /// </summary> /// <param name="folderStructureOptions">Folder structure retrieve options</param> /// <returns>String array representation of <paramref name="folderStructureOptions"/></returns> public static string[] ToStringArray(this RetrieveFolderStructureOptions folderStructureOptions) { List<string> result = new List<string>(3); if(folderStructureOptions.Contains(RetrieveFolderStructureOptions.NoFiles)) { result.Add("nofiles"); } if (folderStructureOptions.Contains(RetrieveFolderStructureOptions.NoZip)) { result.Add("nozip"); } if (folderStructureOptions.Contains(RetrieveFolderStructureOptions.OneLevel)) { result.Add("onelevel"); } if (folderStructureOptions.Contains(RetrieveFolderStructureOptions.Simple)) { result.Add("simple"); } return result.ToArray(); }
public static bool HasRouteKey(this string routeUrl, string routeKey) { return routeUrl.Contains("{" + routeKey + "}") || routeUrl.Contains("{" + "*" + routeKey + "}"); }
public static double GetDoubleValueFromStringEntry(this string val) { // Todo, felkollar var cultureToUse = System.Threading.Thread.CurrentThread.CurrentCulture; if (string.IsNullOrEmpty(val)) { return 0.0; } if (val.Contains(".")) { cultureToUse = new CultureInfo("en-US"); } else if (val.Contains(",")) { cultureToUse = new CultureInfo("sv-SE"); } var cleanVal = val.Trim() .Replace(" ", string.Empty) .Replace(":", string.Empty) .Replace(";", string.Empty) .Replace(":", string.Empty); double tempd; return double.TryParse(cleanVal, NumberStyles.Number, cultureToUse, out tempd) ? Math.Round(tempd, 2) : 0.0; }
public static string GetStringBetween( this string input, string startString, string endString, int startFrom = 0) { input = input.Substring(startFrom); startFrom = 0; if (!input.Contains(startString) || !input.Contains(endString)) { return string.Empty; } var startPosition = input.IndexOf(startString, startFrom, StringComparison.Ordinal) + startString.Length; if (startPosition == -1) { return string.Empty; } var endPosition = input.IndexOf(endString, startPosition, StringComparison.Ordinal); if (endPosition == -1) { return string.Empty; } return input.Substring(startPosition, endPosition - startPosition); }
/// <summary> /// http://stackoverflow.com/a/39096603/128662 /// </summary> public static string RemoveBetween(this string rawString, char enter, char exit) { if (rawString.Contains(enter) && rawString.Contains(exit)) { int substringStartIndex = rawString.IndexOf(enter) + 1; int substringLength = rawString.LastIndexOf(exit) - substringStartIndex; string replaced = rawString; if (substringLength > 0 && substringLength > substringStartIndex) { string substring = rawString.Substring(substringStartIndex, substringLength).RemoveBetween(enter, exit); if (substring.Length != substringLength) // This would mean that letters have been removed { replaced = rawString.Remove(substringStartIndex, substringLength).Insert(substringStartIndex, substring); } } //Source: http://stackoverflow.com/a/1359521/3407324 Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", enter, exit)); return new Regex(" +").Replace(regex.Replace(replaced, string.Empty), " "); } else { return rawString; } }
public static bool Contains(this Rect rect, Rect otherRect) { Vector2 topLeftPoint = new Vector2(otherRect.xMin, otherRect.yMin); Vector2 bottomRightPoint = new Vector2(otherRect.xMax, otherRect.yMax); //Debug.Log("Rect " + new Vector2(rect.xMax, rect.yMax) + " topLeftPoint " + topLeftPoint + " bottomRightPoint " + bottomRightPoint); return rect.Contains(topLeftPoint) && rect.Contains(bottomRightPoint); }
public static string StemNameBase(this string name) { if (name == null) { throw new ArgumentNullException("name"); } if (!name.Contains(" ")) { name = name.Replace('_', ' '); if (!name.Contains(" ")) { name = name.Replace('-', ' '); } name = respace.Replace(name, " "); } var ws = name; var wsprev = name; do { wsprev = ws; ws = trim.Replace(wsprev.Trim(), " ").Trim(); } while (wsprev != ws); if (string.IsNullOrWhiteSpace(ws)) { return name; } return ws; }
public static dynamic parseRow(this string matchString, string[] columns) { var defaults = new Dictionary<string, string>() { {"Event", "Open Play"}, {"Location", "Courtyard"}, {"Club", "Courtyard"}, {"Time", "6am"}, {"Comments", ""}, }; var delimiter = matchString.Contains("\t") ? "\t" : matchString.Contains(",") ? "," : " "; var array = matchString.Split(delimiter.ToCharArray(), StringSplitOptions.None); //var values = new Dictionary<string, string>(); dynamic expando = new ExpandoObject(); var values = (IDictionary<string, object>)expando; for (int i = 0; i < array.Length; i++) { var key = columns[i]; var value = array[i]; if (value == "") value = defaults[key]; values.Add(key, value); } return values; }
public static CellType ToCellType(this string battleType) { return battleType.Contains("sp_midnight") ? CellType.夜戦 : battleType.Contains("ld_airbattle") ? CellType.空襲戦 //ColorNoからも分かるが、航空戦と誤認しないため : battleType.Contains("airbattle") ? CellType.航空戦 : CellType.None; }
public static bool IsBase64(this string base64String) { // Based on http://stackoverflow.com/a/6309439 // pre-test to check if it's obviously invalid // (fast for long values like images and videos) if (string.IsNullOrEmpty(base64String) || base64String.Length % 4 != 0 || base64String.Contains(" ") || base64String.Contains("\t") || base64String.Contains("\r") || base64String.Contains("\n")) { return false; } // now do the real test try { Convert.FromBase64String(base64String); return true; } catch (FormatException) { return false; } }
/// <summary> /// Compares to the given value returning true if comparable. /// </summary> /// <param name="value">The value.</param> /// <param name="compareValue">The compare value.</param> /// <param name="compareType">Type of the compare.</param> /// <returns></returns> public static bool CompareTo( this string value, string compareValue, ComparisonType compareType ) { // Evaluate compare types that are not type specific switch ( compareType ) { case ComparisonType.Contains: return value.Contains( compareValue ); case ComparisonType.DoesNotContain: return !value.Contains( compareValue ); case ComparisonType.StartsWith: return value.StartsWith( compareValue, StringComparison.OrdinalIgnoreCase ); case ComparisonType.EndsWith: return value.EndsWith( compareValue, StringComparison.OrdinalIgnoreCase ); case ComparisonType.IsBlank: return string.IsNullOrWhiteSpace( value ); case ComparisonType.IsNotBlank: return !string.IsNullOrWhiteSpace( value ); case ComparisonType.RegularExpression: return Regex.IsMatch( value, compareValue ); } // numeric compares decimal? decimalValue = value.AsDecimalOrNull(); decimal? decimalCompareValue = compareValue.AsDecimalOrNull(); if ( decimalValue.HasValue && decimalCompareValue.HasValue ) { switch ( compareType ) { case ComparisonType.EqualTo: return decimalValue == decimalCompareValue; case ComparisonType.GreaterThan: return decimalValue.Value > decimalCompareValue.Value; case ComparisonType.GreaterThanOrEqualTo: return decimalValue.Value >= decimalCompareValue.Value; case ComparisonType.LessThan: return decimalValue.Value < decimalCompareValue.Value; case ComparisonType.LessThanOrEqualTo: return decimalValue.Value <= decimalCompareValue.Value; case ComparisonType.NotEqualTo: return decimalValue.Value != decimalCompareValue.Value; } } // date time compares DateTime? datetimeValue = value.AsDateTime(); DateTime? datetimeCompareValue = compareValue.AsDateTime(); if ( datetimeValue.HasValue && datetimeCompareValue.HasValue ) { switch ( compareType ) { case ComparisonType.EqualTo: return datetimeValue == datetimeCompareValue; case ComparisonType.GreaterThan: return datetimeValue.Value > datetimeCompareValue.Value; case ComparisonType.GreaterThanOrEqualTo: return datetimeValue.Value >= datetimeCompareValue.Value; case ComparisonType.LessThan: return datetimeValue.Value < datetimeCompareValue.Value; case ComparisonType.LessThanOrEqualTo: return datetimeValue.Value <= datetimeCompareValue.Value; case ComparisonType.NotEqualTo: return datetimeValue.Value != datetimeCompareValue.Value; } } // string compares switch ( compareType ) { case ComparisonType.EqualTo: return value.Equals( compareValue, StringComparison.OrdinalIgnoreCase ); case ComparisonType.GreaterThan: return value.CompareTo( compareValue ) > 0; case ComparisonType.GreaterThanOrEqualTo: return value.CompareTo( compareValue ) >= 0; case ComparisonType.LessThan: return value.CompareTo( compareValue ) < 0; case ComparisonType.LessThanOrEqualTo: return value.CompareTo( compareValue ) <= 0; case ComparisonType.NotEqualTo: return !value.Equals( compareValue, StringComparison.OrdinalIgnoreCase ); } return false; }
public static string SubstringRange(this string s, char start, char end) { if (s.Contains(start) && s.Contains(end)) { return s.Substring(s.IndexOf(start), s.IndexOf(end) - s.IndexOf(start) + 1); } else return ""; }
public static string RemoveRange(this string s, char start, char end) { if (s.Contains(start) && s.Contains(end)) { return s.Remove(s.IndexOf(start), s.IndexOf(end) - s.IndexOf(start) + 1); } else return s; }
public static bool IntersectsLine( this Rect r, Vector2 p1, Vector2 p2 ) { return LineIntersectsLine ( p1, p2, new Vector2 ( r.x, r.y ), new Vector2 ( r.x + r.width, r.y ) ) || LineIntersectsLine ( p1, p2, new Vector2 ( r.x + r.width, r.y ), new Vector2 ( r.x + r.width, r.y + r.height ) ) || LineIntersectsLine ( p1, p2, new Vector2 ( r.x + r.height, r.y + r.height ), new Vector2 ( r.x, r.y + r.height ) ) || LineIntersectsLine ( p1, p2, new Vector2 ( r.x, r.y + r.height ), new Vector2 ( r.x, r.y ) ) || ( r.Contains ( p1 ) && r.Contains ( p2 ) ); }
public static string NormalizeLineEndings(this string input) { if (input.Contains("\n") && !input.Contains("\r\n")) { input = input.Replace("\n", "\r\n"); } return input; }
/// <summary> /// 取平台的字符串值 /// </summary> /// <param name="platform"></param> /// <returns></returns> public static string GetString(this Platform platform) { StringBuilder stringBuilder = new StringBuilder(); if (platform.Contains(Platform.Android)) stringBuilder.AppendFormat("{0},", Platform.Android.ToString().ToLowerInvariant()); if (platform.Contains(Platform.iOS)) stringBuilder.AppendFormat("{0},", Platform.iOS.ToString().ToLowerInvariant()); return stringBuilder.ToString().TrimEnd(','); }
public static bool isDataValid(this string data) { if (data.Contains(UdpHelper.START_PROTOCOL) && data.Contains(UdpHelper.END_PROTOCOL)) { return true; } return false; }
public static bool InvertedContains(this List<string> list, string item, bool include) { if (include) { return list.Contains(item); } return !list.Contains(item); }
/// <summary> /// Implement's VB's Like operator logic. /// </summary> public static bool Like(this string s, string pattern) { if (s != null && pattern != null) // values cant be null { if (s.Contains(' ') && !pattern.Contains(' ')) // check if there is a white space in the current value s { List<String> sStrings; sStrings = s.Split(' ').ToList<String>(); // make the List to contain the values between whitespaces foreach(String sString in sStrings) // go through all values between whitespace and check if there is a match with the pattern { Match test = Regex.Match(sString, "^" + pattern, RegexOptions.IgnoreCase); if (test.Success) return true; } return false; } else { Match test = Regex.Match(s, "^" + pattern, RegexOptions.IgnoreCase); // check if there is a match with the pattern if (test.Success) return true; else { if (s == "") return false; int count = 0; for(int index = 0; index < s.Length; index++) { if (pattern.Length > index) { if (s[index] == pattern[index]) count++; else count--; } else count--; } if (1 <= count) return true; else { if (s.Contains(pattern)) return true; else return false; } } } } else return false; }
/// <summary> /// Get a composite <see cref="DateTime"/> instance based on <paramref name="date"/> and <paramref name="time"/> values. /// </summary> /// <param name="dataset">Dataset from which data should be retrieved.</param> /// <param name="date">Tag associated with date value.</param> /// <param name="time">Tag associated with time value.</param> /// <returns>Composite <see cref="DateTime"/>.</returns> public static DateTime GetDateTime(this DicomDataset dataset, DicomTag date, DicomTag time) { var dd = dataset.Contains(date) ? dataset.Get<DicomDate>(date) : null; var dt = dataset.Contains(time) ? dataset.Get<DicomTime>(time) : null; var da = dd != null && dd.Count > 0 ? dd.Get<DateTime>(0) : DateTime.MinValue; var tm = dt != null && dt.Count > 0 ? dt.Get<DateTime>(0) : DateTime.MinValue; return new DateTime(da.Year, da.Month, da.Day, tm.Hour, tm.Minute, tm.Second); }
public static string Braces(this string value) { if (string.IsNullOrWhiteSpace(value)) throw new Exception("MDX element value cannot be null or white space"); if (value.Contains('[') || value.Contains(']')) throw new Exception("MDX element already contains square braces"); return string.Format("[{0}]", value); }
/// <summary> /// check if the bounds is completely inside, check all four points /// </summary> static public bool Contains(this PolygonCollider2D poly, Bounds b) { Vector2 pmin = b.min; Vector2 pmax = b.max; // check if the bounds is completely inside, check all four points return poly.Contains(pmin) && poly.Contains(pmax) && poly.Contains(new Vector2(pmin.x, pmax.y)) && poly.Contains(new Vector2(pmax.x, pmin.y)); }
public static Parameter ToParameter(this String arg, ILiveStatement statement) { string defVal = arg.Contains("=") ? arg.SubstringAfter("=").Trim() : null; string type = null; string paramName = arg.SubstringBefore(':', '=').Trim(); if (!arg.Contains(":")) { #region insist that default value is specified and simple if (string.IsNullOrEmpty(defVal)) { throw new Exception("Error in " + statement.LineNumber + " : no type specified, no default value"); } switch (defVal) { case "null": throw new Exception("Error in " + statement.LineNumber + " : no type specified, unable to determine type from default value"); case "true": case "false": type = "bool"; break; } if (type == null) { if (defVal.StartsWith("\"") && defVal.EndsWith("\"")) { type = "string"; } else { float f = 0; bool isFloat = float.TryParse(defVal, out f); if (isFloat) { type = "number"; } else { throw new Exception("Error in " + statement.LineNumber + " : no type specified, unable to determine type from default value"); } } } #endregion } else { type = arg.SubstringAfter(":").TrimStart().SubstringBefore(' ', '=').TrimStart(); } var p = new Parameter { Name = paramName, InitialValue = defVal, Type = type }; return p; }
public static void Contains(this string target, string value, bool hasAccess) { if (hasAccess) { Assert.That(target.Contains(value), "Target should include {0} and it does not.", value); } else { Assert.That(!target.Contains(value), "Target should not include {0} and it does.", value); } }
/// <summary> /// Humanizes the input string; e.g. Underscored_input_String_is_turned_INTO_sentence -> 'Underscored input String is turned INTO sentence' /// </summary> /// <param name="input">The string to be humanized</param> /// <returns></returns> public static string Humanize(this string input) { // if input is all capitals (e.g. an acronym) then return it without change if (input.All(Char.IsUpper)) return input; if (input.Contains('_') || input.Contains('-')) return FromUnderscoreDashSeparatedWords(input); return FromPascalCase(input); }
public static string PercentEncode(this string source) { var value = source; if (source.Contains(";")) { value = source.Replace(";", "%3B"); } if (source.Contains(",")) { value = source.Replace(",", "%2C"); } return value; }
/// <summary> /// Determines whether the specified text is double. /// </summary> /// <param name="text">The text.</param> /// <returns> /// <c>true</c> if the specified text is double; otherwise, <c>false</c>. /// </returns> public static bool IsDouble(this string text) { if (text.Contains('.') || text.Contains(Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator)) { double d = 0; if (double.TryParse(text, out d)) return true; } return false; }
public static Browser GetBrowserFromString(this string str) { str = str.ToLower().Trim(); if (str.Contains("firefox")) return Browser.Firefox; if (str.Contains("chrome")) return Browser.GoogleChrome; if (str.Contains("safari")) return Browser.Safari; if (str.Contains("opera")) return Browser.Opera; return str.Contains("ie") ? Browser.InternetExplorer : Browser.Other; }
public static string GetStrBetweenTags(this string value, string startTag, string endTag) { if (value.Contains(startTag) && value.Contains(endTag)) { int index = value.IndexOf(startTag) + startTag.Length; return value.Substring(index, value.IndexOf(endTag) - index); } else return null; }