Hello
is there a function which would enable me to determine if the contents of a variable is say "-"
Printable View
Hello
is there a function which would enable me to determine if the contents of a variable is say "-"
Yes, you can use Instr function to test for it and return the index position of it in the variable string.
VB Code:
Dim str As String Dim iRet As Integer str = "VB-Office Guru" iRet = Instr(1, str, "-") If iRet > 0 Then MsgBox "Contains a '-' at position: " & iRet ' = 3 Else MsgBox "does not contain a '-'" End If
Minor correction ... the opening quote is required ...
str = "VB-Office Guru"
"InStr" is nice in that it automatically casts a number to a string, so you can detect negative signs (-) in numbers as well as hyphens (-) in text strings !!!
What type-o? :D You didn't see nut'n. :lol:
Thanks.
i meant do a comparison of a single character to check if the character is '-' or not.
i guess i could do something like
VB Code:
str = '-' if str='-' then MsgBox "it is a -" else MsgBox "it is not a -"
You can certainly do that, but you need an "End If" at the end. You can still use the "Instr" for the a character match in any string if you want ... but it can't tell you if the string happens to be longer than 1 character.
If needs be you can also scan through a string character by character to determine what it is (loop around the length using MID), or if you are needing to replace a certain character with another then the REPLACE function will help.
thanks guys