当前位置: 首页 > article >正文

024集—— 正则表达式、replace、DateTime日期的用法——C#学习笔记

DateTime 是一个struct结构体。

代码如下:

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

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
             args = new string[] { "yngqq","qq" };
            // To run this program, provide a command line string.
            // In Visual Studio, see Project > Properties > Debug.
            string userName = args[0];
            string date = DateTime.Now.ToString (); //Today.ToShortDateString();

            // Use the + and += operators for one-time concatenations.
            string str = "你好," + userName + ",现在是 " + date + "。";
            System.Console.WriteLine(str);

            str += " 你好吗?";
            System.Console.WriteLine(str);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

    }
}

 

以下是正则表达式及replace的用法:

 

 

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

namespace ConsoleApp1
{
    class ReplaceSubstrings
    {
        string searchFor;
        string replaceWith;

        static void Main(string[] args)
        {

            ReplaceSubstrings app = new ReplaceSubstrings();
            string s = "齐天大圣 孙悟空 到此一游!哈哈";
            Console.WriteLine(s);
            // Replace one substring with another with String.Replace.
            // Only exact matches are supported.
            s = s.Replace("孙悟空", "孙行者");
            Console.WriteLine(s);
            // Output: The peaks are behind the clouds today.

            // Use Regex.Replace for more flexibility. 
            // Replace "the" or "The" with "many" or "Many".
            // using System.Text.RegularExpressions
            app.searchFor = "齐天大圣"; //一个简单的正则表达式.
            app.replaceWith = "美猴王";
            s = Regex.Replace(s, app.searchFor, app.ReplaceMatchCase, RegexOptions.IgnoreCase);
            Console.WriteLine(s);
            // Output: Many peaks are behind many clouds today.

            // Replace all occurrences of one char with another.
            s = s.Replace(' ', '_');
            Console.WriteLine(s);
            s = s.Replace("!", "");
            Console.WriteLine(s);
            // Output: Many_peaks_are_behind_many_clouds_today.

            // Remove a substring from the middle of the string.
            string temp = "一游";
            int i = s.IndexOf(temp);
            if (i >= 0)
            {
                s = s.Remove(i, temp.Length);
            }
            Console.WriteLine(s);
            // Output: Many_peaks_are_behind_clouds_today.

            // Remove trailing and leading whitespace.
            // See also the TrimStart and TrimEnd methods.
            string s2 = "    二师兄,我来了      ";
            // Store the results in a new string variable.
            temp = s2.Trim();
            Console.WriteLine(temp);
            // Output: I'm wider than I need to be.

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        // Custom match method called by Regex.Replace
        // using System.Text.RegularExpressions
        string ReplaceMatchCase(Match m)
        {
            // Test whether the match is capitalized
            if (Char.IsUpper(m.Value[0]) == true)
            {
                // Capitalize the replacement string
                // using System.Text;
                StringBuilder sb = new StringBuilder(replaceWith);
                sb[0] = (Char.ToUpper(sb[0]));
                return sb.ToString();
            }
            else
            {
                return replaceWith;
            }
        }
    }

}


http://www.kler.cn/a/288498.html

相关文章:

  • 马斯克万卡集群AI数据中心引发的科技涟漪:智算数据中心挑战与机遇的全景洞察
  • MySql结合element-plus pagination的分页查询
  • MyBatis CRUD快速入门
  • C# 委托与匿名方法
  • DApp开发:定制化解决方案与源码部署的一站式指南
  • 开源 2 + 1 链动模式、AI 智能名片、S2B2C 商城小程序在用户留存与品牌发展中的应用研究
  • 关于SSL认证后出现的http和https无法同时访问问题
  • 【原创】edge-tts与基于mpv的edge-playback,使命令行和Python的Text To Speech唾手可得
  • SDN架构详解
  • 黄仁勋预言步入现实 谷歌展示实时游戏生成AI模型GameNGen
  • ELAU伺服控制器
  • 《从C/C++到Java入门指南》- 23.关键字及其新特性
  • 【PyTorch】安装,环境搭建
  • 【Netty】实战:基于Http的Web服务器
  • 数据分析及应用:如何分析基于绝对中位差的异常值检测问题?
  • LINUX网络编程:Tcpsocket封装
  • Java GC机制:Minor GC与Full GC的触发条件
  • 假期作业--数据结构
  • uniapp插槽用法
  • vue子组件样式影响父组件
  • 每天一个数据分析题(五百一十六)- 贝叶斯分类算法
  • Axure打造科技感数据可视化大屏原型
  • 网络安全宗旨和目标
  • OpenCV颜色空间转换(1)颜色空间转换函数cvtColor()的使用
  • 【论文阅读】skill code 和 one-shot manipulate
  • C++ 设计模式——职责链模式