Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String contains only a given set of characters

Tags:

I need to know if a given string is a valid DateTime format string because the string may represent other things. I tried DateTime.ParseExact(somedate.ToString(format), format) thinking it would barf on an invalid format, but it doesn't.

So I'm good with simply testing if the string contains only "yYmMdDsShH" characters. Something like std::string.find_first_not_of would work, but System.String doesn't have this.

I thought that RegEx might do the trick, but I'm very weak with regular expressions.

Note that Linq is not available for this one (.NET 2.0 only).

Updated

To clarify, I need to know if a given string represents a date time format and not something else like this:

if (input == "some special value") ... // it's a special case value else if (Environment.GetEnvironmentVariable(input)) ... // it's an environment variable name else if (IsDateTimeFormatString(input)) ... // it's a date time format string else if (input.IndexOfAny(Path.GetInvalidPathChars()) < 0) ... // it's a file path else    throw new Exception(); // Not a valid input 

I can restrict a DateTime format string to only "yYmMdDsShH", or I can add a few separator characters into it as well, it's up to me what to allow or not allow.

like image 896
Tergiver Avatar asked Jul 20 '10 18:07

Tergiver


People also ask

How do you make sure a string only contains certain characters?

How to check if a string contains only certain characters? Create a set (or a string) containing the allowed characters. Iterate through the characters in the string and use the membership operator "in" to check if the character is in the allowed set of characters.

How do you check if a string contains a set of characters in Python?

Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

How do you check if a string contains a set of characters in Java?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise.

How do you check if a string contains a character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.


1 Answers

With .NET2, you need to roll your own check for this. For example, the following method uses a foreach to check:

bool FormatValid(string format) {     string allowableLetters = "yYmMdDsShH";      foreach(char c in format)     {          // This is using String.Contains for .NET 2 compat.,          //   hence the requirement for ToString()          if (!allowableLetters.Contains(c.ToString()))               return false;     }      return true; } 

If you had the option of using .NET 3.5 and LINQ, you could use Enumerable.Contains to work with characters directly, and Enumerable.All. This would simplify the above to:

bool valid = format.All(c => "yYmMdDsShH".Contains(c)); 
like image 192
Reed Copsey Avatar answered Oct 07 '22 00:10

Reed Copsey