Reputation: 3651
The following code give the ArgumentOutOfRangeException when I try to use it to test my strings if they contain characters other than numbers and alphabets.
Guess it makes sense considering the line I marked with the comment will run over the bounds at the last index. How do I resolve this cos I can't possibly start off with (i-1, i) nor (i,i). Please advice. Thanks.
public static bool LegalString(string s)
{
string dict = "abcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < s.Length; i++)
{
if (!dict.Contains(s.Substring(i, 1).ToLower()))
{
Console.WriteLine("'" + s.Substring(i, i + 1).ToLower() + "'");//Line that is giving the error
return false;
}
}
return true;
}
EDIT:
if (!LegalString(name))
{
MessageBox.Show("Invalid Entry for name. Enter only numbers and alphabets.");
}
I am using the method as above. It will work fine if I enter a name for example: Sam.
If I enter for example, Sam/// it will return the exception when I am expecting the messagebox to appear instead.
Upvotes: 0
Views: 422
Reputation: 8098
It's the i + 1
within this line that's the issue:
Console.WriteLine("'" + s.Substring(i, i + 1).ToLower() + "'");//Line that is giving the error
If you're trying to display the substring that isn't within the comparison string, you need to change the line to this:
Console.WriteLine("'" + s.Substring(i, 1).ToLower() + "'");//Line that is giving the error
EDIT:
I modified your method to have the following and it works as expected with the input string of "Sam//". i.e. it returns false
:
public static bool LegalString(string s)
{
string dict = "abcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < s.Length; i++)
{
if (!dict.Contains(s.Substring(i, 1).ToLower()))
{
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 4770
What is the expect output in the line Console.WriteLine("'" + s.Substring(i, i + 1).ToLower() + "'")
. That is the error, you are expecting a sub string with the lenght i + 1. I don't think so. Maybe should be s.Lenght - i, just 1.
Check it out.
Upvotes: 0
Reputation: 366
You could use a regular expression instead...
private static bool ContainsNonAlphanumeric(string s)
{
Regex pattern = new Regex(@"\W|_");
if(pattern.IsMatch(s))
{
return true;
}
return false;
}
Have a look at this fiddle... http://dotnetfiddle.net/g2YAT0
Upvotes: 0
Reputation: 727047
You can avoid the loop altogether, and use a built-in function to test for letter or digit, like this:
public static bool LegalString(string s) {
if (!s.All(Char.IsLetterOrDigit)) {
Console.WriteLine( "'{0}'", s.First(c => !Char.IsLetterOrDigit(c)));
return false;
}
return true;
}
Note that the built-in function allows other alphanumeric characters that are not included in your dict
string. If this is not desirable, you could plug in your own function for checking the correctness of individual characters.
Upvotes: 1
Reputation: 62532
As an aside, rather that look through dict
each time you could write a function like this:
bool IsValidChar(char c)
{
return char.IsDigit(c) || (c>='a' && c<='z') || (c>='A' && c<='Z');
}
Upvotes: 0