Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if string contains only one type of letter C#

I want to find out how I could detect if a string contains only one type of letter for example

Input:

......

I want it to detect if the string contains only that letter, so the output should be something like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String contains only one type of letter

>Solution :

Try this:

private bool ContainsOnlyOneLetter(string String)
    {
        if(String.Length == 0)
        {
            return true;
        }
        for(int i = 0; i < String.Length;i++)
        {
            if(String.Substring(i,1) != String.Substring(0,1))
            {
                return false;
            }
        }
        return true;
    }

And you can use the function like this:

bool containsOneLetter = ContainsOnlyOneLetter("...");

        if (containsOneLetter == true)
        {
            //Put code here when the letters are the same...
        }
        else
        {
            //Code when there are different letters..
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading