user1926138
user1926138

Reputation: 1514

How to compare 1st character of a string to a specific character in VBScript?

I have a string. I need to compare 1st character of the string to a specific character. Below is the C# code.

String URL = "www.vulnuryrweb.com";

bool isValid = URL[0] == '/'
               && URL[1] != '/' 
               && URL[1] != '\\';

What will be VB-Script equivalent of above code?

Upvotes: 4

Views: 4956

Answers (3)

Slai
Slai

Reputation: 22876

URL = "www.vulnuryrweb.com"

char1 = Left(URL, 1)
char2 = Mid(URL, 2, 1)

isValid = ( char1 = "/" And char2 <> "/" And char2 <> "\" )

MsgBox isValid 

Update: It can be simplified with the Like operator :

URL = "www.vulnuryrweb.com"

isValid = URL Like "/[/\]*"

Debug.Print isValid

[/\] checks if the second character is / or \, and * matches 0 or more characters.

Upvotes: 1

Matt Wilko
Matt Wilko

Reputation: 27322

use the Mid function to get the first n chars of a string, then compare this to the results you want to check for

Dim isValid
isValid = (Mid(URL, 1, 1) = "/" And Mid(URL, 2, 1) <> "/" And Mid(URL, 2, 1) <> "\")

Note that Mid uses a 1 based index for characters (index 1 is the first char) whereas C# uses 0 based (url[0] is the first char)

Upvotes: 0

Perry
Perry

Reputation: 2282

According to http://www.w3schools.com/asp/func_mid.asp you will need to do

URL = "www.vulnuryrweb.com";
firstLetter = Mid(URL,1,1)

which will return w in this case

Upvotes: 0

Related Questions