The task is to create a program consisting of a textBox and a button. In the textBox, any words are entered in any register, by clicking on the button, the textBox with the corrected register is written to the content file. Each new word begins with a capital letter, all other letters are small.

Example

Entry: PRIVET, LINE NAME MARINA.


Conclusion: Hi, My Name is Marina.

With the file, everything is clear, I know how to create it and record it. Please tell us how to make such a register correction or give an example. I will be very grateful.

    5 answers 5

    As it turned out during the search, there is a ready-made method System.Globalization.TextInfo.ToTitleCase , which brings the text to the desired form. One problem, the words consisting entirely of capital letters he perceives as abbreviations and skips.

    Generally, the title casing converts to the lowercase. However, this method is not currently uppercase, such as an acronym.

    To work around, you can reduce the text to lower case, then call this method:

     //директива using using System.Globalization; ... var text = "пРиВеТ, мЕНя ЗОвут МАРИНА."; //получаем TextInfo для русского языка var textInfo = new CultureInfo("ru-RU").TextInfo; //преобразуем текст var capitalizedText = textInfo.ToTitleCase(textInfo.ToLower(text)); Console.WriteLine(capitalizedText); 

    We get:

    Hi, My Name is Marina.

    Please note that:

    • abbreviations will be converted: "FSB" to turn into "FSB";
    • word boundaries are determined by non-letter characters: “somewhere” become “Somewhere”;
    • capitalization rules depend on locale. In the example above, the ru-RU locale is used. If it is necessary to take into account the capitalization rules for other languages ​​/ regions, changes in the initialization of TextInfo will be required.

    The documentation also states that in future versions of .Net, support for linguistically correct capitalization may be added to the method:

    It is somewhat simpler and faster. Slower in the future.

    The behavior of the method for the Russian locale should not change significantly, but adding additional rules may adversely affect performance.

    Other options in the same question in English: Capitalizing words in a string using c # .

    • 2
      hmm, and whether it will work differently on different PCs: var currentTextInfo = Thread.CurrentThread.CurrentCulture.TextInfo; ? Maybe you need to use one strictly defined culture, such as var currentTextInfo = new CultureInfo("ru-ru").TextInfo; ? - Andrei NOP
    • @AndreyNOP Yes, it can still react to a change in locale. I'll think about it a bit and tell it in the answer, thanks for the valuable remark! - default locale

    For example:

     static string CapitalizeAllWords(string s) { var sb = new StringBuilder(s.Length); bool inWord = false; foreach(var c in s) { if (char.IsLetter(c)) { sb.Append(inWord ? char.ToLower(c) : char.ToUpper(c)); inWord = true; } else { sb.Append(c); inWord = false; } } return sb.ToString(); } 

    Example of use:

     Console.WriteLine(CapitalizeAllWords("пРиВеТ, мЕНя ЗОвут МАРИНА.")); 

    The same in linq, as we all love:

     static string CapitalizeAllWords(string s) { bool inWord = false; return new string( s.Select(c => ( c: char.IsLetter(c) ? (inWord ? char.ToLower(c) : char.ToUpper(c)) : c, inWord = char.IsLetter(c)).c) .ToArray()); } 

    This option, of course, comic, and do not use it

    • one
      For greater joy, the link can be in one line: new String(s.Select((c,i) => Char.IsLetter(c)&&(i==0||!Char.IsLetter(s[i-1])) ? Char.ToUpper(c) : Char.ToLower(c)).ToArray()); - default locale
    • one
      Just need to specify the locale. And then what a big letter, and what does not, depends on the locale. - VladD

    As an option, you can try

     string TextBoxText = "hello world my name is hitler"; var result = TextBoxText.Split(' ').ToList(); var t = new List<string>(); result.ForEach(g => { t.Add(g.Replace(g[0].ToString(), g[0].ToString().ToUpper())); }); 

    How would it work ok

    • just an alias - doesn't seem to work? - Qwertiy
     var text = "привет тест. ВОТ так Получилось.забыли пробел. нет точки"; text = text.ToLower(); var firstQuery = Regex.Replace(text, @"(^\w)|(\.\s\w)|(\.\w)", m => m.Value.ToUpper()); var secondQuery = Regex.Replace(text, @"(^\w)|(\.\s\w)|(\.\w)|(\s\w)", m => m.Value.ToUpper()); //Вывод текста где первый символ в предложении с большой буквы. Даже если нет пробелов после точки. Console.WriteLine(firstQuery); //Вывод текста где все слова в предложении с большой буквы. Даже если нет пробелов после точки. Console.WriteLine(secondQuery); 

      It is better to use StringBuilder, since you have to change the string.

       StringBuilder s = new StringBuilder("пРиВеТ, мЕНя ЗОвут МАРИНА."); s[0] = char.ToUpper(s[0]); //Приведение к верхнему регистру s[1] = char.ToLower(s[1]); // Приведение к нижнему регистру 

      The result will be -> "WELCOME, MY VENUE MARINA."