//String 常用函数 //1.Contains(是否包含XX字符串) //2.StartsWith(是否以XX开头) //3.EndsWith(是否以XX结尾) //4.IndexOf(获取某个字符在字符串中第一次出现的位置) //5.LastIndexOf(获取某个字符在字符串中最后一次出现的位置) //6.SubString(截取字符串) //7.ToLower(字符串变为大写) //8.ToUpper(字符串转大写) //9.Replace(替换字符串) //10.Trim(去掉首尾空格) //案例一:判断是否是网址:以http://开头,以.com结尾 /* string s = "http://www.baidu.com"; if (s.StartsWith("http://") && s.EndsWith(".com")) { Console.WriteLine("合法网址"); } else { Console.WriteLine("非法网址"); } Console.ReadKey(); */ //案例二:判断邮箱是否合法 用户名是否有敏感词汇 /* string name = "XXX万岁"; string email = "[email protected]"; if (name.Contains("邓小平")||name.Contains("主席")) { Console.WriteLine("用户名有敏感词汇!"); } else if (email.EndsWith("@qq.com")) { Console.WriteLine("不支持QQ邮箱!"); } else { Console.WriteLine("注册成功!"); } Console.ReadKey(); */ /* string a = "abcdeeadsa"; Console.WriteLine(a.IndexOf(‘e‘)); Console.WriteLine(a.IndexOf("cd")); Console.WriteLine(a.LastIndexOf(‘a‘)); */ /* string a = "http://www.baidu.com:8080"; Console.WriteLine(a.Substring(0, 5)); Console.WriteLine(a.Substring(4)); Console.ReadKey(); */ //案例三:获取文件名及后缀名 /* string fileName = "[ads-108]苍井空.avi"; int dotIndex = fileName.IndexOf(‘.‘); string name = fileName.Substring(0, dotIndex); Console.WriteLine(name); string h = fileName.Substring(dotIndex + 1); Console.WriteLine(h); string a = fileName.Substring(0, fileName.LastIndexOf(‘]‘) + 1); Console.WriteLine(a); */ //练习一:从“http://www.rupeng.com:8090/a.htm”获取域名和端口号 /* string yu = "http://www.rupeng.com:8090/a.htm"; string y = yu.Substring(yu.IndexOf(‘w‘),yu.LastIndexOf(‘:‘)-7); int l = yu.LastIndexOf(‘/‘); string d = yu.Substring(0, yu.LastIndexOf(‘/‘)); string d1 = d.Substring(d.LastIndexOf(‘:‘)+1); Console.WriteLine(y); Console.WriteLine(d1); Console.ReadKey(); */ /* string a = "HELLO"; string b = a.ToLower();//生成新的字符串 Console.WriteLine(b); Console.ReadKey(); */ /* string a = "hello"; string r = a.Replace(‘l‘, ‘L‘); Console.WriteLine(r); Console.ReadKey(); */ /* string a = "领导";//字符串对象不可变性,一但声明就不能改变 string b = a.Replace("领导", "***"); Console.WriteLine(b); Console.ReadKey(); */ string a = " 我 "; string b = a.Trim(); Console.WriteLine(b); Console.ReadKey();
时间: 2024-10-06 19:46:56