Is any of the names in search-Names contained in text To Check, will check case insensitive - CSharp System

CSharp examples for System:String Case

Description

Is any of the names in search-Names contained in text To Check, will check case insensitive

Demo Code


using System.Text;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
using System;/* w w w  .  j  a  v  a2  s. c om*/

public class Main{
        /// <summary>
        /// Is any of the names in searchNames contained in textToCheck,
        /// will check case insensitive, for a normal case sensitive test
        /// use textToCheck.Contains(searchName).
        /// </summary>
        /// <param name="textToCheck">String to check</param>
        /// <param name="searchNames">Search names</param>
        /// <returns>Bool</returns>
        public static bool Contains( string textToCheck, string[] searchNames )
        {
            string stringToCheckLower = textToCheck.ToLower();
            foreach ( string name in searchNames )
                if ( stringToCheckLower.Contains( name.ToLower() ) )
                    return true;
            // Nothing found, no searchNames is contained in textToCheck
            return false;
        } // Contains(textToCheck, searchNames)
        #endregion

        #region String contains (for case insensitive compares)
        /// <summary>
        /// Is searchName contained in textToCheck, will check case insensitive,
        /// for a normal case sensitive test use textToCheck.Contains(searchName)
        /// </summary>
        /// <param name="textToCheck">Text to check</param>
        /// <param name="searchName">Search name</param>
        /// <returns>Bool</returns>
        public static bool Contains( string textToCheck, string searchName )
        {
            return textToCheck.ToLower().Contains( searchName.ToLower() );
        } // Contains(textToCheck, searchName)
        /// <summary>
        /// Helper function to convert letter to lowercase. Could someone
        /// tell me the reason why there is no function for that in char?
        /// </summary>
        public static char ToLower( char letter )
        {
            return (char)letter.ToString().ToLower(
                CultureInfo.InvariantCulture )[ 0 ];
        } // ToLower(letter)
}

Related Tutorials