C#字符结构用法介绍和示例

2021年5月10日16:03:35 发表评论 899 次浏览

在c#中,Char Struct被用来表示一个字符作为UTF-16代码单元。此结构定义在System名称空间下。基本上,这是用来表示。net框架中的Unicode字符。Unicode标准使用唯一的21位标量(称为代码点)来标识每个Unicode字符。它还定义了UTF-16编码形式,指定了如何将一个编码点编码为一个或多个16位值的序列。每个16位值从十六进制0x0000到0xFFFF,以Char结构存储。Char对象的值是它的16位数字(序数)值。

Char结构提供了不同的方法,用于将当前Char对象的值转换为另一种类型的对象,比较Char对象,检查Char对象的Unicode类别等。

属性:

  • MaxValue:这是一个常量字段, 代表可能的最大Char值。
  • MinValue:这是一个常量字段, 表示可能的Char最小值。

方法

方法 描述
CompareTo() 将此实例与指定的对象或值类型进行比较, 并指示此实例是在先后, 在后还是在排序时出现在与指定对象或值类型相同的位置。
ConvertFromUtf32(Int32) 将指定的Unicode代码点转换为UTF-16编码的字符串
ConvertToUtf32() 将UTF-16编码的代理对的值转换为Unicode代码点。
Equals() 返回一个值, 该值指示此实例是否等于指定的对象或Char值。
GetHashCode() 返回此实例的哈希码。
GetNumericValue() 将指定的数字Unicode字符转换为双精度浮点数。
GetTypeCode() 返回值类型Char的TypeCode。
GetUnicodeCategory() 将Unicode字符分类到由UnicodeCategory值之一标识的组中。
IsControl() 指示是否将指定的Unicode字符归类为控制字符。
IsDigit() 指示是否将Unicode字符归类为十进制数字。
IsHighSurrogate() 指示指定的Char对象是否为高代理。
IsLetter() 指示是否将Unicode字符归类为Unicode字母。
IsLetterOrDigit() 指示将Unicode字符分类为字母还是十进制数字。
IsLower() 指示是否将Unicode字符归类为小写字母。
IsLowSurrogate() 指示指定的Char对象是否为低代理。
IsNumber() 指示Unicode字符是否归类为数字。
IsPunctuation() 指示是否将Unicode字符归类为标点符号。
IsSeparator() 指示是否将Unicode字符归类为分隔符。
IsSurrogate() 指示字符是否具有代理代码单元。
IsSurrogatePair() 指示两个指定的Char对象是否形成代理对。
IsSymbol() 指示是否将Unicode字符归类为符号字符。
IsUpper() 指示是否将Unicode字符归类为大写字母。
IsWhiteSpace() 指示是否将Unicode字符归类为空格。
Parse(字符串) 将指定字符串的值转换为其等效的Unicode字符。
ToLower() 将Unicode字符的值转换为其等效的小写字母。
ToLowerInvariant(Char) 使用不变区域性的大小写规则将Unicode字符的值转换为等效的小写字母。
ToString() 将此实例的值转换为其等效的字符串表示形式。
ToUpper() 将Unicode字符的值转换为等效的大写字母。
ToUpperInvariant(Char) 使用不变区域性的大小写规则将Unicode字符的值转换为等效的大写形式。
TryParse(字符串, 字符) 将指定字符串的值转换为其等效的Unicode字符。返回码指示转换是成功还是失败。

示例1:

//C# program to demonstrate the
//Char.CompareTo(Char) Method
using System;
  
class GFG {
  
     //Main Method
     public static void Main()
     {
         char c1 = 'G' ;
         char c2 = 'f' ;
         char c3 = 'M' ;
  
         //using Char.CompareTo(Char) Method
         //returns 0 as this instance has
         //same position in the sort as in c1
         Console.WriteLine( 'G' .CompareTo(c1));
  
         //using Char.CompareTo(Char) Method
         //returns -31 as this instance
         //precedes c2
         Console.WriteLine( 'G' .CompareTo(c2));
  
         //using Char.CompareTo(Char) Method
         //returns -6 as this instance follows
         //c3
         Console.WriteLine( 'G' .CompareTo(c3));
     }
}

输出如下:

0
-31
-6

示例2:

//C# program to illustrate the
//Char.IsWhiteSpace(Char) Method
using System;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //Declaration of data type
         bool output;
  
         //checking if space
         //is a whitespace
         char c1 = ' ' ;
         output = Char.IsWhiteSpace(c1);
         Console.WriteLine(output);
  
         //checking if carriage return
         //is a whitespace
         char c2 = '\n' ;
         output = Char.IsWhiteSpace(c2);
         Console.WriteLine(output);
  
         //checking if hyphen
         //is a whitespace
         char c3 = '-' ;
         output = Char.IsWhiteSpace(c3);
         Console.WriteLine(output);
     }
}

输出如下:

True
True
False

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.char?view=netframework-4.7.2#definition

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: