C#入门篇6-2:字符串操作 string常用的函数
2024-08-25 08:49:55
//String 字符串的常见操作 public static void Fun1() { string MyStr = " Hello World! "; //length长度属性 Console.WriteLine(MyStr.Length); //Substring()截取 Console.WriteLine(MyStr.Substring(, )); //ToCharArray()转换为字符数组 char[] charr = MyStr.ToCharArray(); //ToUpper()转换大写 string UpperStr = MyStr.ToUpper(); Console.WriteLine("转换为大写:" + UpperStr + "\n"); //ToLower()转换小写 Console.WriteLine("转换为小写:" + MyStr.ToLower()); //TrimStart() and TrimEnd() 去掉字符左右边的空格 TrimStart--去掉左边 string LefStr = MyStr.TrimStart(); string RigStr = MyStr.TrimEnd(); Console.WriteLine("左边 " + LefStr + "\n" + "右边 " + RigStr + "\n"); //Replace()替换字符 string ReplaceStr = MyStr.Replace('W', 'M'); Console.WriteLine("替换字符 " + ReplaceStr + "\n"); //Concat()连接字符 string concatStr = string.Concat(MyStr, "asas", "asasa"); Console.WriteLine("连接字符 " + concatStr + "\n"); //IsNullOrEmpty()判断空值 bool NullorEmpty = string.IsNullOrEmpty(MyStr); Console.WriteLine("判断空值 " + NullorEmpty.ToString() + "\n"); //StartsWith()开头 Console.WriteLine(MyStr.StartsWith(" ") == true); //EndsWith()结尾 bool endwith = MyStr.EndsWith(" "); Console.WriteLine("结尾 " + endwith.ToString() + "\n"); //IndexOf() and LastIndexOf()字符串的位置 string strLocation = "123abc4c56efd"; Console.WriteLine(') + "\n"); Console.WriteLine("LastIndexOf:" + strLocation.LastIndexOf('c') + "\n"); //插入字符 Console.WriteLine(MyStr.Insert(, "W")); //MyStr.PadLeft();MyStr.PadRight() //Remove()移除 Console.WriteLine(, ) + "\n"); //Format()格式化 ); Console.WriteLine(str); //Compare()和CompareTo() 比较方法 string strA = "File.exe"; "; int numA = string.Compare(strA, strB); Console.WriteLine("Compare: " + numA.ToString() + "\n"); int numB = strA.CompareTo(strB); Console.WriteLine("CompareTo: " + numB.ToString()); //比较两个指定的 System.String 对象。 // 返回结果: // 一个 32 位有符号整数,指示两个比较数之间的词法关系。 //值条件小于零 strA 小于 strB。 零strA 等于 strB。 大于零 strA大于 strB。 }
常用的就这些吧,有好的话我在补上!!!
最新文章
- 使用python处理子域名爆破工具subdomainsbrute结果txt
- Fedora 23安装 NS2 (network simulator 2)
- C#基础-可访问性-public、ptotected、protected internal、internal、private
- 14周事情总结-机器人-大数据hadoop
- SOA和微服务
- 弹出层easydialog-v2.0
- Maven简介(一)
- 安装mysql到服务器的linux环境下
- genymotion的安装及运行
- Android中资源的引用
- Vue 组件&;组件之间的通信 之 使用slot分发内容
- Flutter 布局控件完结篇
- oracle日志归档空间清理
- abc高级bash shell编程
- 软件包管理:rpm命令管理-包命名与依赖性
- 知乎回答:每日完成任务用于打卡的APP
- Linux之静态库与动态库20160706
- mysql 查询条件
- Java面向对象作业-用接口方式测试向下转型
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-通知(Advice)API
热门文章
- web开发模式小结:页面乱码和跳转
- redis在Windows下以后台服务一键搭建集群(单机--伪集群)
- 更改placeholder样式
- Python Visual Studio 2015
- org.hibernate.HibernateException: Unable to get the defa
- IOS UITabBarController(控制器)的子控制器
- Linux I/O调度
- UVA 1213 - Sum of Different Primes(递推)
- kubernetes-ingress(十)
- 漫谈Ajax在.Net中的使用