Skip to content

Instantly share code, notes, and snippets.

@marlun78
Last active September 27, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlun78/1336047 to your computer and use it in GitHub Desktop.
Save marlun78/1336047 to your computer and use it in GitHub Desktop.
C# StringExtensions Class
/**
* 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