中英文数字混合的复合格式处理

Author: 水如烟

曾写过一个How TO:操作字长字符串,现在重新整理,使更方便些.
Public   Class  uString

    
Private   Sub   New ()
    
End Sub

    
Private   Shared  gEncoding  As  System.Text.Encoding  =  System.Text.Encoding.GetEncoding( " GB2312 " )

    
Public   Shared   Property  Encoding()  As  System.Text.Encoding
        
Get
            
Return  gEncoding
        
End   Get
        
Set ( ByVal  value  As  System.Text.Encoding)
            gEncoding 
=  value
        
End   Set
    
End Property

    
Public   Shared   Function  Length( ByVal  s  As   String As   Integer
        
Return  gEncoding.GetByteCount(s)
    
End Function

    
Public   Shared   Function  PadLeft( ByVal  s  As   String ByVal  totalWidth  As   Integer ByVal  paddingChar  As   Char As   String
        
Return  s.PadLeft(GetValidNum(totalWidth  +  s.Length  -  Length(s)), paddingChar)
    
End Function

    
Public   Shared   Function  PadLeft( ByVal  s  As   String ByVal  totalWidth  As   Integer As   String

        
Return  s.PadLeft(GetValidNum(totalWidth  +  s.Length  -  Length(s)))
    
End Function

    
Public   Shared   Function  PadRight( ByVal  s  As   String ByVal  totalWidth  As   Integer ByVal  paddingChar  As   Char As   String
        
Return  s.PadRight(GetValidNum(totalWidth  +  s.Length  -  Length(s)), paddingChar)
    
End Function

    
Public   Shared   Function  PadRight( ByVal  s  As   String ByVal  totalWidth  As   Integer As   String
        
Return  s.PadRight(GetValidNum(totalWidth  +  s.Length  -  Length(s)))
    
End Function

    
Public   Shared   Function  SubString( ByVal  Line  As   String ByVal  Index  As   Integer ByVal   Len   As   Integer As   String
        
Dim  num  As   Integer   =  Length(Line)
        
Return  gEncoding.GetString(gEncoding.GetBytes(Line), Index,  Len )
    
End Function

    
Private   Shared   Function  GetValidNum( ByVal  num  As   Integer As   Integer
        
If  num  <   0   Then
            
Return   0
        
Else
            
Return  num
        
End   If
    
End Function

    
Public   Shared   Function   Format ( ByVal  sformat  As   String ByVal  arg0  As   Object As   String
        
Return   Format (sformat,  New   Object () {arg0})
    
End Function

    
Public   Shared   Function   Format ( ByVal  sformat  As   String ByVal  arg0  As   Object ByVal  arg1  As   Object As   String
        
Return   Format (sformat,  New   Object () {arg0, arg1})
    
End Function

    
Public   Shared   Function   Format ( ByVal  sformat  As   String ByVal  arg0  As   Object ByVal  arg1  As   Object ByVal  arg2  As   Object As   String
        
Return   Format (sformat,  New   Object () {arg0, arg1, arg2})
    
End Function

    
Public   Shared   Function   Format ( ByVal  sformat  As   String ByVal  args  As   Object ())  As   String
        
Return  myFormat(sformat, args)
    
End Function

    
Private   Shared   Function  myFormat( ByVal  sformat  As   String ByVal  args  As   Object ())  As   String
        
Dim  mysformat  As   String   =  sformat

        
' 以下匹配字符串,找的是复合格式{index[,alignment][:formatString]}有alignment的完整项{}
         Dim  mPattern  As   String   =   " {(/d+)([/s]*,[/s]*[-]?)(/d+)(.*?)} "
        
Dim  mMatchs  As  System.Text.RegularExpressions.MatchCollection
        mMatchs 
=  System.Text.RegularExpressions.Regex.Matches(mysformat, mPattern)

        
' 处理各匹配项,重置各项长度
         For   Each  m  As  System.Text.RegularExpressions.Match  In  mMatchs

            
Dim  s  As   String   =  m.Groups( 0 ).Value  ' 匹配完整项
             Dim  index  As   Integer   =  Convert.ToInt32(m.Groups( 1 ).Value)  ' 格式索引
             Dim   len   As   Integer   =  Convert.ToInt32(m.Groups( 3 ).Value)  ' 原固定长度

            
Dim  NowLen  As   Integer   =  AligLen( len , args(index))  ' 现固定长度
             ' 以下重置固定长度
             Dim   replace   As   String   =   " {${1}${2} "   &  NowLen.ToString  &   " ${4}} "
            
Dim  Nows  As   String   =  m.Result( replace )
            mysformat 
=  mysformat.Replace(s, Nows)
        
Next

        
Return   String .Format(mysformat, args)
    
End Function

    
'混合 字符文本固定长度的重置
     Private   Shared   Function  AligLen( ByVal   len   As   Integer ByVal  arg  As   Object As   Integer
        
Return   len   -  Length(arg.ToString)  +  arg.ToString.Length
    
End Function
End Class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值