前大写字母添加空格 [英] Add spaces before Capital Letters

查看:171
本文介绍了前大写字母添加空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于字符串ThisStringHasNoSpacesButItDoesHaveCapitals什么是大写字母之前添加空间的最佳方式。因此最终的字符串将是这个字符串没有空格但它确实有资本

下面是我尝试用正则表达式

  System.Text.RegularEx pressions.Regex.Replace(价值[A-Z],$ 0)


解决方案

的正则表达式将正常工作(我连投了马丁·布朗的答案),但它们很昂贵(而且我个人认为任何图案长于几个字符登天钝)

此功能

 字符串AddSpacesToSentence(字符串文本,布尔preserveAcronyms)
{
        如果(string.IsNullOrWhiteSpace(文本))
           返回的String.Empty;
        StringBuilder的newText =新的StringBuilder(text.Length * 2);
        newText.Append(文[0]);
        的for(int i = 1; I< text.Length;我++)
        {
            如果(char.IsUpper(文[I]))
                如果((文[I - 1] =''和;!&放大器; char.IsUpper(文[I - 1]))||
                    (preserveAcronyms&放大器;&安培; char.IsUpper(文[I - 1])及和放大器;
                     I< text.Length - 1和;&放大器; !char.IsUpper(文[I + 1])))
                    newText.Append('');
            newText.Append(文[I]);
        }
        返回newText.ToString();
}

会做10万次的2968750蜱,正则表达式将25,000,000蜱(和多数民众赞成编译正则表达式)。

这是更好的,对于一个给定值更好(即更快),但是它更code来维持。 更好往往是相互竞争的要求妥协。

希望这有助于:)

更新结果
这是一个好长好长一段时间,因为我看着这一点,我才意识到时序尚未更新,因为code改变(只改变一点点)。

在与'Abbbbbbbbb字符串重复100次(即1000字节),100,000转换运行需要手codeD功能4517177蜱,和下面的正则表达式采取59435719使得手codeD功能运行在时间的7.6%,它需要的正则表达式。

更新2
这将需要缩略语考虑?它会吧!
中频说明书的逻辑是相当模糊的,因为你可以看到它扩展到这...

 如果(char.IsUpper(文[I]))
    如果(char.IsUpper(文[I - 1]))
        如果(preserveAcronyms&放大器;&安培; I< text.Length - 1安培;!&安培; char.IsUpper(文[I + 1]))
            newText.Append('');
        别的;
    否则,如果(文[I - 1]!='')
        newText.Append('');

...无助了!

下面是原来的简单的方法不担心缩写

 字符串AddSpacesToSentence(字符串文本)
{
        如果(string.IsNullOrWhiteSpace(文本))
           返回;
        StringBuilder的newText =新的StringBuilder(text.Length * 2);
        newText.Append(文[0]);
        的for(int i = 1; I< text.Length;我++)
        {
            如果(char.IsUpper(文[I])及和放大器;!文本[我 - 1] ='')
                newText.Append('');
            newText.Append(文[I]);
        }
        返回newText.ToString();
}

Given the string "ThisStringHasNoSpacesButItDoesHaveCapitals" what is the best way to add spaces before the capital letters. So the end string would be "This String Has No Spaces But It Does Have Capitals"

Here is my attempt with a RegEx

System.Text.RegularExpressions.Regex.Replace(value, "[A-Z]", " $0")

解决方案

The regexes will work fine (I even voted up Martin Browns answer), but they are expensive (and personally I find any pattern longer than a couple of characters prohibitively obtuse)

This function

string AddSpacesToSentence(string text, bool preserveAcronyms)
{
        if (string.IsNullOrWhiteSpace(text))
           return string.Empty;
        StringBuilder newText = new StringBuilder(text.Length * 2);
        newText.Append(text[0]);
        for (int i = 1; i < text.Length; i++)
        {
            if (char.IsUpper(text[i]))
                if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
                    (preserveAcronyms && char.IsUpper(text[i - 1]) && 
                     i < text.Length - 1 && !char.IsUpper(text[i + 1])))
                    newText.Append(' ');
            newText.Append(text[i]);
        }
        return newText.ToString();
}

Will do it 100,000 times in 2,968,750 ticks, the regex will take 25,000,000 ticks (and thats with the regex compiled).

It's better, for a given value of better (i.e. faster) however it's more code to maintain. "Better" is often compromise of competing requirements.

Hope this helps :)

Update
It's a good long while since I looked at this, and I just realised the timings haven't been updated since the code changed (it only changed a little).

On a string with 'Abbbbbbbbb' repeated 100 times (i.e. 1,000 bytes), a run of 100,000 conversions takes the hand coded function 4,517,177 ticks, and the Regex below takes 59,435,719 making the Hand coded function run in 7.6% of the time it takes the Regex.

Update 2 Will it take Acronyms into account? It will now! The logic of the if statment is fairly obscure, as you can see expanding it to this ...

if (char.IsUpper(text[i]))
    if (char.IsUpper(text[i - 1]))
        if (preserveAcronyms && i < text.Length - 1 && !char.IsUpper(text[i + 1]))
            newText.Append(' ');
        else ;
    else if (text[i - 1] != ' ')
        newText.Append(' ');

... doesn't help at all!

Here's the original simple method that doesn't worry about Acronyms

string AddSpacesToSentence(string text)
{
        if (string.IsNullOrWhiteSpace(text))
           return "";
        StringBuilder newText = new StringBuilder(text.Length * 2);
        newText.Append(text[0]);
        for (int i = 1; i < text.Length; i++)
        {
            if (char.IsUpper(text[i]) && text[i - 1] != ' ')
                newText.Append(' ');
            newText.Append(text[i]);
        }
        return newText.ToString();
}

这篇关于前大写字母添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆