【C#】课堂知识点#3
1、讲解了实验1中,利用Char.is***来进行判断字符类型。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 13 Console.WriteLine("请输入一个字符"); 14 int x = Console.Read(); 15 char ch = (char)x; 16 string res = ""; 17 if ( char.IsDigit(ch) ) 18 { 19 res = "数字"; 20 } 21 else if ( char.IsLower(ch) ) 22 { 23 res = "小写字母"; 24 } 25 else if ( char.IsUpper(ch) ) 26 { 27 res = "大写字母"; 28 } 29 else 30 { 31 res = "其他字符"; 32 } 33 Console.WriteLine("{0} 是 {1}",ch,res); 34 } 35 } 36 }
2、讲解了实验2中,利用Split分割字符数组,以及统计每一个字母的输出的次数。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 string s = "If you ask me how much i love you!"; 13 //利用了匿名数组,省略了实例化的过程. 14 string []str = s.Split(new char[] {',','.',' ',' '},StringSplitOptions.RemoveEmptyEntries); 15 16 //获取数组的长度 17 int len = str.Length; 18 int Len = str.GetLength(0); 19 20 //两次转换,一次转化成大写字母,第二次转化成Char类型的数组. 21 char [] arr = s.ToUpper().ToCharArray(); 22 int[] Cnt = new int[26]; 23 foreach (var item in arr ) 24 { 25 if( char.IsUpper(item)) 26 Cnt[item-'A']++; 27 } 28 29 //输出对应字母的出现的次数 30 for (int i = 0 ; i < 26; i++ ) 31 { 32 Console.WriteLine("{0} - {1}",(char)(i+'A') , Cnt[i]); 33 } 34 35 } 36 } 37 }