目录
1.Char.IsLetterOrDigit(Char)用法
2.IsLetterOrDigit(String, Int32)方法
2.IsPunctuation(String, Int32)
一、Char 结构的方法
Char结构的方法很多,用法大同小异。下面仅仅举例常用的几种方法。
二、Char.IsLetterOrDigit 方法
指示 Unicode 字符是否属于字母或十进制数字类别。
1.Char.IsLetterOrDigit(Char)用法
C# 中的 `Char.IsLetterOrDigit(Char)` 方法用于确定指定的字符是否为字母或数字。
如果指定的字符是字母或数字,则该方法返回 `true`;否则,返回 `false`。字母包括所有大写和小写字母,数字包括所有阿拉伯数字(0 到 9)。
// IsLetterOrDigit(Char)用途
namespace ConsoleApp15
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char ch = 'A';
if (char.IsLetterOrDigit(ch))
{
Console.WriteLine(ch + " 是字母或数字。");
}
else
{
Console.WriteLine(ch + " 不是字母或数字。");
}
}
}
}
//运行结果:
/*
A 是字母或数字。
*/
2.IsLetterOrDigit(String, Int32)方法
Char.IsLetterOrDigit(string, int)
用于确定指定字符串在指定索引位置的字符是否为字母或数字。该方法接受两个参数:
public static bool IsLetterOrDigit (string s, int index);
参数
s String
一个字符串。
index Int32
s 中要计算的字符的位置。
返回
Boolean
如果 true 中位于 index 的字符是一个字母或十进制数,则为 s;否则为 false。
例外
ArgumentNullException
s 上声明的默认值为 null。
ArgumentOutOfRangeException
index 小于零或大于 s 中最后一个位置。
// IsLetterOrDigit(String, Int32)方法
namespace ConsoleApp16
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string str = "Hello World!";
int index = 6;
if (char.IsLetterOrDigit(str, index))
{
Console.WriteLine(str[index] + " 是字母或数字。");
}
else
{
Console.WriteLine(str[index] + " 不是字母或数字。");
}
}
}
}
//运行结果:
/*
W 是字母或数字。
*/
三、Char.IsLetter 方法
指示 Unicode 字符是否属于 Unicode 字母类别。
1.IsLetter(Char)
指示指定的 Unicode 字符是否属于 Unicode 字母类别。
public static bool IsLetter (char c);
参数
c Char
要计算的 Unicode 字符。
返回
Boolean
如果 true 是一个字母,则为 c;否则为 false。
C#中的Char.IsLetter()
方法用于判断一个字符是否为字母。此方法是System.Char
类的静态方法,它接受一个字符作为参数,并返回一个布尔值,表示该字符是否为字母。
// IsLetter(Char)
namespace ConsoleApp17
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char ch = 'A';
if (char.IsLetter(ch))
{
Console.WriteLine(ch + " 是字母。");
}
else
{
Console.WriteLine(ch + " 不是字母。");
}
}
}
}
//运行结果:
/*
A 是字母。
*/
2.IsLetter(String, Int32)
指示指定字符串中位于指定位置处的指定字符串是否属于 Unicode 字母类别。
public static bool IsLetter (string s, int index);
参数
s String
一个字符串。
index Int32
s 中要计算的字符的位置。
返回
Boolean
如果 true 中位于 index 的字符是一个字母,则为 s;否则为 false。
例外
ArgumentNullException
s 上声明的默认值为 null。
ArgumentOutOfRangeException
index 小于零或大于 s 中最后一个位置。
C#中的Char.IsLetter(String, Int32)
方法用于判断一个字符串中的指定索引位置的字符是否为字母。此方法是System.Char
类的静态方法,它接受一个字符串和一个整数作为参数,并返回一个布尔值,表示该字符串在指定索引位置的字符是否为字母。
// IsLetter(String, Int32)方法
namespace ConsoleApp18
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string str = "Hello World!";
if (char.IsLetter(str, 0))
{
Console.WriteLine(str[0] + " 是字母。");
}
else
{
Console.WriteLine(str[0] + " 不是字母。");
}
}
}
}
//运行结果:
/*
H 是字母。
*/
四、Char.IsDigit 方法
指示 Unicode 字符是否属于十进制数字类别。
1. IsDigit(String, Int32)
指示指定字符串中位于指定位置处的字符是否属于十进制数字类别。
public static bool IsDigit (string s, int index);
参数:
str:要检查的字符串。
index:要检查的字符在字符串中的索引位置。
返回值:
如果str[index]是数字(0-9),则返回true;否则,返回false。
// IsDigit(String, Int32)方法
namespace ConsoleApp19
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string str = "1234567890";
if (char.IsDigit(str, 0))
{
Console.WriteLine(str[0] + " 是数字。");
}
else
{
Console.WriteLine(str[0] + " 不是数字。");
}
}
}
}
//运行结果:
/*
1 是数字。
*/
2.IsDigit(Char)
指示指定的 Unicode 字符是否属于十进制数字类别。
public static bool IsDigit (char c);
参数:
value:要检查的字符。
返回值:
如果 value 是数字(0-9),则返回 true;否则,返回 false。
// IsDigit(Char)方法
namespace ConsoleApp20
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char value = '1';
if (char.IsDigit(value))
{
Console.WriteLine(value + " 是数字。");
}
else
{
Console.WriteLine(value + " 不是数字。");
}
}
}
}
//运行结果:
/*
1 是数字。
*/
五、Char.IsLower 方法
指示 Unicode 字符是否属于小写字母类别。
1. IsLower(Char)
指示指定的 Unicode 字符是否属于小写字母类别。
public static bool IsLower (char c);
参数
c Char
要计算的 Unicode 字符。
返回
Boolean
如果 true 是一个小写字母,则为 c;否则为 false。
// IsLower(Char)
namespace ConsoleApp21
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char charValue = 'r';
bool isLower = char.IsLower(charValue);
Console.WriteLine(charValue +" Is Lowercase: " + isLower);
}
}
}
//运行结果:
/*
r Is Lowercase: True
*/
2.IsLower(String, Int32)
指示指定字符串中位于指定位置处的字符是否属于小写字母类别。
bool result = Char.IsLower(stringValue, index);
其中,stringValue 是一个 string 类型的变量,表示要判断的字符串;
index 是一个 int 类型的变量,表示要判断的字符在字符串中的索引位置。
// IsLower(String, Int32)
namespace ConsoleApp22
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string stringValue = "Hello World";
bool isLower = char.IsLower(stringValue, 2);
Console.WriteLine("The index 2 of "+ stringValue + " Is Lowercase: " + isLower);
}
}
}
// 运行结果:
/*
The index 2 of Hello World Is Lowercase: True
*/
六、Char.IsNumber 方法
指示 Unicode 字符是否属于数字类别。
1. IsNumber(String, Int32)
Char.IsNumber(string, int)
方法用于判断一个字符串中指定索引位置的字符是否为数字字符(0-9)。以下是 Char.IsNumber(string, int)
方法的基本语法:
bool result = Char.IsNumber(stringValue, index);
其中,stringValue 是一个 string 类型的变量,表示要判断的字符串;
index 是一个 int 类型的变量,表示要判断的字符在字符串中的索引位置。
// IsNumber(String, Int32)
namespace ConsoleApp23
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string stringValue = "123456";
bool isNumber = char.IsNumber(stringValue, 0);
Console.WriteLine("Is Number: " + isNumber);
}
}
}
//运行结果:
/*
Is Number: True
*/
2.IsNumber(Char)
指示指定的 Unicode 字符是否属于数字类别。Char.IsNumber(char)
方法用于判断一个字符是否为数字字符(0-9)。
public static bool IsNumber (char c);
参数
c Char
要计算的 Unicode 字符。
返回
Boolean
如果 true 是一个数字,则为 c;否则为 false。
// IsNumber(Char)
namespace ConsoleApp24
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char character = '3';
bool isNumber = char.IsNumber(character);
Console.WriteLine(character+" Is Number: " + isNumber);
}
}
}
//运行结果:
/*
3 Is Number: True
*/
七、Char.IsPunctuation 方法
指示 Unicode 字符是否属于标点符号类别。
1.IsPunctuation(Char)
指示指定的 Unicode 字符是否属于标点符号类别。
public static bool IsPunctuation (char c);
参数
c Char
要计算的 Unicode 字符。
返回
Boolean
如果 true 是一个标点符号,则为 c;否则为 false。
// IsPunctuation(Char)
namespace ConsoleApp25
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
char c = '!';
bool isPunctuation = char.IsPunctuation(c);
if (isPunctuation)
{
Console.WriteLine("The character '{0}' is a punctuation mark.", c);
}
else
{
Console.WriteLine("The character '{0}' is not a punctuation mark.", c);
}
}
}
}
//运行结果:
/*
The character '!' is a punctuation mark.
*/
2.IsPunctuation(String, Int32)
指示指定字符串中位于指定位置处的字符是否属于标点符号类别。
public static bool IsPunctuation (string s, int index);
参数
s String
一个字符串。
index Int32
s 中要计算的字符的位置。
返回
Boolean
如果 true 中位于 index 的字符是一个标点符号,则为 s;否则为 false。
例外
ArgumentNullException
s 上声明的默认值为 null。
ArgumentOutOfRangeException
index 小于零或大于 s 中最后一个位置。
// IsPunctuation(String, Int32)
namespace ConsoleApp26
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
string text = "Hello, world!";
int index = 6;
bool isPunctuation = char.IsPunctuation(text, index);
if (isPunctuation)
{
Console.WriteLine("The character '{0}' is a punctuation mark.", text[index]);
}
else
{
Console.WriteLine("The character '{0}' is not a punctuation mark.", text[index]);
}
}
}
}
//运行结果:
/*
The character ' ' is not a punctuation mark.
*/