Get First Difference Index With - CSharp System

CSharp examples for System:String Search

Description

Get First Difference Index With

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.IO;/*from  ww w. j av  a2 s.c  o  m*/
using System.Collections.Generic;
using System;

public class Main{
        public static int GetFirstDifferenceIndexWith(this string input, string other, bool ignoreCase = false)
        {
            var firstDifferenceIndex = -1;

            if (input != null && other != null)
            {
                var maxIndex = Math.Min(input.Length, other.Length);
                for (var i = 0; i < maxIndex; i++)
                {
                    var areEqualChars = ignoreCase ?
                        char.ToUpperInvariant(input[i]) == char.ToUpperInvariant(other[i]) :
                        input[i] == other[i];
                    if (!areEqualChars)
                    {
                        firstDifferenceIndex = i;
                        break;
                    }
                }

                if (firstDifferenceIndex < 0 && input.Length != other.Length)
                {
                    firstDifferenceIndex = maxIndex;
                }
            }

            if (input == null ^ other == null)
            {
                firstDifferenceIndex = 0;
            }

            return firstDifferenceIndex;
        }
}

Related Tutorials