挖土

Coding for fun.
posts - 29, comments - 20, trackbacks - 0, articles - 23
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

给string加几个扩展方法

Posted on 2010-03-31 14:58 挖土. 阅读(124) 评论(3) 编辑 收藏

 

代码
    public static class StringExtend
    {
        
public static bool IsNullOrEmpty(this string s)
        {
            
return string.IsNullOrEmpty(s);
        }

        
public static bool IsInt(this string s)
        {
            
int i;
            
return int.TryParse(s, out i);
        }

        
public static int ToInt(this string s)
        {
            
return int.Parse(s);
        }
    }

 

 

Feedback

#1楼  回复 引用 查看   

2010-04-01 17:34 by 沭阳      
int.Parse() 会发生异常

#2楼[楼主]  回复 引用 查看   

2010-04-01 20:18 by 挖土.      
引用沭阳:int.Parse() 会发生异常


调用的时候不能直接调用 ToInt()

string stringNum="1234";
int iNum;
if(stringNum.IsInt())
{
iNum = stringNum.ToInt();
}

#3楼[楼主]  回复 引用 查看   

2010-04-01 20:20 by 挖土.      
引用沭阳:int.Parse() 会发生异常


而且,你调用ToInt 的时候出现异常也是正确的异常,那是你的程序的逻辑问题。否则 Int.Parse就不该存在了。