walter
walter

Reputation: 823

Validate string to have no space char and only first and last char as delimeter

I need validation for string to comply with next:

How this can be done?

Updated sorry missed that should only be one delimiter char at start and at the end

Upvotes: 0

Views: 546

Answers (2)

Ral Zarek
Ral Zarek

Reputation: 1076

If I understood your question right, this should do:

char delimiter = ...  
string delimiterString = delimiter.ToString();
string s = ...
bool right = !s.Contains(' ') 
  && s.StartsWith(delimiterString) 
  && s.EndsWith(delimiterString)
  && !s.Substring(1,s.Length-2).Contains(delimiter);

Upvotes: 1

Wolf5370
Wolf5370

Reputation: 1374

Use regex...Assuming # as delimeter and zero characrters between as acceptable.

Regex rx=new Regex("^#+[^\s]*#$");

return rx.IsMatch(teststring);

Upvotes: 1

Related Questions