Last active
September 27, 2015 21:38
-
-
Save marlun78/1336047 to your computer and use it in GitHub Desktop.
C# StringExtensions Class
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
/** | |
* C# String Extensions | |
* Copyright (c) 2011 marlun78 | |
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83 | |
*/ | |
using System; | |
using System.Collections.Generic; | |
namespace Garkbit { | |
public static class StringExtensions { | |
public static bool IsNotNullOrEmpty(this string _this) { | |
return !String.IsNullOrEmpty(_this); | |
} | |
public static string Repeat(this string _this, int count) { | |
return Repeat(_this, count, String.Empty); | |
} | |
public static string Repeat(this string _this, int count, string separator) { | |
if (count <= 0 || String.IsNullOrEmpty(_this)) { | |
return String.Empty; | |
} | |
else { | |
List<string> list = new List<string>(); | |
for (int i = 0; i < count; i++) { | |
list.Add(_this); | |
} | |
return String.Join(separator, list); | |
} | |
} | |
public static bool ContainsAll(this string _this, params string[] values) { | |
if (_this == null) { | |
throw new NullReferenceException("String.ContainsAll() can not be called on a null value."); | |
} | |
for (int i = 0, l = values.Length; i < l; i++) { | |
if (!_this.Contains(values[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static bool ContainsAny(this string _this, params string[] values) { | |
if (_this == null) { | |
throw new NullReferenceException("String.ContainsAny() can not be called on a null value."); | |
} | |
for (int i = 0, l = values.Length; i < l; i++) { | |
if (_this.Contains(values[i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment