Returns the index of the last repeated index of the first group of repeated characters that begin with the character To Find - CSharp System

CSharp examples for System:Char

Description

Returns the index of the last repeated index of the first group of repeated characters that begin with the character To Find

Demo Code

//  Copyright ? 2011, Grid Protection Alliance.  All Rights Reserved.
using System.Text.RegularExpressions;
using System.Text;
using System.IO;//from  www .  ja v a2s .c  o m
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Returns the index of the last repeated index of the first group of repeated characters that begin with the <paramref name="characterToFind"/>.
        /// </summary>
        /// <param name="value">String to process.</param>
        /// <param name="characterToFind">The character of interest.</param>
        /// <param name="startIndex">The index from which to begin the search.</param>
        /// <returns>The index of the last instance of the character that is repeated or (-1) if no repeated chars found.</returns>
        private static int LastIndexOfRepeatedChar(string value, char characterToFind, int startIndex)
        {
            if (startIndex > value.Length - 1)
                return -1;

            int i = value.IndexOf(characterToFind, startIndex);

            if (i == -1)
                return -1;

            for (int j = i + 1; j < value.Length; j++)
            {
                if (value[j] != characterToFind)
                    return j - 1;
            }

            return value.Length - 1;
        }
}

Related Tutorials