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

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.RegularExpressionsapp.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.RegularExpressionsstring ReplaceMatchCase(Match m){// Test whether the match is capitalizedif (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.mrgr.cn/news/18606.html

相关文章:

  • MovieSum:大型复杂文本摘要数据集,提供格式化剧本信息以及维基摘要 | ACL 2024
  • ‘rimraf‘ 不是内部或外部命令,也不是可运行的程序
  • 57-java csrf防御方案
  • 学习笔记---自动驾驶
  • 亚马逊云注册就送小礼品,数量充足,耳机键盘等你来拿!
  • 集合框架,List常用API,栈和队列初识
  • Android U 多任务启动分屏——Launcher流程(更新中)
  • Linux中MFS分布式文件系统(实战教程)全网最详细
  • debian系统安装mysql
  • 5、Django Admin后台移除“删除所选”操作
  • PrimeTime low power-多电压设计流程(3)
  • STM32开发资料
  • 共创AI+ 数智新引擎—2024 大模型创新应用线下沙龙上海站圆满落幕
  • VI改造计划补充篇
  • 科研|基于SprinBoot+vue的科研管理系统(源码+数据库+文档)
  • 世平安森美 NFAL5065L4BT IPM 应用于1500W 热泵热水器压缩机驱动器的方案介绍
  • 短信群发技术指南(106短信群发必掌握)
  • 零风险!零付费!我把 AI 接入微信群,爸妈玩嗨了~附教程(上):高德 API 接入
  • 微信小程序请求数据接口封装
  • Linux驱动(三):字符设备驱动之杂项