将文本格式化为 Pascal 或驼峰大小写的算法
-
09-06-2019 - |
题
使用这个问题 作为基础,有一个算法或编码示例将某些文本更改为 Pascal 或 Camel 大小写。
例如:
mynameisfred
变成
Camel: myNameIsFred
Pascal: MyNameIsFred
解决方案
我发现了一个帖子,里面有一群 Perl 人在争论这个问题 http://www.perlmonks.org/?node_id=336331.
我希望这不是对这个问题的过多回答,但我想说你有一个问题,因为这将是一个非常开放式的算法,也可能有很多“失误”作为点击。例如,假设您输入:-
camelCase("hithisisatest");
输出可能是:-
"hiThisIsATest"
或者:-
"hitHisIsATest"
算法不可能知道该选择哪个。您可以添加一些额外的代码来指定您更喜欢更常见的单词,但再次会发生遗漏(Peter Norvig 在 http://norvig.com/spell- Correct.html 哪个 可能 帮助算法方面,我写了一个 C# 实现 如果 C# 是您的语言)。
我同意马克的观点,并说你最好有一个接受分隔输入的算法,即this_is_a_test 并将其转换。这很容易实现,即伪代码:-
SetPhraseCase(phrase, CamelOrPascal):
if no delimiters
if camelCase
return lowerFirstLetter(phrase)
else
return capitaliseFirstLetter(phrase)
words = splitOnDelimiter(phrase)
if camelCase
ret = lowerFirstLetter(first word)
else
ret = capitaliseFirstLetter(first word)
for i in 2 to len(words): ret += capitaliseFirstLetter(words[i])
return ret
capitaliseFirstLetter(word):
if len(word) <= 1 return upper(word)
return upper(word[0]) + word[1..len(word)]
lowerFirstLetter(word):
if len(word) <= 1 return lower(word)
return lower(word[0]) + word[1..len(word)]
如果您愿意,您也可以用适当的大小写算法替换我的 CapitaliseFirstLetter() 函数。
上述算法的 C# 实现如下(带有测试工具的完整控制台程序):-
using System;
class Program {
static void Main(string[] args) {
var caseAlgorithm = new CaseAlgorithm('_');
while (true) {
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) return;
Console.WriteLine("Input '{0}' in camel case: '{1}', pascal case: '{2}'",
input,
caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.CamelCase),
caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.PascalCase));
}
}
}
public class CaseAlgorithm {
public enum CaseMode { PascalCase, CamelCase }
private char delimiterChar;
public CaseAlgorithm(char inDelimiterChar) {
delimiterChar = inDelimiterChar;
}
public string SetPhraseCase(string phrase, CaseMode caseMode) {
// You might want to do some sanity checks here like making sure
// there's no invalid characters, etc.
if (string.IsNullOrEmpty(phrase)) return phrase;
// .Split() will simply return a string[] of size 1 if no delimiter present so
// no need to explicitly check this.
var words = phrase.Split(delimiterChar);
// Set first word accordingly.
string ret = setWordCase(words[0], caseMode);
// If there are other words, set them all to pascal case.
if (words.Length > 1) {
for (int i = 1; i < words.Length; ++i)
ret += setWordCase(words[i], CaseMode.PascalCase);
}
return ret;
}
private string setWordCase(string word, CaseMode caseMode) {
switch (caseMode) {
case CaseMode.CamelCase:
return lowerFirstLetter(word);
case CaseMode.PascalCase:
return capitaliseFirstLetter(word);
default:
throw new NotImplementedException(
string.Format("Case mode '{0}' is not recognised.", caseMode.ToString()));
}
}
private string lowerFirstLetter(string word) {
return char.ToLower(word[0]) + word.Substring(1);
}
private string capitaliseFirstLetter(string word) {
return char.ToUpper(word[0]) + word.Substring(1);
}
}
其他提示
唯一的方法是通过字典来运行单词的每个部分。
“mynameisfred”只是一个字符数组,将其分成“我的名字是弗雷德”意味着理解每个字符的连接意味着什么。
如果您的输入以某种方式分开,您可以轻松做到这一点,例如“我的名字是弗雷德”或“我的名字是弗雷德”。
不隶属于 StackOverflow