收藏 分销(赏)

字符串的常用方法说明.doc

上传人:仙人****88 文档编号:9467483 上传时间:2025-03-27 格式:DOC 页数:9 大小:44KB 下载积分:10 金币
下载 相关 举报
字符串的常用方法说明.doc_第1页
第1页 / 共9页
字符串的常用方法说明.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
1、 IndexOf()(搜索字符串) 非静态方法,返回String中的每一个或多个字符在此字符串中的第一个匹配项的索引。 2、 Contains() 非静态方法,返回一个值,该值指示指定的String对象是否出现在此字符串中。 3、 Join() (string.Join(“”,a)) 串联对象数组中的各个元素,其中在每个元素之间使用指定的分隔符 4、//求字符串的长度(Length) //string str="123345-"; //Console.WriteLine("请输入一个字符串?"); //string str = Console.ReadLine(); //Console.WriteLine(str.Length); //Console.ReadLine(); 5、//IsNullOrEmpty()---指示指定的字符串是null,还是System.String.Empty字符串,返回值是bool类型 //string.IsNullOrEmpty(null); //静态方法,判断为null或者为””(静态方法) 类名.静态方法 //字符串是不可变的,有时需要改变字符串中的某些值,这时就转换成char数组 6、//ToCharArray() 将string转换为char[] 7、 // ToLower() 小写,必须接收返回值。(因为:字符串的不可变); //string str1 = "ABCD"; //string str2 = str1.ToLower(); //Console.WriteLine(str2); //Console.ReadLine(); 8、//ToUpper() 大写 ToUpper()方法和ToLower()方法都没有参数 //string str1 = "abcd"; //string str2 = str1.ToUpper(); //Console.WriteLine(str2); //Console.ReadLine(); 9、//Equals() 比较两个字符串是否相同,忽略大小写的比较 //string str1 = new string(new char[]{'a','b','c'}); //string str2 = new string(new char[]{'a','b','c'}); //字符串特殊.=== //if (str1.Equals(str2))//通过Equals判断对象是同一个对象也布准确了 //{ // Console.WriteLine("对象相同"); //} //else //{ // Console.WriteLine("不是相同的对象"); //} //Console.ReadLine(); 10、 //IndexOf() 如果没有找到对应的数据,返回-1. (搜索字符串) //面试题:统计一个字符串中,”天安门”出现的次数 //字符串的搜索 //string str = "北京传智播客软件培训,传智播客.net培训,传智播客Java培训。传智播客官网,北京传智播客欢迎您"; //string KeyWord = "传智播客"; //int index = 0; //定义索引 //int count = 0; //记录次数 ////int index=str.IndexOf(KeyWord ,0); //while ((index = str.IndexOf(KeyWord, index)) != -1) //{ // count++; // Console.WriteLine("传智播客出现的索引位置为:{0},共出现了{1}次", index, count); // index = index + KeyWord.Length; //} //Console.WriteLine("========================="); //Console.WriteLine("一共出现了{0}次", count); //Console.ReadLine(); 11、//LastIndexOf() 如果没有找到对应的数据,返回-1 (搜索字符串) //字符串方法IndexOf()和LastIndexOf()可以得到目标字符串第一个和最后一个出现的一个字符或字符的位置 //string str = "C# makes string easy."; //int a = str.IndexOf("ke"); //int b = str.LastIndexOf("s"); //Console.WriteLine("ke第一次出现的位置{0}",a); //Console.WriteLine("s最后一次出现的位置{0}",b); //Console.ReadLine(); 12、//Substring() //2个重载,截取字符串。 //string str1 = "123456789"; ////string str2 = str1.Substring(3); //string str2 = str1.Substring(3,4); //Console.WriteLine(str2); //Console.ReadLine(); 13、//Split() //分割字符串。//可以重载 //string str= "12=++345===678=====9"; ////StringSplitOptions.RemoveEmptyEntries 前面的参数就必须是char数组 ////char[] ch = { '='}; ////string[] chars = str.Split(ch, StringSplitOptions.RemoveEmptyEntries); //string[] chars = str.Split(new char[]{'=','+'},StringSplitOptions.RemoveEmptyEntries); //切掉"=" 以后有空白 //char[]数组,数组就可以for循环 //for (int i = 0; i < chars.Length ; i++) //{ // Console.WriteLine(chars[i]); //} //Console.WriteLine(); //Console.ReadLine(); 14、 //Join()---串联对象数组中的各个元素,其中在每个元素之间使用指定的分隔符 静态方法 //string str= "12=++345===678=====9"; //string[] chars = str.Split(new char[]{'=','+'},StringSplitOptions.RemoveEmptyEntries); //切掉"=" 以后有空白 //char[]数组,数组就可以for循环 //string st=string.Join("好",chars); //Join(string n,params object[] values)可变数组 //Console.WriteLine(st); //for (int i = 0; i < chars.Length ; i++) //{ // Console.WriteLine(chars[i]); //} //Console.WriteLine(); //Console.ReadLine(); 15、 //Format() 静态方法 //类似 Console.ReadLine(); //将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式,复合格式字符串 //string st=string.Format("{0},{1},{2}", 1, 2, 3); //WinForm中输出用到 MessBox() //三层,数据库,ADO.Net会用到 //Console.WriteLine("{0},{1},{2}", 1, 2, 3); ////Console.WriteLine(st); //Console.ReadLine(); 16、 //字符串的连接 string str1="One"; string str2=str1+"Two"+"Three"+"Four"; Console.WriteLine(str2 ); Console.ReadLine(); 17、////Replace() ===(字符串替换) //Console.WriteLine("请输入一个字符串?"); //string name = Console.ReadLine(); ////怎么判断一个字符串有没有其它字符 ////Contains() 非静态方法,返回一个值,该值指示指定的String对象是否出现在此字符串中 ////IndexOf()(搜索字符串) //if (name.Contains("MN")) //{ // string newStr = name.Replace("MN", "LYQ")+"***"; // Console.WriteLine(newStr); //} //else //{ // Console.WriteLine("没有合适的替换!"); //} //Console.ReadLine(); //注意:对字符串的操作一定要用变量接收返回值。(字符串是不能变的,每次都是构建一个新的字符串) string str="a"; //开辟一个空间存a str="b"; //又开辟一个空间存b,不再指向a,而指向b,不可变是指:str []="b"; str=str.Replace('c','C'); 18、//Insert()---在字符串中插入另一个字符串 //string str1 = "123456789"; //string str2=str1.Insert(4,"hello"); //Console.WriteLine(str2); //Console.ReadLine();
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服