字符串长度的问题
两个字符串
“abcd”
"abcdef "
我想将这两个字符串都出换成固定的长度,如下:
“abcd ”
“abcdef ”
请问如何做???
[解决办法]
楼主用我写的函数吧,呵呵.
Public Class Form1
Dim a, b, c, d As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
a = "abcd "
b = "abcdef "
c = "abcdefgh "
d = "abcdefghijklmn "
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(SetStringLength(a, 8) & "| ")
MsgBox(SetStringLength(b, 8) & "| ")
MsgBox(SetStringLength(c, 8) & "| ")
MsgBox(SetStringLength(d, 8) & "| ")
End Sub
Function SetStringLength(ByVal str As String, ByVal length As Int32) As String
If length > str.Length Then
Dim s As New String( " ", length - str.Length)
Return str & s
ElseIf length < str.Length Then
Return str.Remove(length, str.Length - length)
Else
Return str
End If
End Function
End Class
[解决办法]
RSet( "abcd ", 10)