코드 3-4 가독성과 유연성이 훨씬 뛰어난 반복을 이용한 코딩
public static string CapitalizeFirstLetter(string text) {
if (text.Length < 2) {
return text.ToUpper();
}
return Char.ToUpper(text[0]) + text.Substring(1).ToLower();
}
public static string CapitalizeEveryWord(string text) {
var words = text.Split(' ');
for (int n = 0; n < words.Length; n++) {
words[n] = CapitalizeFirstLetter(words[n]);
}
return String.Join(" ", words);
}
public static string FormatFilename(string filename) {
var words = filename.Split(' ');
for (int n = 0; n < words.Length; n++) {
string word = words[n];
if (word.Length < 2) {
words[n] = word.ToUpperInvariant();
} else {
words[n] = Char.ToUpperInvariant(word[0]) +
word.Substring(1).ToLowerInvariant();
}
}
return String.Join("_", words);
}