c#——字符串及字符串常用函数


c#——字符串及字符串常用函数

一、字符串的不可变性

(1)字符(char)和字符串(string)的区别(如例1)。

  • char不可以什么都不写(不可为空),string可以什么都不写(可以为空)。
  • 1char有且只能有1个字符。
  • 1个字符串可以有1字符,可以有很多个字符组成的,也可以为(字符串的长度可大可小)。

例1:

namespace 字符串1 { class Program { static void Main(string[] args) { //1个char有且只能有一个字符(不可以为空)。 char c1='a'; char c2='b'; //1个字符串可以有1个字符,可以有很多个字符组成的,还可以为空。 string s1 = ""; string s2 = "a"; string s3="abcd"; } } }

使用s.length属性来获得字符串中的字符个数。

charc=s[1]通过索引来只读指写位置的char(如例2)

例2:

namespace 字符串1 { class Program { static void Main(string[] args) { //遍历字符串中的每一个无素 string s = "hello"; Console.WriteLine(s.Length); //5, s.Length获取字符串的字符个数 Console.WriteLine(s[1]); //e, s[i]通过索引读取指写位置的字符char char c3 = s[4]; //string可以看做是char的只读数组 Console.WriteLine(c3); //o for (int i = 0; i < s.Length; i++) //遍历字符串s中的每一个字符(从前向后) { char c = s[i]; Console.WriteLine(c); } for (int i = s.Length - 1; i >= 0; i--) //遍历字符串s中的每一个字符(从后向前) { Console.WriteLine(s[i]); } //s[0] = 'y'; //不能赋值(只能读,不能写) Console.ReadKey(); } } }

例3:将字符串中的h替换为a

namespace 字符串2 { class Program { static void Main(string[] args) { //将字符串中的h替换为a string s1 = "hello"; char[] chars = s1.ToCharArray(); //用s.ToCharArray()方法得到字符串的char数组 chars[0]='a'; //对char数组进行修改 string s2=new string(chars); //调用new string(char[])这个构造函数来创建char数组的字符串 Console.WriteLine(s1); //hello,改变chars并不会改变s1,因为chars是一份拷贝(体现了字符串的一重要特性:不可变性)。 Console.WriteLine(s2); //aello Console.ReadKey(); } } }

c#中字符 串有一重要的特性:不可变性,字符串一旦声明就不再可以改变。

如果要对char进行修改,那么就必须创建一个新的字符串,用s,ToCharArray()方法得到字符串的char数组,对数组进行修改后,调用new string(char[])这个构造函数来创建char数组的字符串。

(2)变量名和变量指向的值的区别:

字符串不可变性指的是内存中的字符串不可变,而不是变量不变。(如例4,例5)

例4:

namespace 字符串2 { class Program { static void Main(string[] args) { string s1 = "hello"; Console.WriteLine(s1); s1 = "yello"; Console.WriteLine(s1); Console.ReadKey(); } } }


例5:

namespace 字符串3 { class Program { static void Main(string[] args) { string s1 = "hello"; string s10 = s1; //s10指向s1指向的字符串,而不是s10指向s1,哪怕s1以后指向了其它内存,那么s10还是指向"hello" char[] chars = s1.ToCharArray(); chars[0] = 'a'; s1 = new string(chars); Console.WriteLine(s1); //aello Console.WriteLine(s10); //hello, 过河拆桥,和s1的改变没有任何的关系 Console.ReadKey(); } } }


二、字符串类常用函数:

字符串的常用函数:

ToLower():得到字符串的小写形式

ToUpper():得到字符串的大写形式

s1.Equals(s2, StringComparison.OrdinalIgnoreCase),两字符串进行比较,忽略大小写的比较。

例6:

namespace 字符串4 { class Program { static void Main(string[] args) { #region 将字符串"Hello"中所有字符全部转化为小写、大写 string s1 = "Hello"; /* string s2 = s1.ToLower(); Console.WriteLine(s1); //Hello Console.WriteLine(s2); //hello */ s1 = s1.ToLower();//并不是改变了字符串的内容,而是生成一个新的全部变为小写的字符串,然后用s1指向这个新的字符串 Console.WriteLine(s1); //hello Console.WriteLine(s1.ToUpper()); //HELLO #endregion #region 去掉字符串" a b "两端的空格 string s3 = " a b "; /* string s4 = s3.Trim(); Console.WriteLine("|{0}|",s3); //| a b | Console.WriteLine("|{0}|",s4); //|a b| */ s3 = s3.Trim(); Console.WriteLine("|{0}|", s3); //|a b| #endregion /* bool b = ("abc" == "ABC"); Console.WriteLine(b); //False */ #region 忽略大小写的比较 //Ignore忽略,Case大小写 //==是区分大小写的比较,Equals("ABC", StringComparison.OrdinalIgnoreCase)是忽略大小写的比较 bool b = "abc".Equals("ABC", StringComparison.OrdinalIgnoreCase); Console.WriteLine(b); #endregion Console.ReadKey(); } } }

三、字符串的分割

字符串的分割 详解如上图,实例如例7

例7:

namespace 字符串5 { class Program { static void Main(string[] args) { #region 按指定的分割符分割字符串数组 string s1 = "aaa,bbb,ccc,ddd.hello|yes,no"; string[] strs = s1.Split(',','.','|'); //可按多个分割符进行拆分 foreach (string item1 in strs) { Console.WriteLine(item1); } #endregion #region 移除空白字符串 /* string s2 = "aaa,bb,cc,,12,3"; string[] strs2 = s2.Split(','); foreach (string item2 in strs2) { Console.WriteLine(item2); } */ string s2 = "aaa,bb,cc,,12,3"; string[] strs2 = s2.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries); foreach (string item2 in strs2) { Console.WriteLine(item2); } #endregion #region 按照1个字符串来进行分割的方法 string s3 = "我是刘海燕我是马兆娟我是蒋倩兰"; string[] str3 = s3.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries); foreach (string item3 in str3) { Console.WriteLine(item3); } #endregion Console.ReadKey(); } } }

string[] lines = System.IO.File.ReadAllLines(@"C:\root.ini", Encoding.Default);从文本文件中读取数据,返回值为string类组,每个元素为一行(如例8)。

例8:从文本文件1.txt中读取数据(1.txt的存储路径为:F:\其它文档\总结\传智博客.net培训 实例练习\1.txt)

namespace 字符串5 { class Program { static void Main(string[] args) { //从文本文件1.txt中读取数据 string[] lines = System.IO.File.ReadAllLines(@"F:\其它文档\总结\传智博客.net培训 实例练习\1.txt", Encoding.Default); foreach (string item in lines) { Console.WriteLine(item); } Console.ReadKey(); } } }


字符串函数

字符串替换:string Replace(string oldValue,string newValue)

例9:名字替换:将李时珍替换为李素丽

namespace Replace函数 { class Program { static void Main(string[] args) { //名字替换(将李时珍替换为李素丽) string s = "李时珍是一个好同志,李时珍是一个好医生,向李时珍学习!"; s = s.Replace("李时珍","李素丽"); Console.WriteLine(s); Console.ReadKey(); } } }

例10:SubString函数:取子字符串

namespace SubString函数 { class Program { static void Main(string[] args) { //SubString()1个参数的重载函数 string s = "http://www.baidu.com"; string 域名 = s.Substring(7); //从指定的序号开始一直到最后的子字符串 Console.WriteLine(域名); //SubString()2个参数的重载函数 string s1 = s.Substring(7,5); //SubString第二个参数指的是截取多长,而不是结束位置 Console.WriteLine(s1); //string s2 = s.Substring(7, 100); //如果Length参数超过了长度,就会报错 //Console.WriteLine(s2); Console.ReadKey(); } } }


例11:Contains函数:判断字符串中是否含有子字符串value

namespace Contains函数 { class Program { static void Main(string[] args) { string s = "我们的社会真和谐啊!"; if (s.Contains("社会") || s.Contains("和谐")) { Console.WriteLine("含有敏感词汇,请文明用语!"); } Console.ReadKey(); } } }

例12:s.StartsWith 判断字符串是否以子串value开始,s.EndsWiths 判断字符串是否以子串value结束

namespace StartsWith函数 { class Program { static void Main(string[] args) { string s = "http://www.cctv.com"; //s.StartsWith 判断字符串是否以子串value开始 if (s.StartsWith("http://") || s.StartsWith("https://")) { Console.WriteLine("是网址"); } else { Console.WriteLine("不是网址"); } //s.EndsWiths 判断字符串是否以子串value结束 if (s.EndsWith("www.baidu.com")) { Console.WriteLine("是百度"); } else { Console.WriteLine("不是百度"); } Console.ReadKey(); } } }

例13:IndexOf函数:取子串value第一次出现的位置,如果不存在,则返回-1。

namespace IndexOf函数 { class Program { static void Main(string[] args) { //IndexOf函数,取子串第一次出现的位置,如果不存在,则返回-1。 string s = "您好,我是刘海燕"; int i1 = s.IndexOf("我是"); //返回子字符串的第一个字符第一次出现的位置 int i2 = s.IndexOf("你是"); //如果不存在,则返回-1 Console.WriteLine(i1); //3,子串“我是”第一次出现的位置是3 Console.WriteLine(i2); //-1,子串不存在“你是”,返回-1 Console.ReadKey(); } } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值