问题描述
pretty的基础,我只是好奇别人怎么可能实现这个算法,想看看是否有任何聪明的技巧来优化算法......我只是实现这个的,我是一个项目在努力。
Pretty basic, I'm just curious how others might implement this algorithm and would like to see if there are any clever tricks to optimize the algorithm...I just had to implement this for a project that I am working on.
由于字符串首字母大写,你会如何去spacifying吗?
Given a string in CamelCase, how would you go about "spacifying" it?
例如。给定的 FooBarGork 我要的富酒吧Gork 返回。
e.g. given FooBarGork I want Foo Bar Gork back.
下面是我在C#中的算法:
Here is my algorithm in C#:
static void Main(string[] args)
{
Console.WriteLine(UnCamelCase("FooBarGork"));
}
public static string UnCamelCase(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (char.IsUpper(str, i) && i > 0) sb.Append(" ");
sb.Append(str[i]);
}
return sb.ToString();
}
既然你要访问的每一个字符一次,我认为最好的情况是O(n)。你将如何实现这一点?
Since you have to visit every character once, I believe the best case is O(n). How would you implement this?
推荐答案
我已经可以感觉到火焰,但我喜欢的正则表达式这种东西。
I can already sense the flames, but I like regex for this kind of stuff.
public static string UnCamelCase(string str)
{
return Regex.Replace(str, "([a-z])([A-Z])", "$1 $2");
}
(这可能不是比你更快地实现,但要的我的更清楚了。)
很显然,这将是更快(在运行时)
And obviously, this would be even faster (at runtime)
private static Regex _unCamelRegex = new Regex("([a-z])([A-Z])", RegexOptions.Compiled);
public static string UnCamelCase(string str)
{
return _unCamelRegex.Replace(str, "$1 $2");
}
这将处理由Pete Kirkham的below (据骆驼套管串状HTT prequest):
This would handle the issue brought up by Pete Kirkham below (as far as camel-cased strings like HTTPRequest):
private static Regex _unCamelRegex1 = new Regex("([a-z])([A-Z])", RegexOptions.Compiled);
private static Regex _unCamelRegex2 = new Regex("([A-Z]+)([A-Z])([a-z])", RegexOptions.Compiled);
public static string UnCamelCase(string str)
{
return _unCamelRegex2.Replace(_unCamelRegex1.Replace(str, "$1 $2"), "$1 $2$3");
}
这其中需要 HTT prequestFOOBarGork
并返回 HTTP请求FOO酒吧Gork
所以,我测试过正规的前pression方法,迭代法使用有机磷农药的实现(与'从1开始,跳过> 0检查变化)和我的第二个答复(一个与静态编译的正则表达式目的)。请注意,结果不包括正则表达式的编译时间。 2万个电话(使用相同的FooBarGork输入):
So I tested the iterative method against the regular expression method using the OPs implementation (with the 'start at 1 and skip the > 0 check' change) and my second reply (the one with the static compiled Regex object). Note that the results do not include the compilation time of the Regex. For 2 million calls (using the same FooBarGork input):
迭代: 00:00:00.80
正则表达式: 00:00:06.71
Iterative: 00:00:00.80
Regex: 00:00:06.71
因此,显而易见的是,迭代的方法是更有效。我已经包含的有机磷农药实行一个固定的版本(as由Jason Punyon建议,任何信贷应该去他)还考虑到空或空参数:
So it is obvious that the iterative approach is much more efficient. I've included a fixed version of the OPs implementation (as suggested by Jason Punyon, any credit should go to him) that also takes into account a null or empty argument:
public static string UnCamelCaseIterative(string str)
{
if (String.IsNullOrEmpty(str))
return str;
/* Note that the .ToString() is required, otherwise the char is implicitly
* converted to an integer and the wrong overloaded ctor is used */
StringBuilder sb = new StringBuilder(str[0].ToString());
for (int i = 1; i < str.Length; i++)
{
if (char.IsUpper(str, i))
sb.Append(" ");
sb.Append(str[i]);
}
return sb.ToString();
}
这篇关于一种算法&QUOT; spacify&QUOT;驼峰格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!