当前位置: 首页 > 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/news/288498.html

相关文章:

  • 关于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++ 设计模式——职责链模式
  • Go父类调用子类方法(虚函数调用)
  • stm32之I2C通信外设
  • 提升RAG检索回答质量: Shortwave的 4 大优化指南
  • 使用 Milvus Lite、Llama3 和 LlamaIndex 搭建 RAG 应用
  • 住宅IP与机房IP:哪种更适合业务应用?
  • 51单片机-第十节-独立按键及数码管优化
  • shell脚本—————局域网IP扫描
  • 开放式耳机漏音有多大?五大超值爆款推荐!
  • 【数据可视化技术】使用Matplotlib、Seaborn进行数据可视化
  • 9、Django Admin优化查询