使用这个问题作为基础是否有算法或编码将某些文本更改为 Pascal 或 Camel 大小写的示例。
例如:
mynameisfred
变成
Camel: myNameIsFred
Pascal: MyNameIsFred
Using this question as the base is there an alogrithm or coding example to change some text to Pascal or Camel casing.
For example:
mynameisfred
becomes
Camel: myNameIsFred
Pascal: MyNameIsFred
发布评论
评论(2)
唯一的方法是通过字典来运行单词的每个部分。
“mynameisfred”只是一个字符数组,将其分成“我的名字是弗雷德”意味着理解每个字符的连接意味着什么。
如果您的输入以某种方式分开,例如“我的名字是fred”或“my_name_is_fred”,您可以轻松做到这一点。
The only way to do that would be to run each section of the word through a dictionary.
"mynameisfred" is just an array of characters, splitting it up into my Name Is Fred means understanding what the joining of each of those characters means.
You could do it easily if your input was separated in some way, e.g. "my name is fred" or "my_name_is_fred".
我在 http://www 上发现了一个帖子,里面有一群 Perl 人在争论这个问题。 .perlmonks.org/?node_id=336331。
我希望这不是对这个问题的过多回答,但我想说你有一个问题,因为这将是一个非常开放式的算法,也可能有很多“失误”作为点击。 例如,假设您输入:-
输出可能是:-
或:-
算法无法知道更喜欢哪个。 您可以添加一些额外的代码来指定您更喜欢更常见的单词,但再次会发生遗漏(Peter Norvig 在 可能在算法方面有所帮助,我写了一个 C# 实现 if C# 是您的语言)。
我同意马克的观点,并说你最好有一个接受分隔输入的算法,即 this_is_a_test 并将其转换。 这很容易实现,即用伪代码: -
如果您愿意,您也可以用适当的大小写算法替换我的 CapitaliseFirstLetter() 函数。
上述算法的 AC# 实现如下(带有测试工具的完整控制台程序):-
I found a thread with a bunch of Perl guys arguing the toss on this question over at http://www.perlmonks.org/?node_id=336331.
I hope this isn't too much of a non-answer to the question, but I would say you have a bit of a problem in that it would be a very open-ended algorithm which could have a lot of 'misses' as well as hits. For example, say you inputted:-
The output could be:-
Or:-
There's no way the algorithm would know which to prefer. You could add some extra code to specify that you'd prefer more common words, but again misses would occur (Peter Norvig wrote a very small spelling corrector over at http://norvig.com/spell-correct.html which might help algorithm-wise, I wrote a C# implementation if C#'s your language).
I'd agree with Mark and say you'd be better off having an algorithm that takes a delimited input, i.e. this_is_a_test and converts that. That'd be simple to implement, i.e. in pseudocode:-
You could also replace my capitaliseFirstLetter() function with a proper case algorithm if you so wished.
A C# implementation of the above described algorithm is as follows (complete console program with test harness):-