I know it's answered, but I think the whole converting to char array is not needed.
You can simplify it further to (based on original code):
// SNString?.Length == 16 -> Should be exactly 16 characters
// SNString? == '-' -> the first dash should be in index number 3
// SNString? == '-' -> the first dash should be in index number 12
// SNString?.Replace("-", "")?.Length == 14 -> if there is only 2 dashes, then removing them would result with length == 14, otherwise is false
return SNString?.Length == 16 && SNString? == '-' && SNString? == '-' && SNString?.Replace("-", "")?.Length == 14;
which is equivalent to :
if(SNString?.Length == 16)
if (SNString? == '-' && SNString? == '-' && SNString?.Replace("-", "")?.Length == 14)
You should check the length of the provided string, then apply the other validations.
The string length will determine if you need to apply the other validations or not.
if the string's length is not equal to 16, then surely it's not a valid format.
if the string length == 16, then it's a valid format which needs to be validated.
Then you just get the position of the first and last dashes. if they're in the correct position,
then the first, second, and third parts should have the correct length. Then, you can use
Replace to replace all dashes with empty string, and check the result length, if the result is 14, then there were only two dashes, which is valid, otherwise is not valid.
Now, you can validate three parts
XXX-XXXXXXXX-XXX, if your characters group are all numbers (without the dashes),
then you can add something like
ulong.TryParse(SNString?.Replace("-", string.Empty), out ulong result), if true, then they're all numbers, otherwise it's false.
Doing that, would avoid converting it to char array.
There is one thing that you should consider, if there is certain format for each characters group, you might need to use
For instance, if the first part should always contains letters only, or one digit with two letters ..etc.
then the current validation process will provide a non-valid string. Also, you might consider adding a validation of English letters and Digits to avoid having some non-English inputs.
here is Regex example (for future reference) :
With this, you'll have the flexablity to adjust the regex pattern to match the conditions you need.
Splitreturns an array with exactly four elements? If you already know that there are exactly three hyphens in the string, then whatever code ensures that invariant should ensure the other invariants are met. If you do not already know that, then your program can crash. Either way, something seems wrong here. \$\endgroup\$