String

常用静态方法

string.Compare(string str1,string str2,bool ignoreCase)

按照字典顺序比较字符串

当str1 > str2时,返回1
当str1 = str2时,返回0
当str1 < str2时,返回-1 ignoreCase:true 忽略大小写

string.Concat(string str1,string str2)

 string str=string.Concat("c","#"); //str="c#";

String.Format(string str)

string str=String.Format("今年是{0}年","2022");//str="今年是2022年";

string.IsNullOrEmpty(string str1)

  • 判断字符是否为null或者为空,返回值为bool
string str2="";
bool b2=string.IsNullOrEmpty(str2);//b2=true; string str3=null;
bool b3=string.IsNullOrEmpty(str3);//b3=true;

string.Join(string str,string[] strArr)

  • 将数组strArr中的内容拼接成一个新的字符串,并在对应数组的每两项间添加分隔符str
string strs=string.Join(",",string[]{"w","e","r","t"});//strs="w,e,r,t";

split去重

string update_invoice = FINVO System.CollectionICENUMBER + "," + invoiceNumber; // 追加发票号
string[] oldInvoiceList = update_invoice.Split(new Char[] { ',' });
string update_invoice_str = string.Join(",", oldInvoiceList.Distinct().ToArray());

Contains

  • Contains 判断字符串中是否包含某个字符,返回bool值
string str="我爱编程";
bool b=str.Contains("程");//b=true;

StartsWith/EndsWith

string str="我好喜欢你";

bool b1=str.StartsWith("好");//b1=false;

bool b2-str.EndsWith("你");//b2=true;

Equals

string str1="asd";
string str2="ert"; bool b = str1.Equals(str2); //b=false;
bool <strName>.Equals(string str, StringComparison.OrdinalIgnoreCase) //表示不区分大小写

IndexOf/LastIndexOf

  • 判断字符串第一次出现(IndexOf)和最后一次出现(LastIndexOf )的位置,如果没有出现过则返回值为-1
string str ="今天的雨很大,天很冷";

int i=str.IndexOf("很"); //i=4;
int i=str.LastIndexOf("很");//j=8;
int m=str.IndexOf("小");//m=-1;

Replace

string str="好困呀";
string s=str.Replace("困","精神");//s="好精神呀";

Insert

  • 在字符串的index位置上插入字符,原来的字符依次后移,变成一个新的字符串
string str="夜深了";
string s=str.Insert(1,"已经");// s="夜已经深了"

Remove

  • 在字符串中移除从startIndex开始,长度为length的字符串,剩下的字符合为一个新的字符串( = .Remove(startIndex,length)
string str="夜已经深了";
string s=str.Remove(1,2);//s="夜深了";

Split

  • 将字符串以sep数组中的字符分割,分割后得到的内容存到一个数组中(string[] .Split(params char[] sep);)
string str="我,真的、好困;呀";
string[] s=str.Split(new char(){',','、',';'});//s=string[]{"我","真的","好困","呀"};

Substring

  • 取字符以index开始截取,并截取lenth个字符(string .Substring(index,lenth))
string str="还在下雨";
string s=str.Substring(2,2);//s="下雨";

ToCharArray

  • 将字符串转化为字符数组(.ToCharArray())
string str="雨已经小了";
char[] s=str.ToCharArray();//s=char[]{'雨',"已","经","小","了"};

Trim

  • 出去两边的空格
string str=" aa ";
string s=str.Trim();//s="aa";

ToUpper/ToLower

  • ToUpper(转换为大写)和ToLower(转换为小写)
string s="RaSer";
string s1=s.ToUpper();//s1="RASER";
string s2=s.ToLower();//s2="raser";

API

属性

方法





List

创建List

using System.Collections.Generic;
List<string> list = new List<string>();
list.Add("a");

数组去重

using System;
using System.Collections.Generic;
using System.Linq; List<string> list1 = new List<string>() {"123","456","789","789" };// ["123","456","789","789"]
List<string> newList = list1.Distinct().ToList(); //["123","456","789"]

数组是否包含

using System;
using System.Collections.Generic;
using System.Linq; List<string> list1 = new List<string>() {"123","456","789","789" };// ["123","456","789","789"]
if(list1.Contains("123")){
Console.WriteLine(true);
}else{
Console.WriteLine(false);
}

数组分组

using System;
using System.Collections.Generic;
using System.Linq; List<int> sqlList = new List<int>();
for(int i=0;i<100;i++){
sqlList.Add(i);
} Console.WriteLine(sqlList.ToString());
List<List<int>> sql_batch = sqlList.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 5) //分成5组
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
Console.WriteLine(sql_batch.ToString());

API

属性

方法

HashTable

  • 线程安全,单线程性能不如Dictionary

创建Map

using System;
using System.Collections; Hashtable table = new Hashtable();
//添加的是键值对
table.Add("name", "zhangsan");
table.Add("age", 10);
table.Add("gender", "male");

删除指定键的元素

using System;
using System.Collections; Hashtable table = new Hashtable();
table.Add("age", 10);
//通过Key来删除一个键值对
table.Remove("age");

修改元素

using System;
using System.Collections; Hashtable table = new Hashtable();
table.Add("age", 10);
//通过Key来修改元素
table["age"] = 30;

遍历Keys

using System;
using System.Collections; Hashtable table = new Hashtable();
//添加的是键值对
table.Add("name", "zhangsan");
table.Add("age", 10);
table.Add("gender", "male");
ICollection keys = table.Keys;
//遍历刚刚获取到的所有的Key
foreach (object key in keys)
{
//通过Key来获取Value
Console.WriteLine($"{key}={table[key]}");
}

遍历Keys-Values

using System;
using System.Collections; Hashtable table = new Hashtable();
//添加的是键值对
table.Add("name", "zhangsan");
table.Add("age", 10); //Hashtable中存储的元素类型是DictionaryEntry
foreach (DictionaryEntry entry in table)
{
//获取一个键值对中的键
object key = entry.Key;
//获取一个键值对中的值
object value = entry.Value;
Console.WriteLine($"{key}={value}");
}

获取HashTable中的键值对的数目

using System;
using System.Collections; Hashtable table = new Hashtable();
int Count = table.Count;

清空集合

using System;
using System.Collections; Hashtable table = new Hashtable();
table.Clear();

判断Hashtable中是否包含了指定的键

using System;
using System.Collections; Hashtable table = new Hashtable();
//添加的是键值对
table.Add("name", "zhangsan");
table.Add("age", 10);
bool result = table.Contains("age");

Dictionary

创建Map

using System.Collections.Generic;

Dictionary<int,string > dict = new Dictionary<int,string>();

dict.Add(1,"111");

dict.Add(2,"222");

删除指定键的元素

using System.Collections.Generic;

Dictionary< string , int > d = new Dictionary< string , int >();
d.Add( "C#" , 2);
d.Add( "VB" , 1);
d.Add( "C" , 0);
d.Add( "C++" , -1);
//删除键为“C”的元素
d.Remove( "C" );
//删除键为“VB”的元素
d.Remove( "VB" );

序列化为JSON对象

using Newtonsoft.Json;
using System.Collections.Generic; Dictionary<string, int> dic = new Dictionary<string, int>() {
{"张三",1},
{"李四",2},
};
string result = JsonConvert.SerializeObject(dic);
Console.WriteLine(result); //{"张三":1,"李四":2}

JSON对象反序列化Dictionary

using Newtonsoft.Json;
using System.Collections.Generic; result = "{\"张三\":1,\"李四\":2}";
Dictionary<string, int> dic2 = JsonConvert.DeserializeObject<Dictionary<string, int>>(result);
foreach (var item in dic2)
{
Console.WriteLine($"{item.Key}---->{item.Value}");
}

判断是否存在相应的key并显示

using System.Collections.Generic;

Dictionary<int,string > dict = new Dictionary<int,string>();
if (dict.ContainsKey(<key>))
{
Console.WriteLine(dict[<key>]);
}

遍历Keys

using System.Collections.Generic;

Dictionary<int,string > dict = new Dictionary<int,string>();
foreach (var item in dict.Keys)
{
Console.WriteLine( "Key:{0}" , item);
}

遍历Values

using System.Collections.Generic;

Dictionary<int,string > dict = new Dictionary<int,string>();
foreach (var item in dict.Values)
{
Console.WriteLine( "value:{0}" , item);
}

遍历整个字典

using System.Collections.Generic;

Dictionary<int,string > dict = new Dictionary<int,string>();
foreach (var item in dict)
{
Console.WriteLine( "key:{0} value:{1}" , item.Key, item.Value);
}

HashSet

  • 高性能且无序的集合,只能使用foreach来进行迭代,而无法使用for循环。

创建HashSet

using System;
using System.Collections.Generic;
HashSet<string> hashSet = new HashSet<string>();
hashSet.Add("A");
hashSet.Add("B");
hashSet.Add("C");
hashSet.Add("D"); // A B C D

判断是否包含

hashSet.Contains("D")

移除某元素

hashSet.Remove(item);

删除所有元素

hashSet.Clear()

判断 HashSet 是否为某一个集合的完全子集

HashSet<string> setA = new HashSet<string>() { "A", "B", "C", "D" };
HashSet<string> setB = new HashSet<string>() { "A", "B", "C", "X" };
HashSet<string> setC = new HashSet<string>() { "A", "B", "C", "D", "E" };
if (setA.IsProperSubsetOf(setC)) //是子集输出1,不是输出0
Console.WriteLine("setC contains all elements of setA.");
if (!setA.IsProperSubsetOf(setB))
Console.WriteLine("setB does not contains all elements of setA.");

集合合并

  • 输出setA setB中的所有元素
using System;
using System.Collections.Generic; HashSet<string> setA = new HashSet<string>() { "A", "B", "C", "D", "E" };
HashSet<string> setB = new HashSet<string>() { "A", "B", "C", "X", "Y" };
setA.UnionWith(setB);
foreach(string str in setA)
{
Console.WriteLine(str);
}
//最终setA的输出结果是 A B C D E X Y

交集

  • 输出setA和setB集合中都有的元素
using System;
using System.Collections.Generic; HashSet<string> setA = new HashSet<string>() { "A", "B", "C", "D", "E" };
HashSet<string> setB = new HashSet<string>() { "A", "B", "C", "X", "Y" };
setA.IntersectWith(setB);
// A B C

差集

  • 输出setA集合中有但setB集合中没有的元素
using System;
using System.Collections.Generic; HashSet<string> setA = new HashSet<string>() { "A", "B", "C", "D", "E" };
HashSet<string> setB = new HashSet<string>() { "A", "B", "C", "X", "Y" };
setA.ExceptWith(setB);
// D E

两个集合都不全有的元素

HashSet<string> setA = new HashSet<string>() { "A", "B", "C", "D", "E" };
HashSet<string> setB = new HashSet<string>() { "A", "X", "C", "Y" };
setA.SymmetricExceptWith(setB);
foreach (string str in setA)
{
Console.WriteLine(str);
}
//对于这个示例,最终输出结果是BDEXY

SortedSet

创建SortedSet

using System;
using System.Collections.Generic; SortedSet<string> set1 = new SortedSet<string>();
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
Console.WriteLine("Elements in SortedSet1...");
foreach (string res in set1) {
Console.WriteLine(res);
}

API

Json

创建JSON

金蝶JSON

using Kingdee.BOS.JSON;
JSONObject jsonObject = new JSONObject();
jsonObject.Put("userName", dynamicObjectCollection[0]["USERNAME"]);
jsonObject.Put("reason", backMsg);
jsonObject.Put("bbcOrderNum",billNo);

C# JSON

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
JObject jObject = new JObject();
jObject["recMobile"] = Convert.ToString(dynamicObject["FReceiverPhone"]);
jObject["recTel"] = Convert.ToString(dynamicObject["FReceiverTel"]);

合并其他对象到属性

JObject obj = new JObject();
obj.Add("name", "张三");
obj.Add("birthday", DateTime.Now); //合并其他对象到当前对象的属性
obj.Add("content", JToken.FromObject(new
{
code = "zhangsan"
})); //返回
{
"name":"张三",
"birthday","2022-05-04",
"content":{
"code":"zhangsan"
}
}

合并其他对象的属性,到当前对象

//合并其他
JObject obj = new JObject();
obj.Add("name", "张三");
obj.Add("birthday", DateTime.Now); JObject obj2 = JObject.FromObject(new
{
code = "lisi"
});
obj.Merge(obj2); //返回
{
"name":"张三",
"birthday","2022-05-04",
"code ":"lisi"
}

JSON解析

using Newtonsoft.Json;
using Newtonsoft.Json.Linq; string jsonText = @"{
"input": {
'size': 193156,
'type': 'image/png'
}";
JObject jobject = (JObject)JsonConvert.DeserializeObject(jsonText);
decimal input_size = Convert.ToDecimal(jobject["input"]["size"]);//193156, 输入图片大小
string input_type = jobject["input"]["type"].ToString();// "image/png",输入图片类型

DateTime

字符串转时间

DateTime time= Convert.ToDateTime("2022-04-28 00:00:00");
Console.WriteLine(time);

时间转字符串

DateTime dt_now = DateTime.Now;
string time=dt_now.ToString("yyyy-MM-dd hh:mm:ss");
Console.WriteLine(time);

时间字符串格式化 yyyy-MM-dd hh:mm:ss 转 yyMMdd

string time = Convert.ToDateTime("2019-08-28 00:00:00").ToString("yyMMdd");
Console.WriteLine(time);

Thread

创建多线程 无参

//threadMain
Console.WriteLine("Main方法开始执行...");
Thread threadA = new Thread(Execute);
threadA.Start(); Thread threadB = new Thread(test);
threadB.Start();
Console.WriteLine("Main方法执行结束..."); //threadB
private void test() {
while (true) {
Console.WriteLine($"test:{DateTime.Now.Ticks}");
}
} //threadA
private void Execute() {
Console.WriteLine("开始下载,此协程的Id是:" + Thread.CurrentThread.ManagedThreadId);
int bill_count = billNoList.Count;
Thread.Sleep(5000 * bill_count);
foreach (string billNo in billNoList)
{
getIvnumber(billNo);
}
Console.WriteLine("下载完成");
}

创建多线程 带参

//定义一个类,用于存放线程需要的数据和线程启动的方法
public class MyThread
{
private string data;//线程数据 public MyThread(string data)
{
this.data = data;
}
//线程启动方法
public void ThreadMain()
{
Console.WriteLine("Running in a thread, data: {0}", data);
}
}
static void Main()
{
MyThread obj = new MyThread("info");//创建实例信息
Thread t3 = new Thread(obj.ThreadMain);//启动实例方法
t3.Start();
}
//定义一个数据类型,传递给线程
public struct Data
{
public string Message;
}
//创建一个方法,将方法给线程的ParameterizedThreadStart委托
static void ThreadMainWithParameters(object obj)
{
Data d = (Data)obj;
Console.WriteLine("Running in a thread, received {0}", d.Message);
}
static void Main()
{
Data d = new Data { Message = "Info" };//创建一个数据实例
Thread t2 = new Thread(ThreadMainWithParameters);//创建线程
t2.Start(d);//启动线程,并传递参数
}

线程启动 前台线程/后台线程

//前台线程
//创建线程方法,以在主线程中调用
static void ThreadMain()
{
Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
Thread.Sleep(3000);
Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
}
static void Main()
{
Thread t1 = new Thread(ThreadMain); t1.Name = "MyNewThread";
t1.Start();   Thread.Sleep(100);
Console.WriteLine("Main thread ending now...");
/*******************输出******************** * Thread MyNewThread started
* Main thread ending now...
* Thread MyNewThread completed
* *****************************************/
}
//后台线程
static void Main()
{
Thread t1 = new Thread(ThreadMain);
t1.Name = "MyNewThread";
t1.IsBackground = true;
t1.Start();
Thread.Sleep(100);
Console.WriteLine("Main thread ending now...");
/*******************输出********************
* Thread MyNewThread started
* Main thread ending now...
* *****************************************/
}
前台线程:主线程默认启动方式为前台线程,主线程结束后,前台线程仍可以活动。
后台线程:如果在线程启动前,将线程的IsBackground属性设置为true,主线程结束时,会终止新线程的执行(不论是否完成任务)。

Ramdom

生成随机数

using System;

Random r = new Random();
Console.WriteLine(r.Next(1,5));

C# 通关手册(持续更新......)的更多相关文章

  1. xss靶场大通关(持续更新ing)

      xss秘籍第一式(常弹) (1)进入自己搭建的靶场,发现有get请求,参数为name,可进行输入,并会将输入的内容显示于网页页面 (2)使用xss的payload进行通关: http://127. ...

  2. 【STM32-V6】STM32F429BIT6开发板开源, 丰富软件资源, 强劲硬件配置, 配套400多实例, 9套手册持续更新中2019-12-12

    淘宝购买地址:淘宝购买链接 次.当前标准库最新版本V2.3,HAL库最新版本V1.1 安富莱微信公共平台,欢迎大家关注(打造高质量公众号) 新版用户手册,重在BSP驱动包设计方法,HAL库的框架学习, ...

  3. BLE资料应用笔记 -- 持续更新

    BLE资料应用笔记 -- 持续更新 BLE 应用笔记 小书匠 简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.'让我们'更深入地探索这些方面吧. 蓝牙无处不在-,您可以在几乎每一台电话.笔记本电 ...

  4. php常用函数(持续更新)

    每一种编程语言在用的过程中都会发现有时候要一种特定需求的功能函数,结果没有内置这样的函数,这个时候就需要自己根据已有函数编写尽可能简单的函数,下面是我在做php相关工作时积累下的函数,会持续更新,您要 ...

  5. 【持续更新】JavaScript常见面试题整理

    [重点提前说]这篇博客里的问题涉及到了了JS中常见的的基础知识点,也是面试中常见的一些问题,建议初入职场的园友Mark收藏,本文会持续更新~ 1. 引入JS的三种方式 1.在HTML标签中直接使用,直 ...

  6. fastadmin 后台管理框架使用技巧(持续更新中)

    fastadmin 后台管理框架使用技巧(持续更新中) FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架,具体介绍,请查看文档,文档地址为:https://doc. ...

  7. tp5 使用技巧(持续更新中...)

    tp5 使用技巧(持续更新中...) 1.自动写入时间 create_time和update_time 使用save方法才行,insert方法不生效,不知为何 2.过滤字段 allowfield和st ...

  8. 痞子衡嵌入式:史上最强i.MX RT学习资源汇总(持续更新中...)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MX RT学习资源. 类别 资源 简介 官方汇总 i.MXRT产品主页 恩智浦官方i.MXRT产品主页,最权威的资料都在这里,参考手 ...

  9. IntelliJ IDEA 2019.3注册码(亲测有效,可激活至 2089 年,持续更新~)

    申明:本教程 IntelliJ IDEA 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除. 注意 本教程适用于 IntelliJ IDEA 所有版本,请放心食用~ ...

随机推荐

  1. Numpy常用random随机函数汇总

    Numpy常用random下的随机函数汇总 官方文档地址:https://docs.scipy.org/doc/numpy-1.14.0/reference/routines.random.html ...

  2. 好用开源的C#快速开发平台

    NFine 是基于 C# 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展,让Web开发更迅速.简单.NFine是一套基于 ASP.NET ...

  3. 前端文件上传-javascript-ajax

    书写是为了更好的记忆. 方案一:form表单上传 该方案优点是支持好,缺点刷新页面. <form action="url" method="post" e ...

  4. Codepen 每日精选(2018-4-16)

    按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以打开原始页面. 内容切换的交互效果https://codepen.io/jcoulterde... 报价卡片的交互效果ht ...

  5. JavaScript 工作原理之六-WebAssembly 对比 JavaScript 及其使用场景

    原文请查阅这里,略有改动,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第六章. 现在 ...

  6. 判断链表是否有环(Java实现)

    判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...

  7. 【每日日报】第五十三天---安装My SQL

    1 2今天安装了My SQL并学习了一些基础的命令 mysql下载及安装教程 2 没有成功安装SQL Server,误删了一些文件 3 明天继续看视频 ------------------------ ...

  8. 彻底理解volatile关键字

    1. volatile简介 在上一篇文章中我们深入理解了java关键字,我们知道在java中还有一大神器就是关键volatile,可以说是和synchronized各领风骚,其中奥妙,我们来共同探讨下 ...

  9. PyQt5 基础知识(六):展示控件

    目录 3. 展示控件 3.1 QLabel 3.1.1 描述 3.1.2 功能作用 3.1.2.1 基本功能 3.1.2.2 文本交互 3.1.2.3 内容操作 3.1.2.3.1 文本字符串 3.1 ...

  10. C++的"开始" Hello World! 你好世界!

    # C++的"开始" Hello World! 你好世界! ```C++ // 第一个程序 //代表注释这一行 #include <iostream> //c++专属头 ...