Classic ASP InStr() Evaluates True on Empty Comparison String - vbscript

I ran into an issue with the Classic ASP VbScript InStr() function. As shown below, the second call to InStr() returns 1 when searching for an empty string in a non empty string. I'm curious why this is happening.
' InStr Test
Dim someText : someText = "So say we all"
Dim emptyString : emptyString = ""
'' I expect this to be true
If inStr(1,someText,"so",1) > 0 Then
Response.write ( "I found ""so""<br />" )
End If
'' I expect this to be false
If inStr(1, someText, emptyString, 1) > 0 Then
Response.Write( "I found an empty string<br />" )
End If
EDIT:
Some additional clarification: The reason for the question came up when debugging legacy code and running into a situation like this:
Function Go(value)
If InStr(1, "Option1|Option2|Option3", value, 1) > 0 Then
' Do some stuff
End If
End Function
In some cases function Go() can get called with an empty string. The original developer's intent was not to check whether value was empty, but rather, whether or not value was equal to one of the piped delimited values (Option1,Option2, etc.).
Thinking about this further it makes sense that every string is created from an empty string, and I can understand why a programming language would assume a string with all characters removed still contains the empty string.
What doesn't make sense to me is why programming languages are implementing this. Consider these 2 statements:
InStr("so say we all", "s") '' evaluates to 1
InStr("so say we all", "") '' evaluates to 1
The InStr() function will return the position of the first occurrence of one string within another. In both of the above cases, the result is 1. However, position 1 always contains the character "s", not an empty string. Furthermore, using another string function like Len() or LenB() on an empty string alone will result in 0, indicating a character length of 0.
It seems that there is some inconsistency here. The empty string contained in all strings is not actually a character, but the InStr() function is treating it as one when other string functions are not. I find this to be un-intuitive and un-necessary.

The Empty String is the Identity Element for Strings:
The identity element I (also denoted E, e, or 1) of a group or related
mathematical structure S is the unique element such that Ia=aI=a for
every element a in S. The symbol "E" derives from the German word for
unity, "Einheit." An identity element is also called a unit element.
If you add 0 to a number n the result is n; if you add/concatenate "" to a string s the result is s:
>> WScript.Echo CStr(1 = 1 + 0)
>> WScript.Echo CStr("a" = "a" & "")
>>
True
True
So every String and SubString contains at least one "":
>> s = "abc"
>> For p = 1 To Len(s)
>> WScript.Echo InStr(p, s, "")
>> Next
>>
1
2
3
and Instr() reports that faithfully. The docs even state:
InStr([start, ]string1, string2[, compare])
...
The InStr function returns the following values:
...
string2 is zero-length start
WRT your
However, position 1 always contains the character "s", not an empty
string.
==>
Position 1 always contains the character "s", and therefore an empty
string too.

I'm puzzled why you think this behavior is incorrect. To the extent that asking Does 'abc' contain ''? even makes sense, the answer has to be "yes": All strings contain the empty string as a trivial case. So the answer to your "why is this happening" question is because it's the only sane thing to do.

It is s correct imho. At least it is what I expect that empty string is part of any other string. But maybe this is a philosophical question. ASP does it so, so live with it. Practically speaking, if you need a different behavior write your own Method, InStrNotEmpty or something, which returns false on empty search string.

Related

Search a string in VBScript to verify if contains a character

I am trying to see if a string contains a dot.
Set Root_Currency = Root_TaxDataSummary.SlvObject("Currency")
curr_val = InStr(Root_Currency,".")
If curr_val.exist Then
pass
else
fail
Is there anything wrong with the way I am going about this?
InStr returns an integer representing the position the searched text can be found in the string.
curr_val.exist won't work because the integer type doesn't have an exist method. Instead:
If curr_val > 0 Then
Or (if this is the only use of that variable):
If InStr(Root_Currency,".") > 0 Then
Lastly, because 0 is treated as False in VBScript, you don't need to include the equality. Either a position is found for the character or you get back a 0/false:
If InStr(Root_Currency,".") Then
InStr returns a 'simple' number (1 based index/position of needle in haystack, or 0 meaning 'not found', or Null meaning Null argument) not an object. So change your code to:
If curr_val Then
' found
Else
' not found
End If

reading between two values

I have to read between 2 values after asking the users if he want between the '' or not between.
Exemple if the user select 1 in the text
'Hi' My name is 'Kev'in and i'm happ'y' to be 'there'
he will have
'Hi' 'Kev' 'y' 'there'
in a text file. If he chose 2, he will have
My name is in and i'm happ to be
Right now I'm using
Do While objScriptFile.AtEndOfStream <> True
strCurrentLine = objScriptFile.ReadLine
intIsComment = InStr(1,strCurrentLine,"'")
If intIsComment > 0 Then
objCommentFile.Write strCurrentLine & vbCrLf
End If
Loop
Else
For now it's only reading both of the value (between '' and not between) but I have no idea how to change it.
That's pretty simple, provided the delimiter is unique. Split the line at ' and output either the even or the odd array elements, depending on whether 1 or 2 was chosen.
...
strCurrentLine = "'Hi' My name is 'Kev'in and i`m happ'y' to be 'there'"
arr = Split(strCurrentLine, "'")
For i = choice To UBound(arr) Step 2
objCommentFile.Write arr(i)
Next
...
The value of choice is your users' selection (either 1 or 2).
Note that for this to work the strings must not contain apostrophes anywhere else. As #Ekkehard.Horner pointed out in his comment you can't use the delimiter character elsewhere in the text (i'm), because otherwise it would be impossible to distinguish where it was intended to be a delimiter and where not.

how to check whether the string taken through gui is a binary string in matlab?

I am working on a watermarking project that embeds binary values (i.e 1s and 0s) in the image, for which I have to take the input from the user, and check certain conditions such as
1) no empty string
2) no other character or special character
3) no number other than 0 and 1
is entered.
The following code just checks the first condition. Is there any default function in Matlab to check whether entered string is binary
int_state = get(handles.edit1,'String'); %edit1 is the Tag of edit box
if isempty(int_state)`
fprintf('Error: Enter Text first\n');
else
%computation code
end
There is no such standard function, but the check can be easily implemented.
Use this error condition:
isempty(int_state) || any(~ismember(int_state, '01'))
It returns false (no error) if the string is non-empty and composed of '0's and '1's only.
The function ismember returns a boolean array that indicates for every character in int_state whether it is contained in the second argument, '01'. The advantage is that this can be generalized to arbitrary sets of allowed characters.
I think the 2nd and 3rd can be combined together as 1 condition: your input string can only be a combination of 0 and 1? If it is so, then a small trick with findstr can do that:
if length(findstr(input_str, '1')) + length(findstr(input_str, '0')) == length(input_str)
condition_satisfied;
end
tf = isnumeric(A) returns true if A is a numeric array and false otherwise.
A numeric array is any of the numeric types and any subclasses of those types.
isnumeric(A)
ans =
1 (when A is numeric).

Exact details of how Empty works in VBScript

I'm new to VBScript and rather perplexed as to why the following code works:
set Adapters = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
for each Nic in Adapters
if Nic.IPEnabled then
MsgBox "IP Address: " & Nic.IPAddress(asdf), 4160, "IP Address"
end if
next
When the variable asdf is undefined, it works. If I define asdf to an invalid index (e.g. -1 or 4), I get an invalid index error.
I thought: Perhaps undefined variables default to 0 in VBS? Nope, I tried to print it and nothing is written.
Where is the functionality of passing an undefined variable as an array index returning the first item in the array documented? What are some other, similar oddities I may run into while programming in VBScript?
Edit: Some things I've discovered:
Undefined variables are equal to Empty by default in VBScript
Empty = 0 is true
Nic.IPAddress(Empty) also returns the first element of the array
MsgBox 0 will print 0, while MsgBox Empty will print nothing
I'm still having trouble finding any documentation stating that array indexing handles Empty quietly by returning the first element, explaining why it is equivalent to but printed differently from 0, and what other constructs handle Empty parameters (and what they do as a result).
The undefined variable is Empty and returning the Empty index of an array will also return the 0 index.
In the IPAddress array, there are two indexes, a 0 and a 1.
So this will have the same effect as your code:
MsgBox "IP Address: " & Nic.IPAddress(0), 4160, "IP Address"
And
MsgBox "IP Address: " & Nic.IPAddress(Empty), 4160, "IP Address"
And this will give you the IPv6 addresses:
MsgBox "IP Address: " & Nic.IPAddress(1), 4160, "IP Address"
Also, this will return both indexes of the array:
WScript.Echo Join(Nic.IPAddress,",")
Also, consider this example,
arr = Array("first","second","third")
WScript.Echo arr(Empty)
The output here will be first
See this post for VBScript Data Types:
"Empty : Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.`
http://msdn.microsoft.com/en-us/library/9e7a57cf(v=vs.84).aspx
Big thanks to Langstrom for providing this:
"Empty: Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables."
Empty:
Nic.IPAddress(asdf) treats the parameter as an int, and CInt(Empty) is equal to 0, the index of the first element of the array.
MsgBox asdf treats the parameter as a string, and CStr(Empty) returns an empty string.
Any function that expects an integer and for which 0 is a valid value will work exactly the same as if 0 had been passed when called with an Empty parameter.
When comparing Empty = 0, Empty is treated as an integer because it being compared to an integer, so the expression is true.
When comparing Empty = "0", Empty is treated as a string, and so the expression is false. Therefore, Empty = "" is true.
Null:
Also, it is worth mentioning that Null is not equal to anything, including itself. Null = Null is false.

Checking if a string has balanced parentheses

I am currently working on a Ruby Problem quiz but I'm not sure if my solution is right. After running the check, it shows that the compilation was successful but i'm just worried it is not the right answer.
The problem:
A string S consisting only of characters '(' and ')' is called properly nested if:
S is empty,
S has the form "(U)" where
U is a properly nested string,
S has
the form "VW" where V and W are
properly nested strings.
For example, "(()(())())" is properly nested and "())" isn't.
Write a function
def nesting(s)
that given a string S returns 1 if S
is properly nested and 0 otherwise.
Assume that the length of S does not
exceed 1,000,000. Assume that S
consists only of characters '(' and
')'.
For example, given S = "(()(())())"
the function should return 1 and given
S = "())" the function should return
0, as explained above.
Solution:
def nesting ( s )
# write your code here
if s == '(()(())())' && s.length <= 1000000
return 1
elsif s == ' ' && s.length <= 1000000
return 1
elsif
s == '())'
return 0
end
end
Here are descriptions of two algorithms that should accomplish the goal. I'll leave it as an exercise to the reader to turn them into code (unless you explicitly ask for a code solution):
Start with a variable set to 0 and loop through each character in the string: when you see a '(', add one to the variable; when you see a ')', subtract one from the variable. If the variable ever goes negative, you have seen too many ')' and can return 0 immediately. If you finish looping through the characters and the variable is not exactly 0, then you had too many '(' and should return 0.
Remove every occurrence of '()' in the string (replace with ''). Keep doing this until you find that nothing has been replaced (check the return value of gsub!). If the string is empty, the parentheses were matched. If the string is not empty, it was mismatched.
You're not supposed to just enumerate the given examples. You're supposed to solve the problem generally. You're also not supposed to check that the length is below 1000000, you're allowed to assume that.
The most straight forward solution to this problem is to iterate through the string and keep track of how many parentheses are open right now. If you ever see a closing parenthesis when no parentheses are currently open, the string is not well-balanced. If any parentheses are still open when you reach the end, the string is not well-balanced. Otherwise it is.
Alternatively you could also turn the specification directly into a regex pattern using the recursive regex feature of ruby 1.9 if you were so inclined.
My algorithm would use stacks for this purpose. Stacks are meant for solving such problems
Algorithm
Define a hash which holds the list of balanced brackets for
instance {"(" => ")", "{" => "}", and so on...}
Declare a stack (in our case, array) i.e. brackets = []
Loop through the string using each_char and compare each character with keys of the hash and push it to the brackets
Within the same loop compare it with the values of the hash and pop the character from brackets
In the end, if the brackets stack is empty, the brackets are balanced.
def brackets_balanced?(string)
return false if string.length < 2
brackets_hash = {"(" => ")", "{" => "}", "[" => "]"}
brackets = []
string.each_char do |x|
brackets.push(x) if brackets_hash.keys.include?(x)
brackets.pop if brackets_hash.values.include?(x)
end
return brackets.empty?
end
You can solve this problem theoretically. By using a grammar like this:
S ← LSR | LR
L ← (
R ← )
The grammar should be easily solvable by recursive algorithm.
That would be the most elegant solution. Otherwise as already mentioned here count the open parentheses.
Here's a neat way to do it using inject:
class String
def valid_parentheses?
valid = true
self.gsub(/[^\(\)]/, '').split('').inject(0) do |counter, parenthesis|
counter += (parenthesis == '(' ? 1 : -1)
valid = false if counter < 0
counter
end.zero? && valid
end
end
> "(a+b)".valid_parentheses? # => true
> "(a+b)(".valid_parentheses? # => false
> "(a+b))".valid_parentheses? # => false
> "(a+b))(".valid_parentheses? # => false
You're right to be worried; I think you've got the very wrong end of the stick, and you're solving the problem too literally (the info that the string doesn't exceed 1,000,000 characters is just to stop people worrying about how slow their code would run if the length was 100times that, and the examples are just that - examples - not the definitive list of strings you can expect to receive)
I'm not going to do your homework for you (by writing the code), but will give you a pointer to a solution that occurs to me:
The string is correctly nested if every left bracket has a right-bracket to the right of it, or a correctly nested set of brackets between them. So how about a recursive function, or a loop, that removes the string matches "()". When you run out of matches, what are you left with? Nothing? That was a properly nested string then. Something else (like ')' or ')(', etc) would mean it was not correctly nested in the first place.
Define method:
def check_nesting str
pattern = /\(\)/
while str =~ pattern do
str = str.gsub pattern, ''
end
str.length == 0
end
And test it:
>ruby nest.rb (()(())())
true
>ruby nest.rb (()
false
>ruby nest.rb ((((()))))
true
>ruby nest.rb (()
false
>ruby nest.rb (()(((())))())
true
>ruby nest.rb (()(((())))()
false
Your solution only returns the correct answer for the strings "(()(())())" and "())". You surely need a solution that works for any string!
As a start, how about counting the number of occurrences of ( and ), and seeing if they are equal?

Resources