Envin
Envin

Reputation: 1523

How can I remove the right side of this string?

In VBScript I have a String "Microsoft Windows 7 Professional |C:\Windows|\Device\Harddisk0\Partition1"

I just want the "Microsoft Windows 7 Professional" when this gets generated. Is there a way I can use the "|" as a delimiter and remove everything (include it) out of the string?

EDIT:

I tried using Left, here is an example of my code

For Each objOperatingSystem in colItems
   strOSName = Left(objOperatingSystem.name,InStr("|"))
Next

I get an error,

Wrong number of arguments or invalid property assignment: 'InStr'

Upvotes: 2

Views: 8598

Answers (2)

Shivam Gupta
Shivam Gupta

Reputation: 427

You may also use Split function in vbscript to split a string to a special character. Split function breaks a string into an array. Use the following:

Split("Microsoft Windows 7 Professional |C:\Windows|\Device\Harddisk0\Partition1", "|", "-1", "-1")(0)

If you find it little awkward you may use:

dim arr

arr = Split("Microsoft Windows 7 Professional |C:\Windows|\Device\Harddisk0\Partition1", "|", "-1", "-1")

myStr = arr(0)

It will work for sure.

Upvotes: 0

Daniel
Daniel

Reputation: 13122

Correct syntax is:

strOSName = TRIM(Left(objOperatingSystem.name,InStr(objOperatingSystem.name,"|")-1))

For safety you may want to try

if InStr(objOperatingSystem.name,"|") then
    strOSName = TRIM(Left(objOperatingSystem.name,InStr(objOperatingSystem.name,"|")-1))
end if

Just because, here is another method:

dim yourString
dim anArray
dim strOSName 
yourString = "Microsoft Windows 7 Professional |C:\Windows|\Device\Harddisk0\Partition1"
anArray = Split(yourString,"|")
strOSName = TRIM(anArray(0))

Upvotes: 4

Related Questions