How to: Manipulate fixed-length strings

Source: Internet
Author: User
Tags integer ustring
String

Public Class Ustringclass ustring
Private Shared gencoding as System.Text.Encoding = System.Text.Encoding.GetEncoding ("GB2312")

Public Shared Property Encoding () 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 () function length (ByVal s as String) as Integer
return Gencoding.getbytecount (s)
End Function

Public Shared function PadLeft () function PadLeft (ByVal s as String, ByVal totalwidth as Integer, ByVal Paddingchar as Char ) as String
Return S.padleft (Totalwidth + s.length-length (s), Paddingchar)
End Function

Public Shared function PadLeft () function PadLeft (ByVal s As String, ByVal totalwidth as Integer) as String
Return S.padleft (Totalwidth + s.length-length (s))
End Function

Public Shared function PadRight () function padright (ByVal s as String, ByVal totalwidth as Integer, ByVal Paddingchar as Ch AR) as String
Return S.padright (Totalwidth + s.length-length (s), Paddingchar)
End Function

Public Shared function PadRight () function padright (ByVal s As String, ByVal totalwidth as Integer) as String
Return S.padright (Totalwidth + s.length-length (s))
End Function

Public Shared function SubString () function SubString (ByVal line as String, ByVal Index As Integer, ByVal Length As Integer ) as String
Return gencoding.getstring (Gencoding.getbytes (line), Index, Length)
End Function

End Class

Test comparison:

Private Sub Toolstripbutton6_click () Sub Toolstripbutton6_click (ByVal sender as System.Object, ByVal e As System.EventArgs) Handles Toolstripbutton6.click
Dim tmp as String = "China Chinese"
Console.WriteLine (LzmTW.uString.Encoding.EncodingName)


Console.WriteLine (LzmTW.uString.Length (TMP))
Console.WriteLine (TMP. Length)

Console.WriteLine (TMP. Substring (0, 2))
Console.WriteLine (LzmTW.uString.SubString (TMP, 0, 2))


Console.WriteLine (New String ("*" C, 20))

Console.WriteLine (LzmTW.uString.PadLeft (TMP, "*" C))
Console.WriteLine (TMP. PadLeft ("*" C))

Console.WriteLine (LzmTW.uString.PadRight (TMP, "*" C))
Console.WriteLine (TMP. PadRight ("*" C))

Console.WriteLine (LzmTW.uString.PadLeft (TMP, 20))
Console.WriteLine (TMP. PadLeft (20))

Console.WriteLine (LzmTW.uString.PadRight (TMP, 20))
Console.WriteLine (TMP. PadRight (20))

End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.