Skip to content

Instantly share code, notes, and snippets.

@leegould
Created January 26, 2012 14:00
Show Gist options
  • Save leegould/1682879 to your computer and use it in GitHub Desktop.
Save leegould/1682879 to your computer and use it in GitHub Desktop.
string extension method - HasRepeatedCharacters
namespace Extensions
{
public static class StringExtensions
{
/// <summary>
/// Check string for repeated characters
/// </summary>
/// <param name="str">string to check (the object being called on)</param>
/// <returns>
/// <c>true</c> if the string has repeated characters otherwise, <c>false</c>.
/// </returns>
public static bool HasRepeatedCharacters(this string str)
{
var chars = str.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
char c = chars[i];
for (int j = 1; j < chars.Length; j++)
{
if (j == i) { continue; }
if (c.Equals(chars[j])) { return true; }
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment