Regex pattern for checking if a string starts with a certain substring?

后端 未结 5 1103
小鲜肉
小鲜肉 2020-12-05 22:50

What\'s the regular expression to check if a string starts with \"mailto\" or \"ftp\" or \"joe\" or...

Now I am using C# and code like this in a big if with many ors

5条回答
  •  鱼传尺愫
    2020-12-05 23:11

    I really recommend using the String.StartsWith method over the Regex.IsMatch if you only plan to check the beginning of a string.

    • Firstly, the regular expression in C# is a language in a language with does not help understanding and code maintenance. Regular expression is a kind of DSL.
    • Secondly, many developers does not understand regular expressions: it is something which is not understandable for many humans.
    • Thirdly, the StartsWith method brings you features to enable culture dependant comparison which regular expressions are not aware of.

    In your case you should use regular expressions only if you plan implementing more complex string comparison in the future.

提交回复
热议问题