Created
January 26, 2012 14:00
-
-
Save leegould/1682879 to your computer and use it in GitHub Desktop.
string extension method - HasRepeatedCharacters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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