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

zy89、90_C#中字符串及控制字符串的常用函数

文章目录

  • 1.字符串
    • 1.1程序代码
  • 2.控制字符串的常用函数
    • 2.1程序代码

1.字符串

1.1程序代码

static void Main(string[] args)
{//********************string类*******************string sentence = "Hello!";char letter = sentence[0];Console.WriteLine(letter);char[] letter1 = { 'C', 'H', 'I', 'C', 'K', 'E', 'N' };string ss = new string(letter1);Console.WriteLine(ss);string ss1 = new string('A', 10);//重复执行10遍Console.WriteLine(ss1);Console.WriteLine("The letters in \"Hello\" are:");for (int i = 0; i < sentence.Length; i++){char letter2 = sentence[i];Console.Write(letter2 + "\t");}//********************复制字符串*******************// =:string sentence1 = "就快被融化";string sentence2 = sentence1;//sentence1 = "再靠近一点";Console.WriteLine("\n{0}\n{1}", sentence1, sentence2);//Copy()string sentence3 = "再多看一眼";string sentence4 = string.Copy(sentence3);Console.WriteLine(sentence3);Console.WriteLine(sentence4);//CopyTo()string source = "someone";char[] destination = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' };int sourceIndex = 2;int destinationIndex = 3;int count = 2;source.CopyTo(sourceIndex, destination, destinationIndex, count);for (int i = 0; i < destination.Length; i++){Console.Write(destination[i]);}//********************比较字符串*******************//1.相等运算符==string string5 = "Hello";string string6 = "hello";if (string5 == string6){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//2.Equsls()string string7 = "Hello";if (string7.Equals("Hello")){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//3.静态Equal()string string8 = "Dog";string string9 = "dog";if (string.Equals(string8, string9)){Console.WriteLine("\nEqual");}else{Console.WriteLine("\nUnequal");}//4.CompareTo()string string10 = "better";string string11 = "best";switch (string10.CompareTo(string11)){case -1: Console.WriteLine(string10 + "<" + string11); break;case 0: Console.WriteLine(string10 + "=" + string11); break;case 1: Console.WriteLine(string10 + ">" + string11); break;default: Console.WriteLine("********"); break;}
}

2.控制字符串的常用函数

2.1程序代码

static void Main(string[] args)
{验证字符串的首尾//1.StartWith()判断字符串是否以特定的子串开头string[] strings = { "started", "starting", "ended", "ending" };for (int i = 0; i < strings.Length; i++){if (strings[i].StartsWith("st")){Console.WriteLine("\"{0}\" starts with \"st\"", strings[i]);}}//2.EndsWith()判断字符串是否以特定的子串结尾for (int i = 0; i < strings.Length; i++){if (strings[i].EndsWith("ed")){Console.WriteLine("\"{0}\" starts with \"ed\"", strings[i]);}}定位字符或子串//1.IndexOf()找到指定字符或子串在字符串中第一次出现的位置。如果找不到,返回-1string sentence = "You love to hear Let her go.";int index1 = sentence.IndexOf('o', 3);//从第三个字符之后开始找指定元素int index2 = sentence.IndexOf("You", 20);Console.WriteLine(index1);Console.WriteLine(index2);//2.LastIndexOf()找到指定字符或子串在字符串中最后一次出现的位置。如果找不到,返回-1int index3 = sentence.IndexOf('o', 6);//从后向前搜索int index4 = sentence.IndexOf("You", 20,4);//向前搜索4个字符Console.WriteLine(index3);Console.WriteLine(index4);//3.IndexOfAny()同时搜索多个指定的字符,搜索到任意一个停止char[] searchLetters= { 'e', 'h' };int index5 = sentence.IndexOfAny(searchLetters);int index6 = sentence.IndexOfAny(searchLetters,10);//第十个元素开始int index7 = sentence.IndexOfAny(searchLetters,10,3);Console.WriteLine(index5);Console.WriteLine(index6);Console.WriteLine(index7);//4.LastIndexOfAny()int index8 = sentence.LastIndexOfAny(searchLetters);int index9 = sentence.LastIndexOfAny(searchLetters, 10);int index10 = sentence.LastIndexOfAny(searchLetters, 10, 3);Console.WriteLine(index8);Console.WriteLine(index9);Console.WriteLine(index10);///截取子串Substring()string sub=sentence.Substring(12);//截取12之后的字符串Console.WriteLine(sub);string sub1=sentence.Substring(5,4);//在第5个之后截取4个Console.WriteLine(sub1);//拆分字符串Split()char[] seperator = { ' ','o' };//遇到字符断开string[] words=sentence.Split(seperator);foreach (string word in words){Console.WriteLine(word);}//更改大小写string string1 = "ok";string string2=string1.ToUpper();Console.WriteLine(string2);string string3 = string2.ToLower();Console.WriteLine(string3);//修改字符串string string4 = "tree";string string5 = string4.Insert(1, "h");Console.WriteLine(string5);string string6 = string4.Replace('e', 'd');Console.WriteLine(string6);string string7 = string4.Replace("re", "ffd");Console.WriteLine(string7);string string8 = string4.Remove(2);//2以后所有元素Console.WriteLine(string8);string string9=string4.Remove(1,2);//1以后的2个字符Console.WriteLine(string9);插入格式化变量int i1 = 360;int j1 = 60;string result = string.Format("{0,6}\n+{1,5}\n----------\n{2,6}", i1, j1, i1 + j1);//{0}6个空间Console.WriteLine(result);
}

相关文章:

  • vue3中动态引入组件并渲染组件
  • DC00023基于jsp+MySQL新生报到管理系统
  • 聊一聊大模型六小虎生存现状!
  • 8.代码风格调试%结课竞赛
  • 大厂面试真题:简单说下Redis的bigkey
  • Unity3D PostLateUpdate为何突然占用大量时间详解
  • CC-LINK IE Field Basic通讯设置
  • Redis --- redis事务和分布式事务锁
  • 用友U8+CRM leadconversion.php SQL注入复现
  • Linux进程的学习(持续更新)
  • C++:类中的特殊关键字,运算重载符
  • Groupby_SQL和pandas等效例子
  • 数据结构7—树(顺序存储二叉树—堆)含TOPK问题
  • OpenCV视频I/O(1)视频采集类VideoCapture介绍
  • YOLOv9改进,YOLOv9主干网络替换为GhostNetV3(2024年华为提出的轻量化架构,全网首发),助力涨点
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【Linux系统编程】快速查找errno错误码信息
  • ➹使用webpack配置多页面应用(MPA)
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • HTML中设置input等文本框为不可操作
  • js写一个简单的选项卡
  • Meteor的表单提交:Form
  • ng6--错误信息小结(持续更新)
  • Python打包系统简单入门
  • Python利用正则抓取网页内容保存到本地
  • REST架构的思考
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • v-if和v-for连用出现的问题
  • Zsh 开发指南(第十四篇 文件读写)
  • 初识MongoDB分片
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 如何进阶一名有竞争力的程序员?
  • 算法-插入排序
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 正则与JS中的正则
  • MyCAT水平分库
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • $.ajax()方法详解
  • (4) PIVOT 和 UPIVOT 的使用
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (二)linux使用docker容器运行mysql
  • (二十四)Flask之flask-session组件
  • (附源码)计算机毕业设计ssm电影分享网站
  • (篇九)MySQL常用内置函数
  • (实测可用)(3)Git的使用——RT Thread Stdio添加的软件包,github与gitee冲突造成无法上传文件到gitee
  • (详细文档!)javaswing图书管理系统+mysql数据库
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)ObjectiveC 深浅拷贝学习
  • (转)Oracle存储过程编写经验和优化措施
  • .md即markdown文件的基本常用编写语法
  • .Net Core 中间件验签