Author:
水如烟
曾写过一个How TO:操作字长字符串,现在重新整理,使更方便些.
曾写过一个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
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的完整项{
![](http://www.cnblogs.com/Images/dot.gif)
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