C# 基础 - string 和 Datetime

2021-03-05 00:27

阅读:432

标签:ring   one   图片   addm   with   rtti   OLE   开始   todo   

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 strList = new List() { "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 在单词边界修整文本,用...代替
13. 字体
/Presentation/Resources/font/#consolas

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 综合使用




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

标签:ring   one   图片   addm   with   rtti   OLE   开始   todo   

原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14474776.html


评论


亲,登录后才可以留言!