简体   繁体   中英

How to break strings into chars that are in upper case in c#

I was wondering if it is possible to break a string at the upper letters. For example:

I have the value "ProductName" and I want to show "Product Name". Is there a way to do so?

Thanks!

No need in third party library!

If the performance is not crucial use

string addSpaces(string input)
{
    return Regex.Replace(input, @"\w", m => char.IsUpper(m.Value[0]) ? ' ' + m.Value : m.Value).Trim();
}

don't forget

using System.Text.RegularExpressions;

If use of third party library is acceptable, look at Humanizer project. https://github.com/MehdiK/Humanizer#humanize-string

Depending on your need, you may find many other things of use.. The one above is certainly covered, in a nice generally useful package.

编辑:您可以复制字符串,然后用[AZ] .\\1 [AZ][AZ]进行正则表达式替换(在其前面加一个点)并执行split('.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM