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

โปรแกรม C# แปลงพิมพ์ใหญ่/เล็กของตัวอักษร

ใช้เมธอด ToLower() และ ToUpper() ของคลาส String

ตัวอย่างโค้ดเต็ม

using System;
using System.Collections.Generic;
using System.Text;
namespace Demo {
    class MyApplication {
        static void Main(string[] args) {
            string str = "AMIT";
            Console.WriteLine("UpperCase : {0}", str);
            // แปลงเป็นพิมพ์เล็ก
            Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());
            Console.ReadLine();
        }
    }
}

ผลลัพธ์

UpperCase : AMIT
Converted to LowerCase : amit

เมธอดที่เกี่ยวข้อง:

  • ToUpper() - แปลงเป็นพิมพ์ใหญ่ทั้งหมด
  • ToLower() - แปลงเป็นพิมพ์เล็กทั้งหมด
  • ToUpperInvariant() / ToLowerInvariant() - ใช้กับ Culture-independent (เหมาะสำหรับไฟล์ config, identifier ฯลฯ)