C#常用操作
c#判断是否为整数 1 equationMgr.Equation[i].Split('=' )[1 ].Trim().All(char .IsNumber)
c#判断是否为数值 1 2 decimal num = 0 ;decimal .TryParse((equationMgr.Equation[i].Split('=' )[1 ].Trim()),out num)
字符串 字符串比较 1 2 3 4 5 6 7 8 9 10 11 方法1 :s1.CompareTo(s2): 表示字符串s1与s2进行大小比较, s1<s2 s1.CompareTo(s2)结果为:-1 s1=s2 s1.CompareTo(s2)结果为:0 s1>s2 s1.CompareTo(s2)结果为:1 方法2 :String.Compare(s1,s2) 表示字符串s1与s2进行大小比较, s1<s2 String.Compare(s1,s2)结果为:-1 s1=s2 String.Compare(s1,s2)结果为:0 s1>s2 String.Compare(s1,s2)结果为:1
字符串排序 1 2 Array.Sort(string []); for (int q = 0 ; q < string [].Length; q++) { string [q] = string [q]; Console.WriteLine(string [q] + " " ); }
判断汉字 判断一个字符是不是汉字通常有三种方法,
1、用ASCII码判断
在 ASCII码表中,英文的范围是0-127,而汉字则是大于127,具体代码如下:
1 2 3 4 5 6 7 8 string text = "是不是汉字,ABC,柯乐义" ; for (int i = 0 ; i < text.Length; i++) { if ((int )text[i] > 127 ) Console.WriteLine("是汉字" ); else Console.WriteLine("不是汉字" ); }
2、用汉字的 UNICODE 编码范围判断
汉字的 UNICODE 编码范围是4e00-9fbb,具体代码如下:
1 2 3 4 5 6 7 8 string text = "是不是汉字,ABC,keleyi.com" ; char [] c = text.ToCharArray(); for (int i = 0 ; i < c.Length;i++) if (c[i] >= 0x4e00 && c[i] <= 0x9fbb ) Console.WriteLine("是汉字" ); else Console.WriteLine("不是汉字" );
3、用正则表达式判断
用正则表达式判断也是用汉字的 UNICODE 编码范围,具体代码如下:
1 2 3 4 5 6 7 8 string text = "是不是汉字,ABC,keleyi.com" ; for (int i = 0 ; i < text.Length; i++) { if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]+{1}quot;)) Console.WriteLine(" 是汉字"); else Console.WriteLine(" 不是汉字"); }
按字符串分割: 1 2 3 4 5 6 7 string text = "apple##banana##orange##grape" ;string [] fruits = text.Split(new string [] { "##" }, StringSplitOptions.None);foreach (string fruit in fruits){ Console.WriteLine(fruit); }
List 常见用法 实例化
1 2 3 4 5 6 7 8 List<string > strList = new List<string >(); strList.Add(string ); Person temp = personList.Find(t => t.name == "Jack" && t.age > 30 && t.sex == true ); List<string > strList = new List<string >();
用if判断新对象(字符串)是否在strList数组内:
1 2 3 4 5 6 if (strList.Contains(string )){continue ;}Function(); else strList.Add(swComponent.GetPathName());
1 2 3 4 5 6 personList.Exists(t => t.name == "John" ) foreach (KeyValuePair<int ,int > temp in strList){ Console.WriteLine("Key = {0}, Value = {1}" ,temp.Key, temp.Value); }
List排序 1 2 3 4 5 6 7 8 class Person : IComparable <Person > { public string name; public int age; public bool sex; public int CompareTo (Person other ) {if (this .age < other.age) {return -1 ; }return 1 ;} }
List 其他方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Count - List元素数量 Add(T item) - 向List添加对象 AddRange() - 尾部添加实现了 ICollection 接口的多个元素 BinarySearch() - 排序后的List内使用二分查找 Clear() - 移除所有元素 Contains(T item) - 测试元素是否在List内 CopyTo(T[] array) - 把List拷贝到一维数组内 Exists() - 判断存在 Find() - 查找并返回List内的出现的第一个匹配元素 FindAll() - 查找并返回List内的所有匹配元素 GetEnumerator() - 返回一个用于迭代List的枚举器 GetRange() - 拷贝指定范围的元素到新的List内 IndexOf() - 查找并返回每一个匹配元素的索引 Insert() - 在List内插入一个元素 InsertRange() - 在List内插入一组元素 LastIndexOf() - 查找并返回最后一个匹配元素的索引 Remove() - 移除与指定元素匹配的第一个元素 RemoveAt() - 移除指定索引的元素 RemoveRange() - 移除指定范围的元素 Reverse() - 反转List内元素的顺序 Sort() - 对List内的元素进行排序 ToArray() - 把List内的元素拷贝到一个新的数组内 trimToSize() - 将容量设置为List中元素的实际数目
Dictionary字典操作 实例化 1 Dictionary<[key],[value ]> mDic = new Dictionary<[key],[value ]>();
Dictionary字典会按两两对应发送记录,形成字典组,
参考资料 https://blog.csdn.net/mr_five55/article/details/129804455
using用法 除了使用异常处理的方式释放资源外,还可以使用 using 的方式释放资源。具体的语句如下。
1 2 3 4 5 using (SqlConnection 连接对象名 = new SQLConnection( 数据库连接串 )){ }
using 关键字的用法主要有两个,一个是引用命名空间,一个是创建非托管资源对象。
在 .NET 平台上资源分为托管资源和非托管资源,托管资源是由 .NET 框架直接提供对其资源在内存中的管理,例如声明的变量;非托管资源则不能直接由.NET 框架对其管理,需要使用代码来释放资源,例如数据库资源、操作系统资源等。