Created
January 17, 2018 14:00
-
-
Save hallojoe/f1c75ef948aca06bef69d2e72cc4f847 to your computer and use it in GitHub Desktop.
String extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Cabana.UmbracoExamineApi | |
{ | |
public static class StringExtensions | |
{ | |
/// <summary> | |
/// VanDamme split | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static List<string> VanDamme(this string split, bool kick = true) => split.SplitCsv(!kick); | |
/// <summary> | |
/// Csv string split helper | |
/// </summary> | |
/// <param name="s"></param> | |
/// <returns></returns> | |
public static List<string> SplitCsv(this string s, bool allowEmpty = false) | |
=> s.Split(new[] { ',' }, allowEmpty ? StringSplitOptions.None : StringSplitOptions.RemoveEmptyEntries)? | |
.Select(x => x.Trim())? | |
.ToList(); | |
/// <summary> | |
/// InvariantCultureIgnoreCase equals | |
/// </summary> | |
/// <param name="s"></param> | |
/// <param name="input"></param> | |
/// <returns></returns> | |
public static bool Equalsish(this string s, string input) => | |
s.Equals(input, StringComparison.InvariantCultureIgnoreCase); | |
/// <summary> | |
/// StringComparisonable Many Replace | |
/// </summary> | |
/// <param name="source">Source string</param> | |
/// <param name="oldValues">Find strings</param> | |
/// <param name="newValue">Replacement string</param> | |
/// <param name="stringComparison">Comparison type</param> | |
/// <returns></returns> | |
public static string Replace(this string source, string[] oldValues, string newValue, StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase) | |
{ | |
foreach (var oldValue in oldValues) | |
source = source.Replace(oldValue, newValue, stringComparison); | |
return source; | |
} | |
/// <summary> | |
/// StringComparisonable Replace | |
/// </summary> | |
/// <param name="source">Source string</param> | |
/// <param name="oldValue">Find string</param> | |
/// <param name="newValue">Replacement string</param> | |
/// <param name="stringComparison">Comparison type</param> | |
/// <returns></returns> | |
public static string Replace(this string source, string oldValue, string newValue, StringComparison stringComparison) | |
{ | |
newValue = newValue ?? string.Empty; | |
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(oldValue) || oldValue.Equals(newValue, stringComparison)) | |
return source; | |
int foundAt; | |
while ((foundAt = source.IndexOf(oldValue, 0, stringComparison)) != -1) | |
source = source.Remove(foundAt, oldValue.Length).Insert(foundAt, newValue); | |
return source; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment