Programing

C #에서 ToUpper ()와 ToUpperInvariant ()의 차이점은 무엇입니까?

crosscheck 2020. 7. 14. 08:26
반응형

C #에서 ToUpper ()와 ToUpperInvariant ()의 차이점은 무엇입니까?


C #에서 ToUpper()ToUpperInvariant()? 의 차이점은 무엇 입니까?

결과가 다른 예를들 수 있습니까?


ToUpper현재 문화를 사용합니다. ToUpperInvariant변하지 않는 문화를 사용합니다.

표준 예는 터키이며, "i"의 대문자는 "I"가 아닙니다.

차이점을 보여주는 샘플 코드 :

using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;

public class Test
{
    [STAThread]
    static void Main()
    {
        string invariant = "iii".ToUpperInvariant();
        CultureInfo turkey = new CultureInfo("tr-TR");
        Thread.CurrentThread.CurrentCulture = turkey;
        string cultured = "iii".ToUpper();

        Font bigFont = new Font("Arial", 40);
        Form f = new Form {
            Controls = {
                new Label { Text = invariant, Location = new Point(20, 20),
                            Font = bigFont, AutoSize = true},
                new Label { Text = cultured, Location = new Point(20, 100),
                            Font = bigFont, AutoSize = true }
            }
        };        
        Application.Run(f);
    }
}

터키어에 대한 자세한 내용은이 터키 테스트 블로그 게시물을 참조하십시오 .

생략 된 문자 등에 대한 다양한 대문자 문제가 있다는 사실에 놀라지 않을 것입니다. 이것은 제가 머리 꼭대기에서 아는 한 가지 예일뿐입니다. -케이싱하고 "MAIL"과 비교합니다. 터키에서는 그다지 효과가 없었습니다 ...


존의 대답은 완벽합니다. 방금 ToUpperInvariant을 호출하는 것과 동일 하게 추가하고 싶었습니다 ToUpper(CultureInfo.InvariantCulture).

따라서 Jon의 예제가 조금 더 단순 해집니다.

using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;

public class Test
{
    [STAThread]
    static void Main()
    {
        string invariant = "iii".ToUpper(CultureInfo.InvariantCulture);
        string cultured = "iii".ToUpper(new CultureInfo("tr-TR"));

        Application.Run(new Form {
            Font = new Font("Times New Roman", 40),
            Controls = { 
                new Label { Text = invariant, Location = new Point(20, 20), AutoSize = true }, 
                new Label { Text = cultured, Location = new Point(20, 100), AutoSize = true }, 
            }
        });
    }
}

나는 또한 더 멋진 글꼴이기 때문에 New Times Roman을 사용 했습니다.

속성이 상속 되기 때문에 컨트롤 대신 FormFont속성을 설정했습니다 .LabelFont

그리고 소형 (예 : 생산이 아닌) 코드를 좋아하기 때문에 다른 몇 줄을 줄였습니다.

나는 지금 당장 할 일이 낫지 않았다.


MSDN으로 시작

http://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx

ToUpperInvariant 메서드는 ToUpper (CultureInfo.InvariantCulture)와 같습니다.

대문자 i영어로 '나'라고 해서 항상 그렇게하는 것은 아닙니다.


String.ToUpper and String.ToLower can give different results given different cultures. The most known example is the Turkish example, for which converting lowercase latin "i" to uppercase, doesn't result in a capitalized latin "I", but in the Turkish "I".

Capitalization of I depending on culture, upper row - lower case letters, lower row - upper case letters

As for me it was confusing even with the above picture (source), I wrote a program (see source code below) to see the exact output for the Turkish example:

# Lowercase letters
Character              | UpperInvariant | UpperTurkish | LowerInvariant | LowerTurkish
English i - i (\u0069) | I (\u0049)     | I (\u0130)   | i (\u0069)     | i (\u0069)
Turkish i - ı (\u0131) | ı (\u0131)     | I (\u0049)   | ı (\u0131)     | ı (\u0131)

# Uppercase letters
Character              | UpperInvariant | UpperTurkish | LowerInvariant | LowerTurkish
English i - I (\u0049) | I (\u0049)     | I (\u0049)   | i (\u0069)     | ı (\u0131)
Turkish i - I (\u0130) | I (\u0130)     | I (\u0130)   | I (\u0130)     | i (\u0069)

As you can see:

  1. Uppercasing lower case letters and lowercasing upper case letters give different results for invariant culture and Turkish culture.
  2. Uppercasing upper case letters and lowercasing lower case letters has no effect, no matter what the culture is.
  3. Culture.CultureInvariant leaves the Turkish characters as is
  4. ToUpper and ToLower are reversible, that is lowercasing a character after uppercasing it, brings it to the original form, as long as for both operations the same culture was used.

According to MSDN, for Char.ToUpper and Char.ToLower Turkish and Azeri are the only affected cultures because they are the only ones with single-character casing differences. For strings, there might be more cultures affected.


Source code of a console application used to generate the output:

using System;
using System.Globalization;
using System.Linq;
using System.Text;

namespace TurkishI
{
    class Program
    {
        static void Main(string[] args)
        {
            var englishI = new UnicodeCharacter('\u0069', "English i");
            var turkishI = new UnicodeCharacter('\u0131', "Turkish i");

            Console.WriteLine("# Lowercase letters");
            Console.WriteLine("Character              | UpperInvariant | UpperTurkish | LowerInvariant | LowerTurkish");
            WriteUpperToConsole(englishI);
            WriteLowerToConsole(turkishI);

            Console.WriteLine("\n# Uppercase letters");
            var uppercaseEnglishI = new UnicodeCharacter('\u0049', "English i");
            var uppercaseTurkishI = new UnicodeCharacter('\u0130', "Turkish i");
            Console.WriteLine("Character              | UpperInvariant | UpperTurkish | LowerInvariant | LowerTurkish");
            WriteLowerToConsole(uppercaseEnglishI);
            WriteLowerToConsole(uppercaseTurkishI);

            Console.ReadKey();
        }

        static void WriteUpperToConsole(UnicodeCharacter character)
        {
            Console.WriteLine("{0,-9} - {1,10} | {2,-14} | {3,-12} | {4,-14} | {5,-12}",
                character.Description,
                character,
                character.UpperInvariant,
                character.UpperTurkish,
                character.LowerInvariant,
                character.LowerTurkish
            );
        }

        static void WriteLowerToConsole(UnicodeCharacter character)
        {
            Console.WriteLine("{0,-9} - {1,10} | {2,-14} | {3,-12} | {4,-14} | {5,-12}",
                character.Description,
                character,
                character.UpperInvariant,
                character.UpperTurkish,
                character.LowerInvariant,
                character.LowerTurkish
            );
        }
    }


    class UnicodeCharacter
    {
        public static readonly CultureInfo TurkishCulture = new CultureInfo("tr-TR");

        public char Character { get; }

        public string Description { get; }

        public UnicodeCharacter(char character) : this(character, string.Empty) {  }

        public UnicodeCharacter(char character, string description)
        {
            if (description == null) {
                throw new ArgumentNullException(nameof(description));
            }

            Character = character;
            Description = description;
        }

        public string EscapeSequence => ToUnicodeEscapeSequence(Character);

        public UnicodeCharacter LowerInvariant => new UnicodeCharacter(Char.ToLowerInvariant(Character));

        public UnicodeCharacter UpperInvariant => new UnicodeCharacter(Char.ToUpperInvariant(Character));

        public UnicodeCharacter LowerTurkish => new UnicodeCharacter(Char.ToLower(Character, TurkishCulture));

        public UnicodeCharacter UpperTurkish => new UnicodeCharacter(Char.ToUpper(Character, TurkishCulture));


        private static string ToUnicodeEscapeSequence(char character)
        {
            var bytes = Encoding.Unicode.GetBytes(new[] {character});
            var prefix = bytes.Length == 4 ? @"\U" : @"\u";
            var hex = BitConverter.ToString(bytes.Reverse().ToArray()).Replace("-", string.Empty);
            return $"{prefix}{hex}";
        }

        public override string ToString()
        {
            return $"{Character} ({EscapeSequence})";
        }
    }
}

ToUpperInvariant uses the rules from the invariant culture


there is no difference in english. only in turkish culture a difference can be found.

참고URL : https://stackoverflow.com/questions/3550213/in-c-sharp-what-is-the-difference-between-toupper-and-toupperinvariant

반응형