vbs 字符串

用vbs写一个方法 函数名和参数我已经定掉了 mode = 1 左对齐 mode = 2 右对齐 strRetLen 函数返回的string长度 targetStr 要转换的目标string 要实现的功能是对于我要转换的string,根据左对齐或者右对齐来补足space返回 比如 StrConvert(“123”,8,1) 则返回「123 」 StrConvert(“123”,8,2) 则返回「 123」 如果要转换的string长度比要返回的长度长,则按左对齐或者右对齐去截掉 StrConvert(“123456”,3,1) 则返回「123」 StrConvert(“123456”,3,2) 则返回「456」 Function StrConvert(ByVal targetStr,ByVal strRetLen,ByVal mode) End Function

方法一

Function StrConvert(ByVal targetStr,ByVal strRetLen,ByVal mode)
Dim i      '目标字符串长度
Dim j      '目标字符串长度和要返回的长度之差
Dim result '输出结果
 
i=len(targetStr)
j=strRetLen-i 
 
If i<=strRetLen  Then

Select Case mode
Case 1 
'输出靠左的,不够的空格补齐
result=targetStr+space(j)
Case 2
'输出靠右的,不够的空格补齐
result=space(j)+targetStr
End Select


Else

        Select Case mode
Case 1 
'使用函数left,输出靠左的,多出来的直接去掉
result=left(targetStr,strRetLen)
        Case 2
'使用函数right,输出靠右的,多出来的直接截掉
result =right(targetStr,strRetLen)
End Select

    End If


End Function

方法二

Function StrConvert(ByVal targetStr,ByVal strRetLen,ByVal mode)

Dim re     '补空格
Dim result '输出最终字符串


    Select Case mode
 
        Case 1
re=targetStr+space(strRetLen)
result=left(re,strRetLen)

Case 2
re=space(strRetLen)+targetStr
result=right(re,strRetLen)
End Select
  

End Function

方法三

Option Explicit


'+++++++++ System Settings +++++++++++++++++++++++++++++++++
''VBS名
Const MODE1 = 1
Const MODE2 = 2


'**************************************************************
'z+++++++++ Script Start ++++++++++++++++++++++++++++++++++++
'Main関数の呼び出し
Call Main()


'@***********************************************************
''メイン関数<br>
'@z***********************************************************
Function Main()
 Call StrConvert("1237",10,1)
End Function


Function StrConvert(ByVal targetStr,ByVal strRetLen,ByVal mode)


 Dim targetLen 
 Dim Absvalue 
 Dim value : value = ""
 
 targetLen = Len(targetStr)
 Absvalue = Abs(targetLen - strRetLen)
 
 If targetLen = strRetLen Then 
  value = targetStr
 End If 
 
 If targetLen > strRetLen and mode = MODE1 Then 
  value = Left(targetStr,strRetLen)
 End If 
 
 If targetLen > strRetLen and mode = MODE2 Then 
  value = Right(targetStr,strRetLen)
 End If
  
 If targetLen < strRetLen and mode = MODE1 Then 
  value = targetStr&Space(Absvalue) 
 End If 
 
 If targetLen < strRetLen and mode = MODE2 Then 
  value = Space(Absvalue)&targetStr
 End If 
 
 MsgBox value
 
End Function




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值