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.
Gambas knows the following operators for operands of type string:
Operator | Description |
---|---|
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).
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 operator | Description |
---|---|
String = String | The comparison is true if two exact string are equal |
String == String | |
String LIKE PatternString | The 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:
Comparison operator | Description |
---|---|
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
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.
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:
Pattern | Interpretation |
---|---|
* | 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 |
space | Any 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
example | truthfulness value | description |
---|---|---|
“Gambas” LIKE “G*” | TRUE | The gambas string begins with G, which can be followed by any number of characters. |
“Gambas” LIKE “G[^Aa]*” | FALSE | Gambas begins with G, which no A or a may follow, leading to the error. The * is ignored. |
“Gambas” LIKE “?[Aa]*” | TRUE | After 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}“ | TRUE | Assumption: 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.
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.
example | truthfulness value | description |
---|---|---|
“Gambas” BEGINS “Gam” | TRUE | The string Gambas begins with the string Gam. |
“Gambas” BEGINS “Ham” | FALSE | Gambas does not begin with the character string Ham. |
“Hans” NOT BEGINS “Ga” | TRUE | True; Hans does NOT start with the string Ga. |
Table 8.3.4.1: Examples for the BEGINS 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.
example | truthfulness value | description |
---|---|---|
“Gambas” ENDS “bas” | TRUE | The string Gambas ends with “bas”. |
“Gambas” ENDS “BAS” | FALSE | The string Gambas does NOT end on “BAS”. |
“Hans” NOT ENDS “ins” | TRUE | Vote; Hans does NOT end up “ins”. |
Table 8.3.5.1: Examples for the operator ENDS