常用的字符串操作方法,DateTime与TimeSpan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 职称枚举类型
{

    class Class1
    {
       
       


        static void Main(string[] args)
        {


            string str1 = "this is world";
            string str2 = "th,is";
            Console.WriteLine(string.Equals(str1,str2));//str1和str2是否相等
            Console.WriteLine(str1.IndexOf("is"));//is在str1中第一次出现的位置。
            Console.WriteLine(str1.LastIndexOf("is"));//is在str1中最后一次出现的位置

            Console.WriteLine(str1.Insert(2,"as"));//在2处插入字符串as
            Console.WriteLine(str1.Remove(1,2));//去掉1开始的2个字符
            Console.WriteLine(str1.Replace("is","de"));//第一个参数是被替换。
            
            //Split分离的是字符串中存在空的地方
            string[] str = str1.Split(',');
            for (int i = 0; i < str.Length;i++ )
            {
                Console.WriteLine(str[i]);

            }
            /*
             * 注意,Split(',')表示对字符串中有逗号的分割,
             *      结果是:
             *      th
                    is,因为th和is之间存在逗号。
             */
            string[] str3 = str2.Split(',');
            for (int i = 0; i < str3.Length; i++)
            {
                Console.WriteLine(str3[i]);

            }
            //复制到字符数组,将字符串转换成数组,这样就可以用for循环挨个将字符取出。
            /*l
              o
              v
              e*/
            string str4 = "love";
            Char[] charArray = str4.ToCharArray();
            for (int i = 0; i < charArray.Length;i++ )
            {
                Console.WriteLine(charArray[i]+" ");

            }


            //大小写转换
            Console.WriteLine(str4.ToUpper());
            Console.WriteLine(str4.ToLower());
            /*
             * 结果:
             * LOVE
               love
             * */


            /* DateTime和TimeSpan*/
            DateTime d1 = new DateTime(
                2018,//year
                12,//month
                12,//day
                23,//hours
                12,//minute
                12,//second
                11//millisecond

                );
            DateTime d2 = new DateTime(2018,12,5);
            DateTime d3 = DateTime.Now;//取出当前的日期和时间。
            d3.ToLongDateString();
            Console.WriteLine("哈哈哈" + string.Format("{0:F}", d3));//哈哈哈2018年6月7日 17:49:13
            Console.WriteLine(d1);//2018/12/12 23:12:12,
            Console.WriteLine(d3);//2018/6/7 17:13:10,
            Console.WriteLine(d2);//2018/12/5 0:00:00
            Console.WriteLine("{0:F}", d1);//2018年12月12日 23:12:12
            Console.WriteLine("{0}", d2);// 2018/12/5 0:00:00
            Console.WriteLine("{0}--{1}--{2}--{3}", d1.Year, d1.Month, d1.Day, d1.DayOfWeek);//2018--12--12--Wednesday
            
            //TimeSpan
            TimeSpan ts1 = d1.TimeOfDay;//当天的时间,就是d1的时间中的当天的时间点。
            TimeSpan ts2 = d1 - d2;
            //TimeSpan中的Days表示时间间隔的天数部分。
            Console.WriteLine("{0}--{1}--{2}", ts1, ts2, ts2.Days);//23:12:12.0110000--7.23:12:12.0110000--7

            string s1 = d1.ToLongDateString();//2018年12月12日
            string s2 = d1.ToShortDateString();//2018/12/12
            string s3 = string.Format("{0:yyyy-mm-dd}",d2);
            Console.WriteLine("{0}--{1}--{2}", s1, s2, s3);//2018年12月12日--2018/12/12--2018-00-05
            
        
        
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值