首页 > 开发 > 综合 > 正文

HOW TO:操作定长字符串

2024-07-21 02:29:22
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

public class ustringclass ustring
    private shared gencoding as system.text.encoding = system.text.encoding.getencoding("gb2312")

    public shared property encoding()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()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 char) 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

测试对比:

    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"
        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, 20, "*"c))
        console.writeline(tmp.padleft(20, "*"c))

        console.writeline(lzmtw.ustring.padright(tmp, 20, "*"c))
        console.writeline(tmp.padright(20, "*"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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表