Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I compare string with spaces

Status
Not open for further replies.

sxie

Technical User
Sep 10, 2003
8
US
Hi, all,

I am very new to scripting. Need your help on handling string comparison. I am trying to check whether a pc model matches, eg., "OpliPlex GX260". However, it seems that it is having difficulty with the space between "OpliPlex" and "GX260".

Here are somelines from my script:

REM Getting the value of pc model
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
pcmodel = objItem.Model
Next

rem If pc model is "gx240" then check marker file. Then either exit or continue to implement the security template import.
IF pcmodel = "OpliPlex GX240" then
execute certain command
End If

If I echo the objItem.Model when executing the script on a Dell OpliPlex GX240, the displays the model as "OpliPlex GX240". I have also tried:
pcmodel = Rtrim(objItem.Model) hoping to eleminate the spaces before and after. But still the IF condition does not satisfy even when I run this script on a GX240 machine. Could you please help?
 
Hi, all,
I answered my own question. What I had to do the following for the IF statement:

If Right(pcmodel,5) = "GX240" Then

I am ready the last 5 characters of the pcmodel avoiding the space issue.
 
Hello sxie,

Compare string with space is not the issue, never heard of it. Maybe it is the case, compare with UCase() or LCase() strings.

regards - tsuji
 
maybe there are multiple spaces in between the two words, but your 'output' displays them as one (a web browser would do this).

Try echoing REPLACE(pcmodel, " ", ".") which would change all spaces to full stops.

Or, you could strip all spaces inside the string, and then compare:
Code:
IF REPLACE(pcmodel, " ", "") = "OpliPlexGX240" then

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Maybe the word is "OptiPlex" as in Dell's Optiplex series of desktop products?
 
Thank you all so much for the tips! Although I was able to use Rtrim and Right(pcmodel,5) to resolve the issue, I will definitely try the LCase() string and the REPLACE function as alternatives.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top