任何等效于 PadLeft/PadRight 的方法? [英] Any method equivalent to PadLeft/PadRight?

查看:31
本文介绍了任何等效于 PadLeft/PadRight 的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道,VBA 中是否有与 VB .NET 的 PadLeft 和 PadRight 方法等效的方法?

Just wondering, is there any equivalent in VBA to VB .NET's PadLeft and PadRight methods?

截至目前,每当我想获取一个字符串并使其具有前导空格的固定长度时,我会根据字符串的长度执行 For...Next 循环.

As of right now, whenever I want to take a string and make it a fixed length with leading spaces, I do a For...Next loop based on the string's length.

例如,我将使用以下代码将字符串格式化为带有前导空格的 8 个字符:

For example, I would use the following code to format a string to 8 characters with leading spaces:

intOrdNoLen = Len(strOrdNo)
For i = 1 To (8 - intOrdNoLen) Step 1
    strOrdNo = " " & strOrdNo
Next

有没有办法在 VBA 中用更少的行来做同样的事情?

Is there a way to do this same thing in fewer lines in VBA?

推荐答案

我不相信有任何显式的 PADLEFTPADRIGHT 函数,但是您可以使用SPACELEFTRIGHT 的组合以在字符串前添加空格,然后获取正确的 X 个字符.

I don't believe there are any explicit PADLEFT or PADRIGHT functions, but you can use a combination of SPACE and LEFT or RIGHT to prepend spaces to your string, and then grab the right X number of characters.

PADLEFT

strOrdNo = RIGHT(Space(8) & strOrdNo, 8)

如果你想要一个字符而不是空格,你可以使用 STRING 代替空格(下面的例子用 X 左填充):

If you want a character instead of spaces, you can use STRING instead of space (the example below left-pads with X):

strOrdNo = RIGHT(String(8, "X") & strOrdNo, 8)

PADRIGHT

strOrdNo = LEFT(strOrdNo & Space(8), 8)

strOrdNo = LEFT(strOrdNo & String(8, "X"), 8)

这篇关于任何等效于 PadLeft/PadRight 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆