在C#中,Char.IsPunctuation()是System.Char struct方法,用於檢查Unicode字符是否可以歸類為標點符號。可以通過傳遞不同類型和數量的參數來重載此方法。
- Char.IsPunctuation(Char)方法
- Char.IsPunctuation(String,Int32)方法
Char.IsPunctuation(Char)方法
此方法用於檢查指定的Unicode字符是否與任何標點符號匹配。如果匹配,則返回True,否則返回False。
用法:
public static bool IsPunctuation(char ch);
參數:
- ch:這是要比較的System.char類型的必需Unicode字符。
返回類型:如果成功匹配任何標點符號,則此方法返回True,否則返回False。此方法的返回類型為System.Boolean。
例:
// C# program to illustrate the 
// Char.IsPunctuation(Char) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    static public void Main() 
    { 
  
        // Declaration of data type 
        bool result; 
  
        // checking if dot(.) 
        // is a punctuation mark 
        char ch1 = '.'; 
        result = Char.IsPunctuation(ch1); 
        Console.WriteLine(result); 
  
        // checking if semi-colon( ; ) 
        // is a punctuation mark 
        char ch2 = ';'; 
        result = Char.IsPunctuation(ch2); 
        Console.WriteLine(result); 
  
        // checking if comma (, ) 
        // is a punctuation mark 
        char ch3 = ', '; 
        result = Char.IsPunctuation(ch3); 
        Console.WriteLine(result); 
  
        // checking if 'g' is 
        // a punctuation mark 
        char ch5 = 'g'; 
        result = Char.IsPunctuation(ch5); 
        Console.WriteLine(result); 
    } 
}
輸出:
True True True False
Char.IsPunctuation(String,Int32)方法
此方法用於檢查指定位置的指定字符串是否與任何標點符號匹配。如果匹配,則返回True,否則返回False。
用法:
public static bool IsPunctuation(string str, int index);
參數:
- 
str:這是要比較的System.String類型的必需字符串。
 index:要比較的是字符串中字符的位置,此參數的類型為System.Int32。
返回類型:如果該方法成功匹配了指定字符串中指定索引處的標點符號,則該方法返回True,否則返回False。此方法的返回類型為System.Boolean。
異常:
- 如果str的值為null,則此方法將提供ArgumentNullException
- 如果索引小於零或大於str中的最後一個位置,則此方法將給出ArgumentOutOfRangeException。
例:
// C# program to illustrate the 
// Char.IsPunctuation(String, Int32) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    static public void Main() 
    { 
  
        // Declaration of data type 
        bool result; 
  
        // checking for punctuation 
        // in a string at specific location 
        string str1 = "GeeksforGeeks"; 
        result = Char.IsPunctuation(str1, 2); 
        Console.WriteLine(result); 
  
        // checking for punctuation 
        // in a string at specific location 
        string str2 = "www.geeksforgeeks.org"; 
        result = Char.IsPunctuation(str2, 3); 
        Console.WriteLine(result); 
    } 
}
輸出:
False True
參考:https://docs.microsoft.com/en-us/dotnet/api/system.char.ispunctuation?view=netframework-4.7.2
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 C# | Char.IsPunctuation() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
