UiBot汉语数字转为阿拉伯数字

/*
作用:该程序用于汉语数字转为阿拉伯数字,目前做到“零到九百九十九”的转化
*/

Dim arrayData = ""
Dim bLeftSide = ""
Dim bRightSide = ""
Dim index = ""
Dim number = ""
Dim prefixStr = ""
Dim suffixStr = ""
Dim count = ""

Function DigitalConversion(str)
	
	// 该数组元素的索引位置即为所对应的阿拉伯数字
	// 数组元素代表汉字数字
	arrayData = ["零","一","二","三","四","五","六","七","八","九"]
	// 循环字符串个数的遍数
	For i=1 To Len(str)
		// 遍历数组用每个数组元素来查找一遍字符串
		For j=0 To Len(arrayData) - 1
			// 查找数组所在位置,此处index为数组元素所在字符串的位置索引
			index = InStr(str,arrayData[j],1,False)
			If index > 0
				// TracePrint(SubStr(str,index,1))
				prefixStr = SubString(str,1,index)  // 裁出索引位置左侧字符串
				// TracePrint(prefixStr)
				suffixStr = SubString(str,index + 1,Len(str)+1) // 裁出索引位置右侧字符串,注:SubString函数的第三个形参,为小于结束位置不等于
				// TracePrint(suffixStr)
				str = prefixStr&j&suffixStr // 拼接字符串,j为汉字数字所对应的阿拉伯数字
			End If
		Next
	Next
	
	
	// 判断包含“百”的情况
	// 查找“百在字符串中的个数”
	number = FindCharNumbers(str,"百")
	For count=0 To number
		index = InStr(str,"百",1,False) // 此处index为“百”的位置
		If index > 0    // 当index为0时表示未找到
			// 判断左侧1个字符是否为数字
			bLeftSide = IsNumeric(SubStr(str,index - 1,1))
			// TracePrint(bLeftSide)
			// 判断右侧1个字符是否为数字
			bRightSide = IsNumeric(SubStr(str,index + 1,1))
			// TracePrint(bRightSide)
			
			If bLeftSide = True And bRightSide = True
				str = StrCut(str,index,1)   // 由于左右侧都有数字则可以直接删去汉字"百"即可
			End If
			
			If bLeftSide = True And bRightSide = False
				prefixStr = SubString(str,1,index)
				// TracePrint(prefixStr)
				suffixStr = SubString(str,index + 1,Len(str)+1)
				// TracePrint(suffixStr)
				str = prefixStr&"00"&suffixStr   // 在“百”和“十”位置上写00
			End If
		End If
	Next
	
	
	// 判断包含“十”的情况
	// 查找“十在字符串中的个数”
	number = FindCharNumbers(str,"十")
	For count=0 To number
		index = InStr(str,"十",1,False) // 此处index为“十”的位置
		If index > 0    // 当index为0时表示未找到
			// 判断左侧1个字符是否为数字
			bLeftSide = IsNumeric(SubStr(str,index - 1,1))
			// TracePrint(bLeftSide)
			// 判断右侧1个字符是否为数字
			bRightSide = IsNumeric(SubStr(str,index + 1,1))
			// TracePrint(bRightSide)
			
			If bLeftSide = True And bRightSide = True
				str = StrCut(str,index,1)   // 由于左右侧都有数字则可以直接删去汉字"十"即可
			End If
			
			If bLeftSide = False And bRightSide = True
				prefixStr = SubString(str,1,index)
				// TracePrint(prefixStr)
				suffixStr = SubString(str,index + 1,Len(str)+1)
				// TracePrint(suffixStr)
				str = prefixStr&"1"&suffixStr   // 在“十”位置上写1代表十位数
			End If
			
			If bLeftSide = True And bRightSide = False
				prefixStr = SubString(str,1,index)
				// TracePrint(prefixStr)
				suffixStr = SubString(str,index + 1,Len(str)+1)
				// TracePrint(suffixStr)
				str = prefixStr&"0"&suffixStr   // 在“十”位置上写0代表个位数
			End If
			
			If bLeftSide = False And bRightSide = False
				prefixStr = SubString(str,1,index)
				// TracePrint(prefixStr)
				suffixStr = SubString(str,index + 1,Len(str)+1)
				// TracePrint(suffixStr)
				str = prefixStr&"10"&suffixStr   // 在“十”位置上写10代表十位数和个位数
			End If
		End If
	Next
	
	Return str
	
End Function

// 查找字符串中指定字符个数,区分中英文标点,区分大小写
Function FindCharNumbers(str,char)
	count = 0
	index = InStr(str,char,1,False)
	Do While index <> 0
		index = InStr(str,char,index+1,False)   // i + 1 是为了从当前顿号的下一个位置查找,若从当前位置 i 开始查找则会一直找到的是这一个 i 
		count = count + 1
	Loop
	Return count
End Function

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值