Vbscript How to find the "-" in a line and put the characters that follow in a new line?

I am using vbscript, how do I find the hyphen "-" in a line and put the characters that follow in a new line?

Mostly my computer names end with a room number

PC1-3 (room 3), PC1-4 (room 4), etc.

I use

Dim roomArray(2) roomArray = Array("-1", "-2", "-3") Dim item For each item in roomArray If instr(strComputerName, item) ..do something End If Next 

but I get false positives because of room -33 containing "-3", etc., so if I find a "-" in the line and put the characters that follow in the line, I can check for the full line, and not to use instr.

Thanks for any help.

+4
source share
3 answers

This will get all the text behind the last hyphen found:

 Function GetRoomNumber(strComputerName) Dim hyphenIndex hyphenIndex = InStrRev(strComputerName, "-") If hyphenIndex > 0 Then GetRoomNumber = Mid(strComputerName, hyphenIndex+1) End If End Function 

Use in your example would be:

 Dim roomArray(2) roomArray = Array("-1", "-2", "-3") Dim item, index For index = LBound(roomArray) To UBound(roomArray) item = roomArray(index) If ("-" & GetRoomNumber(strComputerName)) = item Then ..do something End If Next 

Or the short version will be (without defining a function with data verification):

 ... If Mid(strComputerName, InStrRev(strComputerName, "-") ) = item Then ... 
+2
source

You need to check if strComputerName ends with dash-roomNumber.

 if Right(strComputerName,Len(item))=item Then... 
+1
source

You can use rights and instr to get this

 dim s s = "PC1-3 (room 3)" msgbox right(s, len(s) - instr(s,"-")) 
+1
source

All Articles