66. 在C#里模拟LotusScript里的字符串截取函数

来源:互联网 发布:mac word 剪切板 编辑:程序博客网 时间:2024/06/04 18:10

LotusScript里的字符串处理函数在截取子字符串方面很方便好用。笔者数年前曾经比较三种脚本语言中的这类字符串处理函数,LotusScript的最全面实用。即使在C#这样新且类库设计优良的语言里,字符串类也没有类似的方法。

Vbscript

Javascript

LotusScript

LotusScript里函数的功能

Left

slice

Left

截取字符串最左边的指定长度的子字符串。

Right

substr

Right

截取字符串最右边的指定长度的子字符串。

Mid

substring

Mid

截取字符串指定位置起的指定长度的子字符串。

 

 

Strleft

在字符串S1里从左往右查找字符串S2,返回S1中位于S2左边的子字符串。

 

 

Strleftback

在字符串S1里从右往左查找字符串S2,返回S1中位于S2左边的子字符串。

 

 

Strright

在字符串S1里从左往右查找字符串S2,返回S1中位于S2右边的子字符串。

 

 

Strrightback

在字符串S1里从右往左查找字符串S2,返回S1中位于S2右边的子字符串。

LotusScript的这些函数使用习惯了,用其他语言的时候就会很想念。于是笔者便在C#里创建了一个StringUtility类,模拟了Strleft、Strleftback、Strright和Strrightback这些函数:

    //Mimic LotusScript string functions   public class StringUtility    {       #region Left function   //参数source为源字符串,value为要查找的子字符串,include指示返回的结果是否包含要查找的子字符串。以下几个方法的参数含义类似。       public static string Left(string source,string value,bool include)       {           int pos = source.IndexOf(value);           if (pos > -1)           {               if (include)               {                   return source.Substring(0, pos + value.Length);               }               else               {                   return source.Substring(0, pos);               }           }           else           {               return "";           }       }      //上面方法的重载,只接受两个字符串参数,返回的结果不包括要查找的子字符串。       public static string Left(string source, string value)       {           return Left(source, value, false);       }      //将第一个Left方法的第二个字符串参数重载为字符类型。       public static string Left(string source, char value,bool include)       {           return Left(source, Convert.ToString(value), include);       }       public static string Left(string source, char value)       {           return Left(source, value, false);       }        #endregion       #region Right() function       public static string Right(string source, string value, bool include)       {           int pos = source.IndexOf(value);           if (pos > -1)           {               if (include)               {                   return source.Substring(pos);               }               else               {                   return source.Substring(pos + value.Length);               }           }           else           {               return "";           }       }       public static string Right(string source, string value)       {           return Right(source, value, false);       }       public static string Right(string source, char value, bool include)       {           return Right(source, Convert.ToString(value), include);       }       public static string Right(string source, char value)       {           return Right(source, value, false );       }       #endregion       #region LeftBack() function       public static string LeftBack(string source, string value, bool include)       {           int pos = source.LastIndexOf(value);           if (pos > -1)           {               if (include)               {                   return source.Substring(0, pos + value.Length);               }               else               {                   return source.Substring(0, pos);               }           }           else           {               return "";           }       }       public static string LeftBack(string source, string value)       {           return LeftBack(source, value, false);       }       public static string LeftBack(string source, char value, bool include)       {           return LeftBack(source, Convert.ToString(value), false);       }       public static string LeftBack(string source, char value)       {           return LeftBack(source, value, false );       }       #endregion       #region RightBack() function       public static string RightBack(string source, string value, bool include)       {           int pos = source.LastIndexOf(value);           if (pos > -1)           {               if (include)               {                   return source.Substring(pos);               }               else               {                   return source.Substring(pos + value.Length);               }           }           else           {               return "";           }       }       public static string RightBack(string source, string value)       {           return RightBack(source, value, false);       }       public static string RightBack(string source, char value, bool include)       {           return RightBack(source, Convert.ToString(value), include);       }       public static string RightBack(string source, char value)       {           return RightBack(source, value, false );       }       #endregion   }

有兴趣的朋友也可以在其他语言里模拟LotusScript的这些函数。


原创粉丝点击