找回密码
 加入
搜索
查看: 2420|回复: 11

[AU3基础] 【已解决】如何按数字给字符串变量两端加上空格

[复制链接]
发表于 2020-6-17 20:15:36 | 显示全部楼层 |阅读模式
本帖最后由 chishingchan 于 2020-6-22 11:22 编辑

例如:排版字符串是12个英文字符长度,如果$s字符串变量值(abcdef)的长度是6,需要将$s字符串变量值两端各加3个 空格,
有什么函数能搞定?谢谢!

粗浅代码
$S = "字符串Abc"
$C = 12                                                                                ;空间长度
$I = "M"                                                                        ;左L 中M 右R 对齐

MsgBox(0, "处理结果: " & _BinaryLen(_AddSpace($S, $C, $I)), Chr(34) & _AddSpace($S, $C, $I) & Chr(34))

Func _AddSpace($String, $Length, $LMR)
        $SLBL = _BinaryLen($String)                                ;中文字占用长度
        $SLCN = StringLen($String)                                ;中文字计算长度
;        $CN = $SLBL - $SLCN                                         ;中文字数
        Switch $LMR
                Case "L"                                                        ;左对齐
                        $RS = $Length - $SLBL                        ;右侧空格数
                        $LS = 0
                        Return StringFormat('%-' & $SLCN + $RS & 's', $String)
                Case "M"                                                        ;中对齐
                        $RS = Int(($Length - $SLBL)/2)        ;右侧空格数
                        $LS = $Length - $SLBL - $RS                ;左侧空格数
                        $L = StringFormat('%-' & $SLCN + $RS & 's', $String)
                        $R = StringFormat('%' & $SLCN + $LS + $RS & 's', $L)
                        Return $R
                Case "R"                                                        ;右对齐
                        $LS = $Length - $SLBL                        ;左侧空格数
                        $RS = 0
                        Return StringFormat('%' & $SLCN + $LS & 's', $String)
        EndSwitch
EndFunc

Func _BinaryLen($Expression)
        $Len = StringLen(StringReplace(StringToBinary($Expression, 1), "0x", ""))/2
        Return $Len
EndFunc

发表于 2020-6-17 20:35:00 | 显示全部楼层
这个量级需要什么算法,直接加空格组合就是…
如果只有5位,两边不等分呢?一边加3,一边加4?
 楼主| 发表于 2020-6-17 20:39:54 | 显示全部楼层
afan 发表于 2020-6-17 20:35
这个量级需要什么算法,直接加空格组合就是…
如果只有5位,两边不等分呢?一边加3,一边加4?
 
Func _AddSpace($String, $Length)
$LS = Ceiling(($Length-_BinaryLen($String))/2);左侧空格数
$RS = Round(($Length-_BinaryLen($String))/2,0);右侧空格数
EndFunc

Func _BinaryLen($Expression)
        $Len = StringLen(StringReplace(StringToBinary($Expression, 1), "0x", ""))/2
        Return $Len
EndFunc
我是这样处理的,但只是返回空格的数字!
 楼主| 发表于 2020-6-17 20:44:33 | 显示全部楼层
afan 发表于 2020-6-17 20:35
这个量级需要什么算法,直接加空格组合就是…
如果只有5位,两边不等分呢?一边加3,一边加4?


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2020-6-17 20:48:11 | 显示全部楼层

你是为了等宽字体占位的显示效果?
 楼主| 发表于 2020-6-17 20:54:55 | 显示全部楼层
afan 发表于 2020-6-17 20:48
你是为了等宽字体占位的显示效果?

是的,有没有好办法?
 楼主| 发表于 2020-6-21 20:44:34 | 显示全部楼层
本帖最后由 chishingchan 于 2020-6-22 11:23 编辑
$S = "字符串Abc"
$C = 12                                                                                ;空间长度
$I = "M"                                                                        ;左L 中M 右R 对齐

MsgBox(0, "处理结果: " & _BinaryLen(_AddSpace($S, $C, $I)), Chr(34) & _AddSpace($S, $C, $I) & Chr(34))

Func _AddSpace($String, $Length, $LMR)
        $SLBL = _BinaryLen($String)                                ;中文字占用长度
        $SLCN = StringLen($String)                                ;中文字计算长度
;        $CN = $SLBL - $SLCN                                         ;中文字数
        Switch $LMR
                Case "L"                                                        ;左对齐
                        $RS = $Length - $SLBL                        ;右侧空格数
                        $LS = 0
                        Return StringFormat('%-' & $SLCN + $RS & 's', $String)
                Case "M"                                                        ;中对齐
                        $RS = Int(($Length - $SLBL)/2)        ;右侧空格数
                        $LS = $Length - $SLBL - $RS                ;左侧空格数
                        $L = StringFormat('%-' & $SLCN + $RS & 's', $String)
                        $R = StringFormat('%' & $SLCN + $LS + $RS & 's', $L)
                        Return $R
                Case "R"                                                        ;右对齐
                        $LS = $Length - $SLBL                        ;左侧空格数
                        $RS = 0
                        Return StringFormat('%' & $SLCN + $LS & 's', $String)
        EndSwitch
EndFunc

Func _BinaryLen($Expression)
        $Len = StringLen(StringReplace(StringToBinary($Expression, 1), "0x", ""))/2
        Return $Len
EndFunc
 楼主| 发表于 2020-6-21 20:45:55 | 显示全部楼层
afan 发表于 2020-6-17 20:48
你是为了等宽字体占位的显示效果?

粗浅的弄了7#代码,a 大可以的话再帮忙精简优化一下,谢谢!
发表于 2020-6-21 20:58:35 | 显示全部楼层
chishingchan 发表于 2020-6-21 20:45
粗浅的弄了7#代码,a 大可以的话再帮忙精简优化一下,谢谢!

你这还是我之前的方法,只不过加了字节长度判断。我还以为不是你需要的,挂了几天没动静,删了。
 楼主| 发表于 2020-6-21 21:09:32 | 显示全部楼层
afan 发表于 2020-6-21 20:58
你这还是我之前的方法,只不过加了字节长度判断。我还以为不是你需要的,挂了几天没动静,删了。

中文字符串与英文字符串还是有点“不对劲”,请帮忙修正。谢谢!
 楼主| 发表于 2020-6-22 11:26:09 | 显示全部楼层
afan 发表于 2020-6-17 20:48
你是为了等宽字体占位的显示效果?

1# 代码虽然冗长点,但是结果没出错。
发表于 2020-6-22 12:33:27 | 显示全部楼层
MsgBox(0, '',   "'" & _StringAddSpace('字符串Abc', 12, 'L') & "'" & @LF & _
                                "'" & _StringAddSpace('字符串Abc', 12) & "'" & @LF & _
                                "'" & _StringAddSpace('字符串Abc', 12, 'R') & "'")
Func _StringAddSpace($sString, $iLength, $sMode = '') ; 根据指定字节长度及对齐方式在字符串首尾添加空格
;~         ###(字符串, 字节长度, 对齐模式)        ;L左对齐 ;R右对齐 ;其它居中
        $sString = StringStripWS($sString, 3)
        Local $iBinaryLen = BinaryLen(StringToBinary($sString))
        Local $iSTRLen = StringLen($sString)
        If $iBinaryLen >= $iLength Then Return $sString
        Local $nAdd = $iLength - $iBinaryLen
        If $sMode = 'L' Then Return StringFormat('%-' & $nAdd + $iSTRLen & 's', $sString)
        If $sMode = 'R' Then Return StringFormat('%' & $nAdd + $iSTRLen & 's', $sString)
        Return StringFormat('%-' & $nAdd + $iSTRLen & 's', StringFormat('%' & Int($nAdd / 2) + $iSTRLen & 's', $sString))
EndFunc   ;==>_StringAddSpace

评分

参与人数 1威望 +10 金钱 +100 贡献 +10 收起 理由
chishingchan + 10 + 100 + 10 很给力!

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-15 16:03 , Processed in 0.104668 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表