Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức ToUpper() trong C#

Phương thức ToUpper() trong C# trả về một bản sao của chuỗi được chuyển đổi thành chữ hoa.

Cú pháp

public string ToUpper ();

Ví dụ

using System; 
public class Demo { 
    public static void Main(String[] args) { 
        string str1 = "Welcome!"; 
        string str2 = "Thisisit!"; 
        char[] arr1 = str1.ToCharArray(3,2); 
        char[] arr2 = str2.ToCharArray(2,2); 
        Console.WriteLine("Chuỗi 1 = " + str1); 
        Console.WriteLine("Chuỗi 1 ToUpper = " + str1.ToUpper()); 
        Console.WriteLine("Chuỗi 1 Substring từ index 4 = " + str1.Substring(4, 4)); 
        Console.Write("Mảng ký tự...Chuỗi 1 ="); 
        for (int i = 0; i < arr1.Length; i++) 
            Console.Write(" " + arr1[i]); 
        Console.WriteLine("\n\nChuỗi 2 = " + str2); 
        Console.WriteLine("Chuỗi 2 ToUpper = " + str2.ToUpper()); 
        Console.WriteLine("Chuỗi 2 Substring từ index 2 = " + str2.Substring(0, 5)); 
        Console.Write("Mảng ký tự...Chuỗi 2 ="); 
        for (int i = 0; i < arr2.Length; i++) 
            Console.Write(" " + arr2[i]); 
    } 
}

Kết quả

Chuỗi 1 = Welcome! 
Chuỗi 1 ToUpper = WELCOME! 
Chuỗi 1 Substring từ index 4 = ome! 
Mảng ký tự...Chuỗi 1 = c o 
Chuỗi 2 = Thisisit! 
Chuỗi 2 ToUpper = THISISIT! 
Chuỗi 2 Substring từ index 2 = Thisi 
Mảng ký tự...Chuỗi 2 = i s

Ví dụ khác

using System; 
using System.Globalization; 
public class Demo { 
    public static void Main(String[] args) { 
        string str1 = "JackSparrow!"; 
        string str2 = "@#$PQRSTUV!"; 
        Console.WriteLine("Chuỗi 1 = " + str1); 
        Console.WriteLine("Chuỗi 1 ToUpper = " + str1.ToUpper(new CultureInfo("en-US", false))); 
        Console.WriteLine("Chuỗi 1 Substring từ index 4 = " + str1.Substring(2, 2)); 
        Console.WriteLine("\n\nChuỗi 2 = " + str2); 
        Console.WriteLine("Chuỗi 2 ToUpper = " + str2.ToUpper(new CultureInfo("en-US", false))); 
        Console.WriteLine("Chuỗi 2 Substring từ index 2 = " + str2.Substring(0, 5)); 
    } 
}

Kết quả

Chuỗi 1 = JackSparrow! 
Chuỗi 1 ToUpper = JACKSPARROW! 
Chuỗi 1 Substring từ index 4 = ck 
Chuỗi 2 = @#$PQRSTUV! 
Chuỗi 2 ToUpper = @#$PQRSTUV! 
Chuỗi 2 Substring từ index 2 = @#$PQ