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

เมธอด Char.GetUnicodeCategory ใน C# พร้อมตัวอย่าง


บทนำ

เมธอด Char.GetUnicodeCategory(String, Int32) ใน C# ใช้จัดประเภทตัวอักษรที่ตำแหน่งที่ระบุในสตริงที่กำหนด ให้เป็นกลุ่มที่ระบุโดยค่า UnicodeCategory ซึ่งเป็น enumeration ที่ครอบคลุมทุกประเภทตัวอักษรในมาตรฐาน Unicode

ไวยากรณ์ (Syntax)

public static System.Globalization.UnicodeCategory GetUnicodeCategory (string str, int index);

โดยที่ str คือสตริง และ index คือตำแหน่งของตัวอักษรใน str (เริ่มจาก 0)

ตัวอย่างที่ 1: ตรวจสอบตัวอักษรพิมพ์เล็ก

using System;
using System.Globalization;

public class Demo 
{
    public static void Main()
    {
        string val = "amit";
        UnicodeCategory unicode = Char.GetUnicodeCategory(val, 2);
        Console.WriteLine("The value at specific index = " + unicode);
    }
}

ผลลัพธ์

LowercaseLetter

ตัวอย่างที่ 2: ตรวจสอบตัวเลข

using System;
using System.Globalization;

public class Demo 
{
    public static void Main()
    {
        string val = "hjk9878hj";
        UnicodeCategory unicode = Char.GetUnicodeCategory(val, 4);
        Console.WriteLine("The value at specific index = " + unicode);
    }
}

ผลลัพธ์

The value at specific index = DecimalDigitNumber

หมวดหมู่ UnicodeCategory หลัก

  • UppercaseLetter - ตัวอักษรพิมพ์ใหญ่ (Lu)
  • LowercaseLetter - ตัวอักษรพิมพ์เล็ก (Ll)
  • TitlecaseLetter - ตัวอักษรพิมพ์ใหญ่ตัวแรก (Lt)
  • DecimalDigitNumber - ตัวเลขฐานสิบ (Nd)
  • LetterNumber - ตัวเลขตัวอักษร (Nl)
  • OtherNumber - ตัวเลขอื่นๆ (No)
  • SpaceSeparator - ช่องว่าง (Zs)
  • LineSeparator - คั่นบรรทัด (Zl)
  • ParagraphSeparator - คั่นย่อหน้า (Zp)
  • Control - ตัวควบคุม (Cc)
  • Format - ตัวจัดรูปแบบ (Cf)
  • Surrogate - Surrogate (Cs)
  • PrivateUse - ส่วนตัว (Co)
  • ConnectorPunctuation - เครื่องหมายเชื่อม (Pc)
  • DashPunctuation - ขีด (Pd)
  • OpenPunctuation/ClosePunctuation - วงเล็บเปิด/ปิด (Ps/Pe)
  • OtherPunctuation - เครื่องหมายอื่นๆ (Po)
  • MathSymbol - สัญลักษณ์คณิตศาสตร์ (Sm)
  • CurrencySymbol - สัญลักษณ์สกุลเงิน (Sc)
  • ModifierSymbol - สัญลักษณ์ปรับเปลี่ยน (Sk)
  • OtherSymbol - สัญลักษณ์อื่นๆ (So)

สรุป

เมธอด Char.GetUnicodeCategory() เป็นเครื่องมือที่ทรงพลังสำหรับการวิเคราะห์และประมวลผลข้อความ Unicode ทำให้สามารถจัดการตัวอักษรทุกภาษาและสัญลักษณ์ได้อย่างถูกต้องตามมาตรฐานสากล