Personal Experience with Umbraco Tags Processing
I have strong experience with publisher websites where tags in articles were important in those days. Therefore, most of the split operations I had to perform were related to comma-separated tags in the Umbraco. I had been playing with tags mostly in Umbraco 4, 6, 7, and 8, so I was forced to use the .NET Framework platform.
Currently, whenever I have to split a string I'm aware that it would be nice to trim the results to have clean items.
The Standard Approach to Splitting and Trimming Tags
Basically, I split tags and trim them as below:
string commaSeparatedTags = "Umbraco, .NET, MS SQL";
var tags = commaSeparatedTags.Split(',', StringSplitOptions.RemoveEmptyEntries);
var trimmedTags = tags.Select(t => t.Trim());
It's worth to mention that I always use the StringSplitOptions.RemoveEmptyEntries flag in case of leading or trailing comma in the string as below:
string commaSeparatedTags = "Umbraco, .NET, MS SQL,";
Discovering the StringSplitOptions.TrimEntries Flag
A few weeks ago, I had to deal with tags during content migration from obsolete CMS to Umbraco.
I have noticed super cool StringSplitOptions.TrimEntries flag which can be used as another option next to StringSplitOptions.RemoveEmptyEntries.
I was very happy that I could split and trim strings at once as follows:
var tags = commaSeparatedTags.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
I decided to check when this super cool trimming feature was introduced and I was shocked that it's available since .NET 5.0 (for 4 years).
Trim white-space characters from each substring in the result. This field is available in .NET 5 and later versions only.
Probably, I have most likely used the split method many times since the trimming feature was released, but I haven't noticed it.
I guess, I didn't expect that such a feature could be introduced.
BTW, when a clean, comma-separated list of tags ready to store as a tags property is needed, then we can use the Join string extension method.
string cleanCommaSeparatedTags = string.Join(',', trimmedTags);
Please be aware that in the CSV storage type of Umbraco.Tags editor, tags are separated only by a comma, not a comma and white-space.
Conclusion
The .NET 5.0 feature, StringSplitOptions.TrimEntries, is a powerful tool for optimizing tag management in Umbraco projects.
With this feature you can streamline the process of splitting and trimming Umbraco tags, making it significantly more efficient and reducing the manual effort required.
🌐 Explore More: Interested in learning about Umbraco and web development insights?
Explore our blog for a wealth of information and expert advice.