string扩展方法使用
2011-12-28 23:46
260 查看
创建一个 static 的类,并且里面的方法也必须是static的,第一个参数是被扩展的对象,必须标注为this,使用时,必须保证namespace using进来了.
下面为实例:
调用
下面为实例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace stringHelper扩展方法1 { static class StringHelper { public static bool IsEmail(this string s) { if (s.Contains("@")) { return true; }else return false; } public static string FKH(this string s) { return "[" + s + "]"; } } }
调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using stringHelper扩展方法1; namespace stringHelper扩展方法 { class Program { static void Main(string[] args) { Console.WriteLine("请输入一个Email邮箱"); string s = Console.ReadLine(); bool b= s.IsEmail(); if (b) { Console.WriteLine("是正确邮箱"); } else { Console.WriteLine("不是正确的邮箱"); } Console.WriteLine(s.FKH().FKH().FKH().FKH()); Console.ReadKey(); } } }
相关文章推荐
- 关于使用扩展方法的警告
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- String类型的方法使用
- C++ stringstream介绍,使用方法与例子
- String使用equals方法和==的区分
- String.format函数使用方法介绍
- macOS 中使用 phpize 动态添加 PHP 扩展的错误解决方法
- String.xml 中%1$s %1$d 的使用方法
- String.valueOf() 方法的使用
- 使用扩展方法添加到String类中简化脚本注入处理
- 使用String.split方法时要注意的问题
- JSP中String的成员方法的使用
- c#使用扩展方法,实现对接口的扩展
- Silverlight使用DownloadStringAsync方法下载数据03
- MVC 中使用扩展方法
- cocos2d-x使用extension扩展包的方法
- 艾伟_转载:c#扩展方法奇思妙用变态篇四:string 的翻身革命
- 使用C#的扩展方法替代For循环
- (转)C++ stringstream介绍,使用方法与例子
- java中使用String的replace方法替换html模板保存文件