다음을 통해 공유


.NET의 변경 사례

사용자의 입력을 허용하는 애플리케이션을 작성하는 경우 데이터를 입력하는 데 사용할 대/소문자(위 또는 아래)를 확신할 수 없습니다. 특히 사용자 인터페이스에 문자열을 표시하는 경우 문자열의 대/소문자를 일관되게 지정하려는 경우가 많습니다. 다음 표에서는 세 가지 대/소문자 변경 메서드에 대해 설명합니다. 처음 두 메서드는 문화권을 수용하는 오버로드를 제공합니다.

메서드 이름 사용하세요
String.ToUpper 문자열의 모든 문자를 대문자로 변환합니다.
String.ToLower 문자열의 모든 문자를 소문자로 변환합니다.
TextInfo.ToTitleCase 문자열을 타이틀 케이스로 변환합니다.

경고

String.ToUpper 문자열을 비교하거나 같은지 테스트하기 위해 문자열을 변환하는 데 메서드와 String.ToLower 메서드를 사용하면 안 됩니다. 자세한 내용은 혼합 사례의 문자열 비교 섹션을 참조하세요 .

혼합된 대/소문자의 문자열 비교

혼합 대소문자의 문자열을 비교하여 그 순서를 결정하려면, comparisonType 매개 변수를 사용하여 String.CompareTo 메서드의 오버로드 중 하나를 호출하고, comparisonType 인수에 대해 StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, 또는 StringComparison.OrdinalIgnoreCase의 값을 제공합니다. 현재 문화권이 아닌 특정 문화권을 사용하여 비교하려면, String.CompareTo 메서드의 오버로드를 cultureoptions 매개변수를 사용하여 호출하고, options 인수에 CompareOptions.IgnoreCase 값을 제공합니다.

혼합 대소문자 문자열을 비교하여 같은지 여부를 확인하려면, comparisonType 매개 변수를 사용하는 String.Equals 메서드의 오버로드 중 하나를 호출하고 comparisonType 인수에 대해 StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, StringComparison.OrdinalIgnoreCase 중 하나의 값을 제공하십시오.

자세한 내용은 문자열 사용에 대한 모범 사례를 참조하세요.

ToUpper 메서드

이 메서드는 String.ToUpper 문자열의 모든 문자를 대문자로 변경합니다. 다음 예제에서는 문자열 "Hello World!"를 혼합 대/소문자에서 대문자로 변환합니다.

string properString = "Hello World!";
Console.WriteLine(properString.ToUpper());
// This example displays the following output:
//       HELLO WORLD!
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.ToUpper())
' This example displays the following output:
'       HELLO WORLD!

앞의 예제는 기본적으로 문화에 민감하게 반응하며, 현재 문화권의 대/소문자 규칙을 적용합니다. 특정 문화권의 대/소문자 규칙을 적용하거나 문화권을 구분하지 않는 대/소문자 변경을 수행하려면 String.ToUpper(CultureInfo) 메서드 오버로드를 사용하고, 지정된 문화권을 나타내는 값 CultureInfo.InvariantCulture 또는 System.Globalization.CultureInfo 개체를 culture 매개 변수로 제공합니다. 메서드를 사용하여 ToUpper 문화권을 구분하지 않는 대/소문자 변경을 수행하는 방법을 보여 주는 예제는 문화권을 구분하지 않는 대/소문자 변경 수행을 참조하세요.

ToLower 메서드

String.ToLower 메서드는 이전 메서드와 비슷하지만 대신 문자열의 모든 문자를 소문자로 변환합니다. 다음 예제에서는 문자열 "Hello World!"를 소문자로 변환합니다.

string properString = "Hello World!";
Console.WriteLine(properString.ToLower());
// This example displays the following output:
//       hello world!
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.ToLower())
' This example displays the following output:
'       hello world!

앞의 예제는 기본적으로 현재 문화권의 대/소문자 규칙을 적용하여 문화에 민감하게 설계되었습니다. 문화권과 무관하게 대소문자 변환을 수행하거나 특정 문화권의 대소문자 규칙을 적용하려면, 메서드 오버로드를 사용해야 하며, culture 매개 변수에 지정된 문화권을 나타내는 값으로 CultureInfo.InvariantCulture이나 System.Globalization.CultureInfo 객체를 제공합니다. 메서드를 사용하여 ToLower(CultureInfo) 문화권을 구분하지 않는 대/소문자 변경을 수행하는 방법을 보여 주는 예제는 문화권을 구분하지 않는 대/소문자 변경 수행을 참조하세요.

ToTitleCase 메서드

TextInfo.ToTitleCase 단어의 첫 번째 문자를 대문자로 변환하고 나머지 문자를 소문자로 변환합니다. 그러나 완전히 대문자인 단어는 약어로 간주되며 변환되지 않습니다.

메서드 TextInfo.ToTitleCase 는 문화권을 구분합니다. 즉, 특정 문화권의 대/소문자 규칙을 사용합니다. 메서드를 호출하려면 먼저 특정 문화권의 CultureInfo.TextInfo 속성에서 해당 문화권의 대/소문자 규칙을 나타내는 TextInfo 개체를 검색해야 합니다.

다음 예제에서는 배열의 각 문자열을 메서드에 전달합니다 TextInfo.ToTitleCase . 문자열에는 적절한 제목 문자열과 약어가 포함됩니다. 문자열은 영어(미국) 문화권의 대/소문자 규칙에 따라 제목 형식으로 변환됩니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { "a tale of two cities", "gROWL to the rescue",
                          "inside the US government", "sports and MLB baseball",
                          "The Return of Sherlock Holmes", "UNICEF and children"};

      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
         Console.WriteLine($"{value} --> {ti.ToTitleCase(value)}");
   }
}
// The example displays the following output:
//    a tale of two cities --> A Tale Of Two Cities
//    gROWL to the rescue --> Growl To The Rescue
//    inside the US government --> Inside The US Government
//    sports and MLB baseball --> Sports And MLB Baseball
//    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
//    UNICEF and children --> UNICEF And Children
Imports System.Globalization

Module Example
    Public Sub Main()
        Dim values() As String = {"a tale of two cities", "gROWL to the rescue",
                                   "inside the US government", "sports and MLB baseball",
                                   "The Return of Sherlock Holmes", "UNICEF and children"}

        Dim ti As TextInfo = CultureInfo.CurrentCulture.TextInfo
        For Each value In values
            Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value))
        Next
    End Sub
End Module
' The example displays the following output:
'    a tale of two cities --> A Tale Of Two Cities
'    gROWL to the rescue --> Growl To The Rescue
'    inside the US government --> Inside The US Government
'    sports and MLB baseball --> Sports And MLB Baseball
'    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
'    UNICEF and children --> UNICEF And Children

이 메서드는 문화에 민감하지만, 언어적으로 올바른 대/소문자 규칙을 제공하지 않습니다. 예를 들어 이전 예제에서 이 메서드는 "두 도시의 이야기"를 "두 도시의 이야기"로 변환합니다. 그러나 en-US 문화권에서 언어적으로 올바른 제목 표기법은 "두 도시의 이야기"입니다.

참고하십시오