Performs a Trim() on both strings and then a case-insensitive comparison and returns true if equivalent. - CSharp System

CSharp examples for System:String Case

Description

Performs a Trim() on both strings and then a case-insensitive comparison and returns true if equivalent.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System;/*from  ww  w  .  j a va 2s. c  o m*/

public class Main{
        /// <summary>
        /// Performs a Trim() on both strings and then a case-insensitive comparison and returns true if equivalent.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="compare"></param>
        /// <returns></returns>
        public static bool CaseInsensitiveTrimmedEquals(this string self, params string[] compare)
        {
            for (var i = 0; i < compare.Length; ++i)
            {
                if (self == null && compare[i] == null) return true;
                if (self == null || compare[i] == null) return false;
                if (string.Equals(self.Trim(), (compare[i] ?? string.Empty).Trim(), StringComparison.OrdinalIgnoreCase)) return true;
            }
            return false;
        }
}

Related Tutorials