Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด ToUpper() ใน C#


เมธอด ToUpper() ใน C# ใช้สำหรับส่งคืนสำเนาของสตริงที่แปลงเป็นตัวพิมพ์ใหญ่ทั้งหมดแล้ว

ไวยากรณ์

public string ToUpper ();

ตัวอย่าง

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("String1 = " + str1); 
        Console.WriteLine("String1 ToUpper = " + str1.ToUpper()); 
        Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4)); 
        Console.Write("Character array...String1 ="); 
        for (int i = 0; i < arr1.Length; i++) 
            Console.Write(" " + arr1[i]); 
        Console.WriteLine("\n\nString2 = " + str2); 
        Console.WriteLine("String2 ToUpper = " + str2.ToUpper()); 
        Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5)); 
        Console.Write("Character array...String2 ="); 
        for (int i = 0; i < arr2.Length; i++) 
            Console.Write(" " + arr2[i]); 
    } 
}

ผลลัพธ์

String1 = Welcome! 
String1 ToUpper = WELCOME! 
String1 Substring from index4 = ome! 
Character array...String1 = c o 
String2 = Thisisit! 
String2 ToUpper = THISISIT! 
String2 Substring from index2 = Thisi 
Character array...String2 = i s

ตัวอย่างที่ 2 (ใช้ CultureInfo)

using System; 
using System.Globalization; 
public class Demo { 
    public static void Main(String[] args) { 
        string str1 = "JackSparrow!"; 
        string str2 = "@#$PQRSTUV!"; 
        Console.WriteLine("String1 = " + str1); 
        Console.WriteLine("String1 ToUpper = " + str1.ToUpper(new CultureInfo("en-US", false))); 
        Console.WriteLine("String1 Substring from index4 = " + str1.Substring(2, 2)); 
        Console.WriteLine("\n\nString2 = " + str2); 
        Console.WriteLine("String2 ToUpper = " + str2.ToUpper(new CultureInfo("en-US", false))); 
        Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5)); 
    } 
}

ผลลัพธ์

String1 = JackSparrow! 
String1 ToUpper = JACKSPARROW! 
String1 Substring from index4 = ck 
String2 = @#$PQRSTUV! 
String2 ToUpper = @#$PQRSTUV! 
String2 Substring from index2 = @#$PQ