411 REFERENCE · Wikibooks
Programming Gambas from Zip/Functions
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Functions Reference
String Functions
Character CodesAscReturns the ASCII code of a characterEvery character has an ASCII code, eg Asc("A") is 65.ChrReturns a character from its ASCII codeChr(65) is "A" . Chr(32) is the space. Chr(1) is Ctrl-A
When checking to see if a key was pressed, use Key["A"] or Key[Key.ShiftKey] instead for the number of a key on the keyboard, rather than the number of a character.Parts of StringsLeftThe left-hand end of a stringLeft("Hello", 4) is "Hell"MidThe “middle” part of a string
Mid(String, StartingAt, HowLong)Mid("Gambas", 3, 2) is mb because that is the string that starts at position 3 and is 2 characters long.RightThe right-hand end of a stringRight("String", 4) is "ring"Find and ReplaceInStrThe position of the second string in the first
InStr(String, LookFor)
InStr(String, LookFor, StartingAt)InStr("Gambas is basic", "bas") is 4, because bas starts at position 4.
InStr("Gambas is basic", "bas", 6) is 11, because InStr only starts looking for bas from position 6.
If the string is not found it returns 0.
InStr("Gambas is basic", "bas", -5) is 11. The -5 means only start looking five from the end until the end, i.e. at position 10, the second space. Note that the last letter, c, is not 1 from the end. It is the end. i is 1 from the end.ReplaceThe string with every occurrence of something replaced by something else
Replace(String, LookFor, WithThis)Replace("Abracadabra", "a", "*") is Abr*c*d*br*
Message( Replace(LetterText, "<name>", FirstName&" "&LastName) )SubstA string with its markers replaced by other stringsSubst("Tell &1 to arrive at &2 PM", "Mark", "4") is
"Tell Mark to arrive at 4 PM"Change CaseUCaseConverts a string to uppercase.UCase("gerard") is "GERARD"LCaseThe string converted to lower caseLCase("GERARD") is "gerard"TrimmingTrimA string with no spaces at either endTrim(" Hello ") is "Hello"LTrimLeft Trim
The string with spaces removed from the startLTrim(" Hello") is "Hello"RTrimRight Trim
The string with spaces removed from the endRTrim("Hello ") is "Hello"Building StringsStringA string made of the same string repeated many times.
String(HowMany, WhichString)String(6, "*") is "******"
String(2, "Gambas") is "GambasGambas"SpaceA string of spacesSpace(8) is eight spacesQuoteThe string in quotation marks
The UnQuote function removes them.Quote("Hello,") & " she said." is "Hello," she said.MiscellaneousLenThe length of a stringLen("Gambas is basic") is 15CompCompares two strings
Comp(FirstString, SecondString)
Comp(FirstString, SecondString, Mode)If the two strings are the same, it returns 0.
If the first string comes after the second in a dictionary, it returns +1
If the first string comes before the second it is -1
If Comp(UserName, "Akiti", gb.IgnoreCase) = 0 Then Message("Welcome Akiti!") else Message("You’re not Akiti!")SplitAn array made from a string
Split(String, Separator)Dim z As String[] = Split("John,Mary,Paul,Gemma,Lucille,Hamish", ",")
The string is split at the commas.
Numeric Functions
Positives and NegativesAbs Absolute value of a number — remove the negative sign if any.Abs(7) = 7
Abs(-7) = 7Sgn Sign (+, 0, –) of a numberSign(3.14) = +1
Sign(–6) = –1
Sign(0) = 0To Whole NumbersInt Integer part of a number
Negative numbers are rounded down; positive numbers abandon the fraction part.Int(3.2) = 3
Int(3.9) = 3
Int(6) = 6
Int(-7.2) = -8
Int(-7.9) = -8Round Round a number to the nearest integer or power of ten
e.g. 10^2 is 100, so Round(x,2) is to the nearest 100.
Negative powers are how many decimal placesRound(Pi, -2) = 3.14
Print Round(1972, 2) = 2000Frac The fractional part of a number
The fractional part of a date is the time.Frac(3.14) = 0.14
Frac(Date(1999, 12, 13, 14, 20, 00)) = 0.597222222015
CDate(Frac(Date(1999, 12, 13, 14, 20, 00))) = 14:20:00Fix Integer part of a number—remove any fractional partFix(3.14) = 3
Fix(-3.14) = -3CIntConvert to an integer
Abandon the fraction part completely.Int(3.2) = 3
Int(3.9) = 3
Int(6) = 6
Int(-7.2) = -7
Int(-7.9) = -7Floor Round down to the next lowest whole numberFloor(3.14) = 3
Floor(-3.14) = -4Ceil Ceiling: round up to the next highest whole numberCeil(3.14) = 4
Ceil(-3.14) = 3ComparingMax Whichever of two numbers is greater
Also works with datesMax(6,4) = 6Min Whichever of two numbers is lesser
Also works with datesMin(6,4) = 4Increment and DecrementINC Increments a variable; same as x += 1 or x = x+1
This is an procedure rather than a function (no brackets; it’s a verb, not a noun)INC xDEC Decrement a variable; same as x –= 1 or x = x–1
This is an procedure rather than a function.DEC x
Character Test Functions
The empty string is NULL. IsNull("") is True. IsNull("M") is False.
IsAscii Tests if a string contains only ASCII characters.IsBlank Tests if a string contains only blank characters.IsDigit Tests if a string contains only digits.IsHexa Tests if a string contains only hexadecimal digits.IsLCase Tests if a string contains only lowercase letters.IsLetter Tests if a string contains only letters.IsPunct Tests if a string contains only printable non-alphanumeric characters.IsSpace Tests if a string contains only space characters.IsUCase Tests if a string contains only uppercase letters.
Random Numbers
RandomizeWithout it you get the same series of random numbers each time your program is run.Randomize
Just the word by itself.RandA random integer between two numbers.Rand(1,6) is a random number between 1 and 6 inclusively. This could be a dice throw.
Rand(1,2) is either a 1 or a 2.RndA random floating point number.Rnd( ) is between 0 and 1
Rnd(9) is between 0 and 9, but never 9 itself.
Rnd(10, 20) is between 10 and 20, but never 20.
Time And Date
A given date and time is stored internally as two integers, the first being the date and the second being the time. The following examples may explain what you can do with dates and times.
Examples:
Now07/08/2019 19:18:54.9298 July 2019, almost 8:19 pm
The current date and timeFormat(Now, "dddd dd/mm/yyyy hh:nn:ss")Tuesday 09/07/2019 20:45:13More on Format() later. Use it to present a date and/or time, or indeed any number, in the way you want. And it is now 8:46pm on July 9, by the way.Date()07/07/2019 23:00:00When today started, less one hour for Daylight Saving Time.Date(1955, 6, 1)05/31/1955 23:00:00Someone’s birthday, less one hour for Daylight Saving Time. The date is assembled from Year, Month, Day.Date(2019, 12, 25, 6, 15, 0)12/25/2019 05:15:006:15 am on Christmas Day, 2019. Time for opening Christmas presents.
Year, Month, Day, Hours, Minutes, SecondsDateAdd(Date(1955, 6, 1), gb.Day, 14)06/14/1955 23:00:0014 days after 1 June 1955. DS again.
You can add other things besides days:
gb.Second, gb.Minute, gb.Hour,
gb.Day, gb.Week, gb.WeekDay (ignore Saturday and Sunday),
gb.Month, gb.Quarter, gb.YearDateDiff(Date(2019, 7, 6), Date(Now), gb.Day)2Days between two days ago and nowDateDiff(Date(Now), Date(2018, 12, 25), gb.Day)195How many days since Christmas?
(It’s now 2019. Last Xmas was 2018.)
DateDiff(RecentDate, PastDate, Units)DateDiff(Date(Now), Date(2019, 12, 25), gb.Day)170How many days until Christmas?
(It’s now 2019.)DateDiff(Date(1955, 6, 1), Now, gb.Second)2022959392How many seconds I have been aliveDateDiff(Date(1955, 6, 1), Now, gb.Day)23413How many days I have been aliveCLong(DateDiff(Date(1955, 6, 1), Now, gb.Minute)) * 722427551928 … no, wait, it’s 2427552144 … no, wait, it’s 2427552216 ...How many heartbeats since my birth.
Had to convert the DateDiff to Long Integer because without it there is an overflow problem: it shows as a negative number. Longs can hold very big numbers.Parts of Dates and TimesHour(Now) & " hrs " & Minute(Now) & " mins"14 hrs 39 minsIt is now 2:39 pm.
You can also use these:
Day(), Hour(), Minute(), Month()
Second(), Time(), Week(), Weekday()
Year()"Rip van Winkle, this is " & Year(Now)Rip van Winkle, this is 2019The year part of a dateWeekDay(Now)20 = Sunday, 6 = Saturday
2 means it is Tuesday.If WeekDay(Now) = gb.Tuesday then label1.text = "It is Tuesday." else label1.text = "It is not Tuesday."It is Tuesday.gb.Tuesday is a constant whose value is 2. Using the constant means you need not remember Tuesday is 2.
Others are gb.Monday, gb.Wednesday, gb.Thursday, gb.Friday, gb.Saturday and gb.SundayTime(Now)14:58:44.654The time part of a date (Now is the current date and time)Time(14, 08, 25)14:08:25Assemble a time from its parts
Time(Hour, Minutes, Seconds)Time()15:05:09.515The time right nowConversionsVal("1/6/55")05/31/1955 23:00:00Converts the string to a date
The 1-hour difference is due to daylight saving time.Val("1/6/55 2:00")06/01/1955 01:00:002 am on 1 June 1955
The 1-hour difference is due to daylight saving time.Val("1-6-55 2:00")nothingVery sensitive to the format… ignoredStr(2484515)2484515If you supply a number, Str() will convert that number to a string.Dim D As Date = 2484515
label1.text = Str(D)16/05/2002 01:00:00The number has changed to a date.
Str() converts it into local date formatDim D As Date = 2484515
label1.text = D05/16/2002United States date formatCDate("1-6-55 2:00")Error“Wanted date, got string instead”
Not only sensitive, but offended...
DidYouKnow Constants