医疗网站模板免费下载北京专业网站维护公司
web/
2025/10/7 13:37:03/
文章来源:
医疗网站模板免费下载,北京专业网站维护公司,只会后端不会前端如何做网站,深圳ui设计公司ToUpper()/ToLower()
作用#xff1a;将字符串中字符转换为大写/小写字符#xff0c;仅对字符有效#xff0c;返回转换后的字符串。
使用#xff1a;字符串变量名.ToUpper() / 字符串变量名.ToLower()
使用实例如下#xff1a;
using System;
using System.Collection…ToUpper()/ToLower()
作用将字符串中字符转换为大写/小写字符仅对字符有效返回转换后的字符串。
使用字符串变量名.ToUpper() / 字符串变量名.ToLower()
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s abcDE123;Console.WriteLine(s.ToUpper());Console.WriteLine(s.ToLower());Console.ReadKey();}}
}
输出结果 ABCDE123 abcde123 Equals()
作用比较两字符串是否相同是则返回真否则返回假。
使用字符串1.Equals(字符串2)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s1, s2, s3;s1 12345;s2 12345;s3 l2345;Console.WriteLine(s1.Equals(s2));Console.WriteLine(s2.Equals(s3));Console.ReadKey();}}
}
s1与s2中内容均为12345而s3中首字符为英文小写字母ls1、s2相同s3与s1、s2不相同则输出结果为 True False Split()
作用根据所选择的字符对字符串分割返回字符串类型的数组。
使用将分割的字符串.Split(用于分割的字符数组)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 111,22,3333.4,555;char[] c new char[2] { ,, . };string[] sArray s.Split(c);foreach(string strIter in sArray){Console.WriteLine(strIter);}Console.ReadKey();}}
}
Split根据字符,与.将字符串s分割后依次存入字符串数组sArray最后输出结果为 111 22 3333 4 555 Substring()
作用从某一下标开始截取字符串返回截取后的字符串。
使用被截取的字符串.Substring(截取起点索引值[,截取字符串长度])
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 1234567;Console.WriteLine(s.Substring(1));Console.WriteLine(s.Substring(1,4));Console.ReadKey();}}
}
第一次截取从索引值为1的位置开始截取至字符串结束第二次截取从索引值为1的位置开始截取4个字符长度的字符串。输出结果如下 234567 2345 IndexOf()/LastIndexOf()
作用查找子字符串在字符串中第一次/最后一次出现位置索引如果未找到返回-1。
使用字符串.IndexOf(查找的子字符串)/字符串.LastIndexOf(查找的子字符串)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 12345671234567;Console.WriteLine(s.IndexOf(234));Console.WriteLine(s.LastIndexOf(234));Console.WriteLine(s.IndexOf(57));Console.ReadKey();}}
}
第一次查找找到了234返回第一次出现234时2的索引第二次查找找到了234返回最后一次出现234时2的索引第三次查找没有找到57返回-1。最终结果 1 8 -1 StartsWith()/EndsWith()
作用判断字符串是否以所查找的字符串开头/结束是则返回True否则返回False。
使用字符串变量.StartsWith(子字符串)/字符串变量.EndsWith(子字符串)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 12345671234567;Console.WriteLine(s.StartsWith(1234)); //s以1234为起始返回TrueConsole.WriteLine(s.EndsWith(567)); //s以567结束返回TrueConsole.WriteLine(s.StartsWith(57)); //s不以57为起始返回FalseConsole.ReadKey();}}
}
最终输出结果为 True True False Replace()
作用将指定字符串中子字符串s1更换为子字符串s2返回更新后的字符串。
使用更新字符串.Replace(s1,s2)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 12345671234567;Console.WriteLine(s.Replace(1234,000)); //将s中所有的1234子字符串更换为000Console.ReadKey();}}
}
最终输出为 000567000567 此处可见实际上Replace将字符串中全部匹配的子字符串都进行了更换。
Contains()
作用判断字符串中是否存在某一子字符串是则返回True否则返回False。
使用字符串变量.Contains(子字符串)
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s 12345671234567;Console.WriteLine(s.Contains(1234)); //s中存在1234返回TrueConsole.WriteLine(s.Contains(000)); //s中不存在000返回FalseConsole.ReadKey();}}
}
输出结果如下 True False Trim()/TrimEnd()/TrimStart()
作用去除字符串首尾/尾部/首部空格返回去除空格后的字符串。
使用字符串变量.Trim()/字符串变量.TrimEnd()/字符串变量.TrimStart()
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s string ;Console.WriteLine(\ s.Trim() \); //去除s首尾空字符Console.WriteLine(\ s.TrimEnd() \); //去除s尾部空字符Console.WriteLine(\ s.TrimStart() \); //去除s首部空字符Console.ReadKey();}}
}
输出结果如下 string string string IsNullOrEmpty()
作用判断字符串是否为空字符串或者为null是则返回True否则返回False。
注意字符串为空字符串与为null不同。空字符串占有内存空间null则不占有。
使用string.IsNullOrEmpty(字符串变量) 此处使用方法与前面几个函数不同
使用实例如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace code
{class Program{static void Main(string[] args){string s1 string ; //字符串非空string s2 null; //字符串为nullstring s3 ; //字符串为空字符串Console.WriteLine(string.IsNullOrEmpty(s1));Console.WriteLine(string.IsNullOrEmpty(s2));Console.WriteLine(string.IsNullOrEmpty(s3));Console.ReadKey();}}
}
输出结果如下 False True True
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88505.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!