User Tools

Site Tools


k8:k8.3:start

8.3 String operators

For operations with strings, Gambas provides you with only 2 operators. For the comparisons of two strings, you will mainly use the comparison operators in table 8.3.2.1.

8.3.1 Operators

Gambas knows the following operators for operands of type string:

OperatorDescription
String & String Connects two strings to one character string
String &/ String Connects two strings containing file names. A path separator is inserted between the two strings if this is required

Table 8.3.1.1: String operators

Message.Error("Fehler: " & Chr(10) & "The character is not in the input alphabet!")
If NOT Exist(User.Home &/ "V24T" &/ "v24T.conf") Then …
Path = SettingsP.DefaultDir &/ Application.Name & ".conf"

Note that in the third example, the last & links two strings to a complete filename (file name and extension such as udpserver.conf).

8.3.2 Comparison of character strings

Of the comparison operators in the following table, the last three are of particular interest because they can be used effectively in many cases by specifying patterns.

Comparison operatorDescription
String = String The comparison is true if two exact string are equal
String == String
String LIKE PatternStringThe system checks whether a string matches a pattern
String BEGINS PatternString The system checks whether a string begins with the pattern
String ENDS PatternString The system checks whether a string ends with the pattern

Table 8.3.2.1: Comparison operators 1 for strings

[1] Dim sCharacterstring As String
[2]
[3] sCharacterstring = "Gambas"
[4]
[5] Message.Info(IIF(sCharacterstring = "gambas", "Comparison true.", "Comparison false."))
[6] Message.Info(IIF(sCharacterstring == "gaMBas", "Comparison true.", "Comparison false."))

Comments:

  • The equals sign in line 1 is an assignment operator.
  • In the first comparison IF sCharacterstring = “gambas” in the 5th line, the equal sign is used as a comparison operator. The comparison provides a false statement.
  • In the 6th line, the comparison IF sCharacterstring == “gaMBas” delivers a true statement, because the comparison does NOT take capital and small letters into account.
Comparison operatorDescription
String1 < String2 The comparison is true if string1 is smaller than string2
String1 > String2 The comparison is true if string1 is larger than string2
String1 <= String2 The comparison is true if string1 is less than or equal to string2
String1 >= String2 The comparison is true if string1 is greater than or equal to string2

Table 8.3.2.2: Comparison operators for strings

For example, the comparison operators ⇐ and >= are available in the class app/src/gambas3/.src/Component/CSymbolInfo.class and are used there to determine whether a single character stored in the string sCar belongs to a certain interval A to Z in the ASCII character set.

If (sCar >= "A" AND sCar <= "Z") OR sCar = "." Then ...

For example, if you want to sort character strings with your own procedures, you can use the above-mentioned comparison operators for character strings, as you can see from the following source code:

Public Sub Form_Open()
  FMain.Center
  FMain.Resizable = False
End ' Form_Open()
 
Public Sub btnSort_Click()
  Dim aStringArray As String[]
 
  aStringArray = Split(txaText.Text, "\n")
    SwapSortStrings(aStringArray)
  txaText.Text = aStringArray.Join("\n")
 
End ' btnSort_Click()
 
Private Sub SwapSortStrings(aMatrix As String[])
' Implementation SwapSort algorithm: http://de.wikipedia.org/wiki/Swap-Sort
  Dim i, j, m As Integer
  Dim sTemp As String
 
  i = 0
  While i < aMatrix.Count
    m = 0
    For j = 0 To aMatrix.Count - 1
      If aMatrix[j] < aMatrix[i] Then Inc m ' Comparison of strings
    Next ' j
    If i = m Then
       Inc i
    Else
       sTemp = aMatrix[m]
       aMatrix[m] = aMatrix[i]
       aMatrix[i] = sTemp
    Endif ' i = m?
  Wend
End

Originalliste Sortierte String-Liste
Figures 8.3.2.1 and 8.3.2.2: Sorting strings

A disadvantage of SwapSort: Each item in the list to be sorted may only appear once. The method String[].Sort() does not have this disadvantage and uses the QuickSort algorithm.

8.3.3.3 LIKE operator

bResult = String [NOT] LIKE Pattern

The boolean variable bResult gets the value TRUE if the pattern string matches string. If NOT is used, the test is inverted.

The operator is not case-sensitive and the following patterns can be used:

PatternInterpretation
*A number of arbitrary characters
?A single character
[abc]A character from the set in square brackets
[x-y]A character from the interval into square bracket
[^x-y]A character not from the interval into square bracket
spaceAny number of characters with an ASCII code smaller than 32
{aaa, bbb,…. }One of the character strings from the { set } separated by commas.
\x Special characters x have special meaning for the LIKE operator and \x is used to represent such special characters.

Table 8.3.3.3.1: Sample

exampletruthfulness valuedescription
“Gambas” LIKE “G*”TRUEThe gambas string begins with G, which can be followed by any number of characters.
“Gambas” LIKE “G[^Aa]*”FALSEGambas begins with G, which no A or a may follow, leading to the error. The * is ignored.
“Gambas” LIKE “?[Aa]*”TRUEAfter any character that must be followed by an a or A, there are any number of other characters.
sDevice NOT LIKE “dev/ttyUS{B0, B1}“TRUEAssumption: The variable sDevice has the value “dev/ttyUSB2”. No element from the pattern set fits.

Table 8.3.3.3.2: Comparison operators 1 for strings

Please note: LIKE is only valid for ASCII strings. If you are testing against UTF-8 strings, you must use the gb.pcre component.

8.3.4 BEGINS operator

bResult = String [NOT] BEGINS Pattern

The boolean variable bResult gets the value TRUE when string begins with the pattern string. If NOT is used, the test is inverted. The BEGINS operator is case-sensitive.

exampletruthfulness valuedescription
“Gambas” BEGINS “Gam”TRUEThe string Gambas begins with the string Gam.
“Gambas” BEGINS “Ham”FALSEGambas does not begin with the character string Ham.
“Hans” NOT BEGINS “Ga”TRUETrue; Hans does NOT start with the string Ga.

Table 8.3.4.1: Examples for the BEGINS operator

8.3.5 ENDS operator

bResult = String [NOT] ENDS Pattern

The boolean variable bResult receives the value TRUE if string ends with the pattern string. If NOT is used, the test is inverted. The ENDS operator is case-sensitive.

exampletruthfulness valuedescription
“Gambas” ENDS “bas”TRUEThe string Gambas ends with “bas”.
“Gambas” ENDS “BAS”FALSEThe string Gambas does NOT end on “BAS”.
“Hans” NOT ENDS “ins” TRUEVote; Hans does NOT end up “ins”.

Table 8.3.5.1: Examples for the operator ENDS

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k8/k8.3/start.txt · Last modified: 09.02.2022 (external edit)

Page Tools