c#基础功能
using System.Net; internal class Program { private static void Main(string[] args) {
// toint32 会进行四舍五入
//math.floor 向下取整,不进行四舍五入,
var str = "38.124584999999999";
var dec = Convert.ToDecimal(str);
var t222 = Convert.ToInt32(Math.Floor(dec));
//组成字符串 var m1 = string.Concat(1, 2,3,4,121); //判断是否有空格的字符串 string.IsNullOrWhiteSpace(" "); string i1 ="abc";string i2 = "ABC"; //比较结果0是相等 int r1 = string.Compare(i1, i2,ignoreCase:true); //百分比 var a = (decimal)5; var b = 3; var c = (a / b).ToString("p"); //时间戳转时间 var t1 = StampToDateTime("1686037513000"); //url转成base64 var result = GetUrlToBase64(@"http://59.249.230.64:80/group1/M00/9B/66/O_nmQGR-5T2AGYEeAAKraJLaWBc10.jpeg"); //发布环境才走 #if !DEBUG var ttt = "ssss"; #endif //分组去每组最大的积分 var customerList = new List<Customer>{ new Customer {Name = "a", Score =30}, new Customer {Name = "a", Score =35}, new Customer {Name = "b", Score =20}, new Customer {Name = "c", Score =50}, new Customer {Name = "b", Score =60}, new Customer {Name = "a", Score =80}, }; List<Customer> customers = customerList .GroupBy(t => t.Name).SelectMany(a => a.Where(b => b.Score == a.Max(c => c.Score))).ToList(); foreach (var data in customers) { Console.WriteLine(data.Name + "," + data.Score); } Console.ReadLine(); } /// <summary> /// 获取网络图片转换为base64 /// </summary> /// <param name="url">网络图片路径 例:http://123.123.123.123/a.jpg</param> /// <returns></returns> public static (bool, string) GetUrlToBase64(string url) { try { WebClient mywebclient = new WebClient(); byte[] Bytes = mywebclient.DownloadData(url); string Base64string = Convert.ToBase64String(Bytes); return (true, Base64string); } catch (Exception e) { return (false, null); } } /// <summary> /// 时间戳转时间带毫秒 /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> public static DateTime StampToDateTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000"); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } } public class Customer { public string Name; public int Score; }
//对比
string a = "开始";
string b = "开始";
if (string.Compare(a, b, true) == 0)
{ }
//值对比
if (string.Equals(a, b)) { }
1、使用str.Length == 0做空串比较
string str="";
if (str.Length == 0){
Console.WriteLine("为空!");}
或者使用if(str==String.Empty)其次使用if(str=="")。
2、使用string.Empty给一个空字符串变量赋初始值
string.Empty不占用内存空间,而str==""的方式是具体的实现,是占用空间的。
string str=string.Empty;
3、避免不必要的字符串ToUpper、ToLower类操作而使用String.Compare
String.Compare可实现忽略字符串大小写,而ToUpper、ToLower这类方法均会重新生成字符串对。
string str1="公众号:dotNet开发跳槽";
string str2="公众号:DOTNet开发跳槽";
//推荐使用
if (string.Compare(str1,str2,true)==0)
Console.WriteLine("相等!");
//不推荐
if (str1.ToUpper()==str2.ToUpper())
Console.WriteLine("相等!");
4、正确使用StringBuilder进行字符串拼接操作
如果要构造一个较长的字符串,尤其是拼接超过10次时(经验值),就应使用StringBuilder做字符串拼接操作。
Stopwatch timer = new Stopwatch();
string s = null;
timer.Start();
for (int i = 0; i < 66666; i++)
{
s += (i + 1) + "、Dotnet开发跳槽 ";
}
timer.Stop();
decimal micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine($"拼串耗时:{micro}");
StringBuilder sb = new StringBuilder();
timer.Restart();
for (int i = 0; i < 66666; i++)
{
sb.Append(i + 1);
sb.Append("、Dotnet开发跳槽 ");
}
string t = sb.ToString();
timer.Stop();
micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine($"StringBuilder耗时:{micro}");
另外避免滥用StringBuilder类似str1+str2+str3+str4的字符串拼接操作会被编译为 String.Concat(str1,str2,str3, str4),效率反而高于StringBuilder。
5、创建StringBuilder应指定初始大小
StringBuilder默认的初始大小为16,一旦超过即需要Resize一次并增加GC压力。建议根据经验值为其指定初始大小,比如最大有128个中文字符,那么初始为256。
StringBuilder sb = new StringBuilder(256);