patterncsharpModerate
Validate that a given string (name) meets certain requirements
Viewed 0 times
validaterequirementsmeetsnamethatcertainstringgiven
Problem
String Name = "yyyy MM DD abjwg kelk.exe"
public static bool IsNameRight(string Name)
{
string[] temp = Name.Split(' ');
string[] de= Name.Split('.');
if (de[1] == "pdf" && temp .Length == 5)
{
if (temp [0].Length == 4
&& temp [1].Length == 2
&& temp [3].Length == 2
&& temp [4] == "abjwg"
&& temp [5] == "kelk.exe")
return true;
else
return false;
}
return false;
}I am using .NET 3.5 and C#
EDIT
You could use Regular expressions but I have never used it or know how to use it, and don't know if it is a good practice.
Solution
As you wanted to use RegEx I have a recommendation for you:
public static bool IsValidName(string Name)
{
if(String.IsNullOrEmpty(Name))
return new ArgumentNullException();
string pattern = @"^[0-9]{4} [0-9]{2} [0-9]{2} abjwg kelk\\.exe$";
return Regex.IsMatch(Name, pattern);
}Code Snippets
public static bool IsValidName(string Name)
{
if(String.IsNullOrEmpty(Name))
return new ArgumentNullException();
string pattern = @"^[0-9]{4} [0-9]{2} [0-9]{2} abjwg kelk\\.exe$";
return Regex.IsMatch(Name, pattern);
}Context
StackExchange Code Review Q#17949, answer score: 15
Revisions (0)
No revisions yet.