using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace PepperDash.Essentials.Core
{
public static class StringExtensions
{
///
/// Returns null if a string is empty, otherwise returns the string
///
/// string input
/// null if the string is emtpy, otherwise returns the string
public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}
///
/// Returns null if a string is empty or made of only whitespace characters, otherwise returns the string
///
/// string input
/// null if the string is wempty or made of only whitespace characters, otherwise returns the string
public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrEmpty(s.Trim()) ? null : s;
}
///
/// Returns a replacement string if the input string is empty or made of only whitespace characters, otherwise returns the input string
///
/// input string
/// string to replace with if input string is empty or whitespace
/// returns newString if s is null, emtpy, or made of whitespace characters, otherwise returns s
public static string ReplaceIfNullOrEmpty(this string s, string newString)
{
return string.IsNullOrEmpty(s) ? newString : s;
}
///
/// Overload for Contains that allows setting an explicit String Comparison
///
/// Source String
/// String to check in Source String
/// Comparison parameters
/// true of string contains "toCheck"
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (string.IsNullOrEmpty(source)) return false;
return source.IndexOf(toCheck, comp) >= 0;
}
///
/// Performs TrimStart() and TrimEnd() on source string
///
/// String to Trim
/// Trimmed String
public static string TrimAll(this string source)
{
return string.IsNullOrEmpty(source) ? string.Empty : source.TrimStart().TrimEnd();
}
///
/// Performs TrimStart(chars char[]) and TrimEnd(chars char[]) on source string.
///
/// String to Trim
/// Char Array to trim from string
/// Trimmed String
public static string TrimAll(this string source, char[] chars)
{
return string.IsNullOrEmpty(source) ? string.Empty : source.TrimStart(chars).TrimEnd(chars);
}
}
}