开发者

Use a Regex to form delimited string for SQL Server

开发者 https://www.devze.com 2022-12-20 15:30 出处:网络
I have a SQL Server database (service-based) with a table (Contact). Instead of having multiple tables, I decided to have one delimited string called Emails. So how co开发者_运维技巧uld I use a Regex

I have a SQL Server database (service-based) with a table (Contact). Instead of having multiple tables, I decided to have one delimited string called Emails. So how co开发者_运维技巧uld I use a Regex and a delimiter to add on to the string.


First of all, you should consider to change your decision to have delimited values instead of an extra table. It may seem simpler at first, but as you have already noticed, it quickly gets painful to work with.

That said, there are some different ways to handle delimited values, but using a regular expression is hardly one of them.

For example:

if (value.Length == 0) {
   value = email;
} else {
   value = value + delimiter + email;
}

Or:

List<string> emails = new List(value.Split(new String[]{ delimiter }));
emails.Add(email);
value = String.Join(delimiter, emails.ToArray());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号