developer tip

C #을 사용하여 각 단어의 첫 문자 또는 전체 문자열의 첫 문자를 대문자로 바꾸는 방법은 무엇입니까?

optionbox 2020. 12. 14. 08:05
반응형

C #을 사용하여 각 단어의 첫 문자 또는 전체 문자열의 첫 문자를 대문자로 바꾸는 방법은 무엇입니까?


이를 수행하기 위해 자체 알고리즘을 작성할 수 있지만 C #에서 루비의 인간화 와 동등한 것이 있어야한다고 생각합니다 .

나는 그것을 검색했지만 날짜를 인간화하는 방법 만 찾았습니다.

예 :

  • "Lorem Lipsum Et"를 "Lorem lipsum et"으로 바꾸는 방법
  • "Lorem lipsum et"을 "Lorem Lipsum Et"로 바꾸는 방법

의 주석에서 설명하고있는 바와 같이 @ 미겔의 대답은 , 당신이 사용할 수있는 TextInfo.ToTitleCase.NET 1.1부터 사용할 수있다한다. 다음은 귀하의 예에 해당하는 코드입니다.

string lipsum1 = "Lorem lipsum et";

// Creates a TextInfo based on the "en-US" culture.
TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;

// Changes a string to titlecase.
Console.WriteLine("\"{0}\" to titlecase: {1}", 
                  lipsum1, 
                  textInfo.ToTitleCase( lipsum1 )); 

// Will output: "Lorem lipsum et" to titlecase: Lorem Lipsum Et

"LOREM LIPSUM ET"와 같이 모두 대문자 인 대소 문자를 무시합니다. 두문자어가 텍스트에있는 경우 대소 문자를 처리하기 때문입니다 ( " NAMBLA "가 "nambla"또는 "Nambla"가되지 않도록).

그러나 첫 번째 문자 만 대문자로 표시하려면 여기에 있는 솔루션을 수행 하거나 문자열을 분할하고 목록 의 첫 번째 문자 만 대문자로 표시 할 수 있습니다 .

string lipsum2 = "Lorem Lipsum Et";

string lipsum2lower = textInfo.ToLower(lipsum2);

string[] lipsum2split = lipsum2lower.Split(' ');

bool first = true;

foreach (string s in lipsum2split)
{
    if (first)
    {
        Console.Write("{0} ", textInfo.ToTitleCase(s));
        first = false;
    }
    else
    {
        Console.Write("{0} ", s);
    }
}

// Will output: Lorem lipsum et 

훨씬 깔끔해 보이도록 정규 표현식을 사용하세요.

string s = "the quick brown fox jumps over the lazy dog";
s = Regex.Replace(s, @"(^\w)|(\s\w)", m => m.Value.ToUpper());

또 다른 우아한 해결책이 있습니다.

프로 젯 ToTitleCase정적 클래스 에서 기능 정의

using System.Globalization;

public static string ToTitleCase(this string title)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title.ToLower()); 
}

그런 다음 프로젝트의 어느 곳에서나 문자열 확장처럼 사용하십시오.

"have a good day !".ToTitleCase() // "Have A Good Day !"

첫 번째 문자 만 대문자로 바꾸려면 다음과 같은 유틸리티 방법을 사용하면됩니다.

return string.IsNullOrEmpty(str) 
    ? str
    : str[0].ToUpperInvariant() + str.Substring(1).ToLowerInvariant();

There's also a library method to capitalize the first character of every word:

http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx


CSS technique is ok but only changes the presentation of the string in the browser. A better method is to make the text itself capitalised before sending to browser.

Most of the above implimentations are ok, but none of them address the issue of what happens if you have mixed case words that need to be preserved, or if you want to use true Title Case, for example:

"Where to Study PHd Courses in the USA"

or

"IRS Form UB40a"

Also using CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string) preserves upper case words as in "sports and MLB baseball" which becomes "Sports And MLB Baseball" but if the whole string is put in upper case, then this causes an issue.

So I put together a simple function that allows you to keep the capital and mixed case words and make small words lower case (if they are not at the start and end of the phrase) by including them in a specialCases and lowerCases string arrays:

public static string TitleCase(string value) {
        string titleString = ""; // destination string, this will be returned by function
        if (!String.IsNullOrEmpty(value)) {
            string[] lowerCases = new string[12] { "of", "the", "in", "a", "an", "to", "and", "at", "from", "by", "on", "or"}; // list of lower case words that should only be capitalised at start and end of title
            string[] specialCases = new string[7] { "UK", "USA", "IRS", "UCLA", "PHd", "UB40a", "MSc" }; // list of words that need capitalisation preserved at any point in title
            string[] words = value.ToLower().Split(' ');
            bool wordAdded = false; // flag to confirm whether this word appears in special case list
            int counter = 1;
            foreach (string s in words) {

                // check if word appears in lower case list
                foreach (string lcWord in lowerCases) {
                    if (s.ToLower() == lcWord) {
                        // if lower case word is the first or last word of the title then it still needs capital so skip this bit.
                        if (counter == 0 || counter == words.Length) { break; };
                        titleString += lcWord;
                        wordAdded = true;
                        break;
                    }
                }

                // check if word appears in special case list
                foreach (string scWord in specialCases) {
                    if (s.ToUpper() == scWord.ToUpper()) {
                        titleString += scWord;
                        wordAdded = true;
                        break;
                    }
                }

                if (!wordAdded) { // word does not appear in special cases or lower cases, so capitalise first letter and add to destination string
                    titleString += char.ToUpper(s[0]) + s.Substring(1).ToLower();
                }
                wordAdded = false;

                if (counter < words.Length) {
                    titleString += " "; //dont forget to add spaces back in again!
                }
                counter++;
            }
        }
        return titleString;
    }

This is just a quick and simple method - and can probably be improved a bit if you want to spend more time on it.

if you want to keep the capitalisation of smaller words like "a" and "of" then just remove them from the special cases string array. Different organisations have different rules on capitalisation.

You can see an example of this code in action on this site: Egg Donation London - this site automatically creates breadcrumb trails at the top of the pages by parsing the url eg "/services/uk-egg-bank/introduction" - then each folder name in the trail has hyphens replaced with spaces and capitalises the folder name, so uk-egg-bank becomes UK Egg Bank. (preserving the upper case 'UK')

An extension of this code could be to have a lookup table of acronyms and uppercase/lowercase words in a shared text file, database table or web service so that the list of mixed case words can be maintained from one single place and apply to many different applications that rely on the function.


Far as I know, there's not a way to do that without writing (or cribbing) code. C# nets (ha!) you upper, lower and title (what you have) cases:

http://support.microsoft.com/kb/312890/EN-US/


There is no prebuilt solution for proper linguistic captialization in .NET. What kind of capitialization are you going for? Are you following the Chicago Manual of Style conventions? AMA or MLA? Even plain english sentence capitalization has 1000's of special exceptions for words. I can't speak to what ruby's humanize does, but I imagine it likely doesn't follow linguistic rules of capitalization and instead does something much simpler.

Internally, we encountered this same issue and had to write a fairly large amount code just to handle proper (in our little world) casing of article titles, not even accounting for sentence capitalization. And it indeed does get "fuzzy" :)

It really depends on what you need - why are you trying to convert the sentences to proper capitalization (and in what context)?


All the examples seem to make the other characters lowered first which isn't what I needed.

customerName = CustomerName <-- Which is what I wanted

this is an example = This Is An Example

public static string ToUpperEveryWord(this string s)
{
    // Check for empty string.  
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }

    var words = s.Split(' ');

    var t = "";
    foreach (var word in words)
    {
        t += char.ToUpper(word[0]) + word.Substring(1) + ' ';
    }
    return t.Trim();
}

참고URL : https://stackoverflow.com/questions/913090/how-to-capitalize-the-first-character-of-each-word-or-the-first-character-of-a

반응형