2008-08-28 22 views

Trả lời

3

Tôi đã tìm thấy một chuỗi với một nhóm người Perl tranh cãi về câu hỏi này tại số http://www.perlmonks.org/?node_id=336331.

Tôi hy vọng đây không phải là quá nhiều câu trả lời cho câu hỏi, nhưng tôi cho rằng bạn có một chút vấn đề ở chỗ nó sẽ là một thuật toán rất cởi mở có thể có nhiều ' nhớ 'cũng như lượt truy cập. Ví dụ, nói rằng bạn đầu vào: -

camelCase("hithisisatest"); 

Kết quả có thể là: -

"hiThisIsATest" 

Hoặc: -

"hitHisIsATest" 

Không có cách nào các thuật toán sẽ biết để thích. Bạn có thể thêm một số mã bổ sung để xác định rằng bạn muốn các từ phổ biến hơn, nhưng một lần nữa sẽ xảy ra (Peter Norvig đã viết một sửa lỗi chính tả rất nhỏ tại http://norvig.com/spell-correct.htmlcó thể trợ giúp thuật toán khôn ngoan, tôi đã viết C# implementation nếu C# của bạn ngôn ngữ).

Tôi đồng ý với Mark và nói rằng bạn nên sử dụng thuật toán có đầu vào được phân tách, tức là this_is_a_test và chuyển đổi điều đó. Điều đó muốn được đơn giản để thực hiện, ví dụ: trong giả: -

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)] 

Bạn cũng có thể thay thế chức năng của tôi capitaliseFirstLetter() với một thuật toán trường hợp thích hợp nếu bạn rất muốn.

Một C# thực hiện các thuật toán mô tả ở trên là như sau (chương trình giao diện điều khiển hoàn chỉnh với khai thác thử nghiệm): -

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); 
    } 
} 
0

Cách duy nhất để làm điều đó sẽ được chạy mỗi phần của từ thông qua một cuốn từ điển.

"mynameisfred" chỉ là một mảng ký tự, chia nhỏ nó thành Tên của tôi Fred có nghĩa là hiểu được sự tham gia của từng nhân vật đó có ý nghĩa gì.

Bạn có thể làm điều đó dễ dàng nếu đầu vào của bạn được phân tách theo một cách nào đó, ví dụ: "tên tôi là fred" hoặc "my_name_is_fred".

Các vấn đề liên quan