在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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
