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을 사용 했습니다.
속성이 상속 되기 때문에 두 컨트롤 대신 Form
의 Font
속성을 설정했습니다 .Label
Font
그리고 소형 (예 : 생산이 아닌) 코드를 좋아하기 때문에 다른 몇 줄을 줄였습니다.
나는 지금 당장 할 일이 낫지 않았다.
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".
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:
- Uppercasing lower case letters and lowercasing upper case letters give different results for invariant culture and Turkish culture.
- Uppercasing upper case letters and lowercasing lower case letters has no effect, no matter what the culture is.
Culture.CultureInvariant
leaves the Turkish characters as isToUpper
andToLower
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.
'Programing' 카테고리의 다른 글
스케일링시 보간 비활성화 (0) | 2020.07.14 |
---|---|
Java LinkedHashMap은 첫 번째 또는 마지막 항목을 얻습니다. (0) | 2020.07.14 |
PostgreSQL DB에서 현재 연결 수를 가져 오는 올바른 쿼리 (0) | 2020.07.14 |
파이썬의 목적 __repr__ (0) | 2020.07.14 |
인터페이스를 구현하는 추상 클래스가 왜 인터페이스 메소드 중 하나의 선언 / 구현을 놓칠 수 있습니까? (0) | 2020.07.14 |