1. string

1. 格式化填充
string str = "this {0} a {1}";
Console.WriteLine(string.Format(str, "is", "boy")); // this is a boy
2. 将数据合并成字符串
string[] strArr = new string[3] { "1", "2", "3"};
List<string> strList = new List<string>() { "1", "2", "3" };
string str = string.Join("--", strArr); //"1--2--3"
string str = string.Join("--", strList); //"1--2--3"
3. 分割
分隔符可为 new char[]{'','',..} 或 new string[]{"", "", ..}
可设置最多分割成多少项、分割后是否自动过滤掉空值
string str = "1,2.3,,4";
string[] sp = str.Split(new char[]{',', '.'}); //{"1", "2", "3", "", "4"}
string[] sp = str.Split(new string[]{","}); //{"1","2.3","","4"}
string[] sp = str.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries); //{"1","2.3","4"}
4. 检查是否包含
if (str.Contain(content)) 5. 起始字符串
string str = "www.abcd.com";
str.StartWith("www"); //true
str.StartWith("abcd"); //false
str.StartWith("abcd", 4); //true 6. 空或只有空格
string.Empty 和 "" 都指向固定的静态只读内存区域
而 null 只在栈上分配了空间,堆上没有。 string.IsNullOrEmpty(str); // ""、string.Empty、null 为 true
string.IsNullOrWhiteSpace(str); //""、" "、string.Empty、null 为 true
7. 替换
str = str.Replace(oldSubString, newSubString); 8. 删除
str = str.Remove(beginIndex, length); 9. 截取
string sub = str.Substring(startIndex);
string sub = str.Substring(startIndex, length); 10. 返回匹配到的第一个索引
int i = str.IndexOf(charOrString);
int i = str.IndexOf(charOrString, startIndex);
int i = str.IndexOf(charOrString, startIndex, searchLength);
int i = str.LastIndexOf(charOrString);
int i = str.LastIndexOf(charOrString, startIndex);
int i = str.LastIndexOf(charOrString, startIndex, searchLength);
11. 装换数字
string s = "123";
int i = int.Parse(s); // string.IsNullOrWhiteSpace(s) 不能为空,或存在非数字的其他字符
12. 前端截取
CharacterEllipsis 在字符边界修整文本,用...代替
WordEllipsis 在单词边界修整文本,用...代替
<TextBlock TextTrimming="None/CharacterEllipsis/WordEllipsis" />
13. 字体
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- 引入字体 -->
<ResourceDictionary>
<FontFamily x:Key="Consolasss">/Presentation/Resources/font/#consolas</FontFamily>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources> C:\Windows\Font\xx.TTF
根目录下 /Presentation/Resources/font/xx.TTF,代码是写成 #xx

2. DateTime

DateTime d1 = DateTime.Now;              //{2019/9/17 20:58:56}
DateTime d2 = DateTime.UtcNow; //{2019/9/17 12:58:56}
int d3 = DateTime.DaysInMonth(2000, 11); //30
TimeSpan d4 = DateTime.Now.TimeOfDay; //{20:58:56.6098659}
DateTime ds = DateTime.Now.AddYears(-3).AddMonths(3).AddDays(-1).AddHours(3).AddMinutes(5).AddSeconds(3).AddMilliseconds(10);
TimeSpan d5 = DateTime.Now - ds;//{1005.20:54:56.9900000}
= DateTime.Now.Subtract(ds); 1. DateTime to String
DateTime dt = DateTime.Now;
string str = dt.ToString("yyyy-MM-dd HH:mm:ss"); //2019-09-17 21:05:55
string str = dt.ToString(); //2019/9/17 21:05:55
string str = string.Format("{0:yyyy-MM-dd}", dt);//2019-09-17
string str = string.Format("{0:yyyy-MM-dd HH:mm:ss}", dt);//2019-09-17 21:05:55 string str = dt.ToShortDateString(); // 2019/09/17
string str = dt.ToShortTimeString(); // 21:07
string str = dt.ToLongDateString(); // 2019年9月17日
string str = dt.ToLongTimeString(); // 21:07:39 2. String to DateTime
string str = "2019-09-17";
string str = "2019-09-17 21:12:04";
string str = "2019/09/17 21:12:04";
DateTime dt = Convert.ToDateTime(str); 3. DateTime to 时间戳
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); //{18156.21:20:31}
var df = Convert.ToInt64(ts.TotalSeconds).ToString(); //"1568755231" 4. TimeSpan to DateTime
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); //{18156.21:20:31}
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0) + ts; //{2019/9/17 21:20:31} 5. 时间戳 to DateTime
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
double sec = Convert.ToDouble(ts.TotalSeconds); //1568755231
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); //{1970/1/1 0:00:00}
dateTime = dateTime.AddSeconds(sec); // {2019/9/17 21:20:31}

3. TimeSpan

public static TimeSpan FromDays(double value);
public static TimeSpan FromHours(double value);
public static TimeSpan FromMinutes(double value);
public static TimeSpan FromSeconds(double value);
public static TimeSpan FromMilliseconds(double value);
public static TimeSpan FromTicks(long value); var aa = DateTime.Now.Ticks; //637359305824346507
var bb = TimeSpan.FromTicks(aa); //{737684.09:09:42.4346507}

4. enum

enum Day { a = 8, b = 6, c, e, f = 7, g };
int a = (int)Day.a; //8
int b = (int)Day.e; //8
int c = (int)Day.g; //8
int d = (int)Day.f; //7
如果自身没有被赋值,就从上一个有赋值的开始算起,第一个默认为 0。

5. XAML 综合使用

<TextBlock Text="{Binding Num, Source={x:Static local:Common.Obj}, StringFormat={}{0}辆}" />
<TextBlock Text="{Binding ToDate, Source={x:Static local:Common.Obj}, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" />
<TextBlock Text="{Binding ToDate, Source={x:Static local:Common.Obj}, StringFormat=yyyy-MM-dd HH:mm:ss:fff}" /> public static class Common
{
public static Object Obj { get; set; }
}
public partial class Window2 : Window
{
public Window2()
{
Common.Obj = new { Num = 34 , ToDate = DateTime.Now};
InitializeComponent();
}
}

C# 基础 - string 和 Datetime的更多相关文章

  1. string转DateTime(时间格式转换)

    1.不知道为什么时间在数据库用varchar(8)来保存,例如"19900505",但是这样的保存格式在处理时间的时候是非常不方便的. 但是转换不能用Convert.ToDateT ...

  2. Java基础String的方法

    Java基础String的方法 字符串类型写法格式如下: 格式一: String 变量名称; 变量名称=赋值(自定义或传入的变量值); 格式二: String 变量名称=赋值(自定义或传入的变量值); ...

  3. Java基础 String 裸暴力算法- 五个小练习

      之间的博客,承上启下:    Java基础 String/StringBuff 常用操作方法复习/内存分析 Java数组直接选择排序.sort()排序 Java基础 String 算法 - 五个练 ...

  4. Java基础—String构造方法

    Java基础--String构造方法 public String(): 创建一个空表字符串对象,不包含任何内容 public String(char[]chs): 根据字符数组的内容,来创建字符串对象 ...

  5. 【2-26】string/math/datetime类的定义及其应用

    一string类 (1)字符串.Length    Length作用于求字符串的长度,返回一个int值 (2)字符串.TrimStart();  TrimStart():可删除前空格,返回一个stri ...

  6. 十四、Java基础---------String、StringBuffer、StringBuilder基本应用

    在前面的博客中曾提及Java的数据类型分为基本数据类型,和引用数据类型,而String便是最常见的应用数据类型,本文将着重介绍这一引用数据类型的用法. String 字符串     String类是对 ...

  7. Java基础-String、StringBuffer、StringBuilder

    看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; ...

  8. C# - string 转为 DateTime(自定义)

    上代码: string dt = " 1 11 1961"; DateTime day; System.Globalization.DateTimeFormatInfo dtFor ...

  9. 再探Java基础——String.format(String format, Object… args)的使用

    最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...

随机推荐

  1. Gome 高性能撮合引擎微服务

    Gome 高性能撮合引擎微服务 使用 Golang 做计算,gRPC 做服务,ProtoBuf 做数据交换,RabbitMQ 做队列,Redis 做缓存实现的高性能撮合引擎微服务 依赖 具体依赖信息可 ...

  2. Ubuntu Live CD联网修复

    此模式下可以联网修复ubuntu系统下绝大多数问题.进入LIVE CD模式,打开终端执行以下命令: #此处/dev/sda1为ubuntu根分区,工作中根据实际分区情况更改 sudo mount /d ...

  3. 用阿里云ecs部署kubernetes/K8S的坑(VIP、slb、flannel、gw模式)

    1 阿里云ecs不支持keepalived vip 1.1 场景描述 本来计划用keepalived配合nginx做VIP漂移,用以反代多台master的apiserver的6443端口,结果部署了v ...

  4. Hexo-使用阿里iconfont图标

    Hexo-使用阿里iconfont图标 因为使用hexo搭建的博客中,大家并不懂都有什么图标,fa fa-xx就懵了,不知道都有什么. 首先,fa fa-xxx中的图标可以在 图标库 中寻找. (上面 ...

  5. Leetcode(712)-账户合并

    给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址 ...

  6. Leetcode(213)-打家劫舍II

    你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...

  7. GPU 加速 & WebGL

    GPU 加速 & WebGL 开启 GPU 加速, 硬件加速 垃圾面试官,瞎忽悠 holy shit 美国想象力英语,前端 leader WebGL 加速 ??? 是什么鬼 ??? three ...

  8. website text select notes menu

    website text select notes menu website 文字选择笔记菜单(下划线, 标记, 复制, 分享) 下划线, 标记 https://time.geekbang.org/ ...

  9. npm install 原理

    npm install 原理 https://docs.npmjs.com/about-npm/ npm consists of three distinct components: the webs ...

  10. asm align 对齐数据

    最大成员dword data: dd 1 db 2 align 4 dw 3 000E0010 - 01 00 00 00 000E0014 - 02 00 00 00 000E0018 - 03 0 ...