Hi,
Do you know the
Like-Operator in Visual Basic that can be used to compare two strings?
small VB-example:
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
|
Private Sub Command1_Click()
Dim s As String
Dim p As String
p = "[0-9]*[+-\*/]*[0-9]*=*[0-9]"
s = "1 + 1 = 2"
If s Like p Then MsgBox s 'OK
s = "a + b = c"
If s Like p Then MsgBox s 'no
End Sub
|
The syntax is:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
result = string Like pattern
The Like operator syntax has these parts:
Part | Description
========|=========================================
result | Required; any numeric variable.
string | Required; any string expression.
pattern | Required; any string expression conforming
| to the pattern-matching conventions
| described in Remarks.
Remarks
If string matches pattern, result is True;
if there is no match, result is False.
Characters | Matches
in pattern | in string
============|=====================================
? | Any single character.
* | Zero or more characters.
# | Any single digit (0–9).
[charlist] | Any single character in charlist.
[!charlist] | Any single character not in charlist.
|
the pattern is like a regular expression that can make the Like-operator very likely.
afair Jabaco does not have the Like-operator but of course it does have regular expressions, that can be found in the namespace:
java#util#regex#
maybe Jabaco will sometimes have the Like-Operator meanwhile you can note down a function and use it like the following:
|
Jabaco Source
|
1
2
3
4
5
|
Public Function Like(str As java#lang#String, pat As String) As Boolean
Dim pt As java#util#regex#Pattern = Pattern.compile(pat, CASE_INSENSITIVE)
Dim mt As java#util#regex#Matcher = pt.matcher(str)
Like = mt.find
End Function
|
even in one line:
|
Jabaco Source
|
1
2
3
|
Public Function Like(str As java#lang#String, pat As String) As Boolean
Like = java#util#regex#Pattern.compile(pat, CASE_INSENSITIVE).Matcher(str).find
End Function
|
suggestion: in Jabaco the Like-Operator could be compiled into this one line.
so what, what's the trick here?
OK, the Matcher-object needs a CharSequence-Object, that is the Base-Class of Java#lang#String, StringBuffer and StringBuilder but not of the Jabaco-String-Object.
It is possible to cast a Jabaco-String into a java#lang#String and it is possible to cast a java#lang#string into a Charsequence.
a small example for how to use it in Jabaco:
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Public Sub Command1_Click()
Dim whiteSpacesOrNot As String = "\s*"
Dim minOneWhitespace As String = "\s+"
Dim Condition As String = "((\w+)|(\w+\s+[=<>]\s+\w+))"
Dim p As String
p = whiteSpacesOrNot & "If" & minOneWhitespace & Condition & minOneWhitespace & "Then"
Dim s As String
s = "if true then": If Like(s, p) Then msgbox s 'ok
s = " If False Then ": If Like(s, p) Then msgbox s 'ok
s = "IF b = True THEN": If Like(s, p) Then msgbox s 'ok
s = "If 1 = 1 Then": If Like(s, p) Then msgbox s 'ok
s = "If fncnam(A, B) Then": If Like(s, p) Then msgbox s 'ok
s = "If Then": If Like(s, p) Then msgbox s 'nop
End Sub
|
As you can see it is much more powerful than the Like-operator in VB
happy coding
OlimilO