Search a string in VBScript to verify if contains a character

冷暖自知 提交于 2020-04-30 05:12:25

问题


I am trying to see if a string contains a dot.

Set Root_Currency = Root_TaxDataSummary.SlvObject("Currency")   
curr_val = InStr(Root_Currency,".")
If curr_val.exist Then

     pass
else
     fail

Is there anything wrong with the way I am going about this?


回答1:


InStr returns an integer representing the position the searched text can be found in the string.

curr_val.exist won't work because the integer type doesn't have an exist method. Instead:

If curr_val > 0 Then

Or (if this is the only use of that variable):

If InStr(Root_Currency,".") > 0 Then

Lastly, because 0 is treated as False in VBScript, you don't need to include the equality. Either a position is found for the character or you get back a 0/false:

If InStr(Root_Currency,".") Then



回答2:


InStr returns a 'simple' number (1 based index/position of needle in haystack, or 0 meaning 'not found', or Null meaning Null argument) not an object. So change your code to:

If curr_val Then
   ' found
Else
   ' not found
End If


来源:https://stackoverflow.com/questions/37168882/search-a-string-in-vbscript-to-verify-if-contains-a-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!