String

Functions for making and taking apart strings. For how strings work, see Strings.

Positions count from 1, as in PureBasic: the first character is at position 1.

Len ✅

Length = Len(String$)

The number of characters in a string. For UTF-16 text this counts code units, so a character outside the Basic Multilingual Plane (an emoji, for example) counts as 2.

Returns the length. An empty string gives 0.

OpenConsole()
PrintN(Str(Len("hello")) + " " + Str(Len("")))

Output:

5 0

Notes. The length is stored with the string, so Len takes the same time for any length.

Left ✅

Result$ = Left(String$, Length)

The first Length characters. If Length is at least the string's length, the whole string.

Right ✅

Result$ = Right(String$, Length)

The last Length characters. If Length is at least the string's length, the whole string.

Mid ✅

Result$ = Mid(String$, StartPosition [, Length])

Part of a string, starting at StartPosition (1 = the first character).

Parameter Meaning
StartPosition where to start. Values below 1 are treated as 1. Past the end gives ""
Length how many characters. Leave it out, or pass a negative number, for the rest of the string
OpenConsole()
s$ = "Hello, world"
PrintN(Left(s$, 5) + "|" + Right(s$, 5) + "|" + Mid(s$, 8, 3) + "|" + Mid(s$, 8))

Output:

Hello|world|wor|world

Space ✅

Result$ = Space(Length)

A string of Length spaces.

OpenConsole()
PrintN("[" + Space(3) + "]")

Output:

[   ]

Chr ✅

Text$ = Chr(CharacterValue)

A one-character string for a UTF-16 code.

Asc ✅

Result = Asc(String$)

The UTF-16 code of the first character. An empty string gives 0.

OpenConsole()
PrintN(Chr(72) + Chr(105) + " " + Str(Asc("A")))

Output:

Hi 65

Str ✅

Result$ = Str(Value.q)

An integer as text.

OpenConsole()
PrintN(Str(-1234) + " " + Str(9223372036854775807))

Output:

-1234 9223372036854775807

Notes. Joining a number to a string with + calls Str automatically: "n=" + 5 gives "n=5".

StrD ✅

Result$ = StrD(Value.d [, NbDecimal])

A floating-point number as text.

Parameter Meaning
Value.d the number
NbDecimal how many decimal places. Leave it out for up to 10, with trailing zeros removed

The value is rounded to the last decimal place shown, from its exact value: a double is a binary fraction, so StrD(0.1, 20) is 0.10000000000000000555 and StrD(1e300, 0) has all 301 digits.

OpenConsole()
PrintN(StrD(10 / 3))
PrintN(StrD(2.5))
PrintN(StrD(2.5, 2))
PrintN(StrD(2.675, 1))
PrintN(StrD(-0.125, 2))

Output:

3.3333333333
2.5
2.50
2.7
-0.13

Notes. Joining a float to a string with + calls StrD without NbDecimal.

StrF ✅

Result$ = StrF(Value.f [, NbDecimal])

As StrD, for a float.

Val ✅

Result.q = Val(String$)

Reads an integer from the start of a string.

  • Leading spaces are skipped.
  • - or + can come first.
  • $ starts a hexadecimal number, % a binary one.
  • Reading stops at the first character that isn't a digit. A string with no number gives 0.
OpenConsole()
PrintN(Str(Val("42")) + " " + Str(Val("  -17 apples")) + " " + Str(Val("$FF")) + " " + Str(Val("%101")) + " " + Str(Val("x")))

Output:

42 -17 255 5 0

LCase ✅

Result$ = LCase(String$)

The string in lower case. Letters outside A–Z are converted too ("É" becomes "é"), using the operating system's case tables.

UCase ✅

Result$ = UCase(String$)

The string in upper case.

OpenConsole()
PrintN(LCase("Hello World") + " " + UCase("café"))

Output:

hello world CAFÉ

Trim ✅

Result$ = Trim(String$ [, Character$])

Removes a character (a space by default) from both ends of a string, as many times as it appears there.

LTrim ✅

Result$ = LTrim(String$ [, Character$])

As Trim, at the start only.

RTrim ✅

Result$ = RTrim(String$ [, Character$])

As Trim, at the end only.

OpenConsole()
PrintN("[" + Trim("  hi  ") + "][" + LTrim("  hi  ") + "][" + RTrim("  hi  ") + "][" + Trim("**hi**", "*") + "]")

Output:

[hi][hi  ][  hi][hi]

FindString ✅

Position = FindString(String$, StringToFind$ [, StartPosition [, Mode]])

Where StringToFind$ first appears in String$, at or after StartPosition (default 1).

Parameter Meaning
Mode #PB_String_CaseSensitive (default) or #PB_String_NoCase

Returns the position (1 = the first character), or 0 if it isn't found.

OpenConsole()
s$ = "PureBasic is pure"
PrintN(Str(FindString(s$, "Bas")) + " " + Str(FindString(s$, "pure")) + " " + Str(FindString(s$, "pure", 1, #PB_String_NoCase)) + " " + Str(FindString(s$, "x")))

Output:

5 14 1 0

ReplaceString ✅

Result$ = ReplaceString(String$, StringToFind$, ReplacementString$ [, Mode [, StartPosition [, NbOccurrences]]])

Replaces every occurrence of StringToFind$.

Parameter Meaning
Mode #PB_String_CaseSensitive (default) or #PB_String_NoCase
StartPosition where to start looking. Default 1
NbOccurrences the most replacements to make. Default: all

RemoveString ✅

Result$ = RemoveString(String$, StringToRemove$ [, Mode [, StartPosition [, NbOccurrences]]])

As ReplaceString with an empty replacement.

OpenConsole()
PrintN(ReplaceString("one two two", "two", "2"))
PrintN(ReplaceString("a-b-c-d", "-", "+", #PB_String_CaseSensitive, 1, 2))
PrintN(RemoveString("This Is Art", "is", #PB_String_NoCase))

Output:

one 2 2
a+b+c-d
Th  Art

CountString ✅

Result = CountString(String$, StringToCount$)

How many times StringToCount$ appears. Occurrences don't overlap: after each match, counting continues after it.

OpenConsole()
PrintN(Str(CountString("How many 'ow' contains Bow ?", "ow")) + " " + Str(CountString("aaaa", "aa")))

Output:

3 2

StringField ✅

Result$ = StringField(String$, Index, Delimiter$)

The Indexth field (1 = the first) of a string divided by Delimiter$, which can be several characters long. Out of range gives "".

OpenConsole()
line$ = "name,age,,city"
For k = 1 To 5
  PrintN(Str(k) + ": [" + StringField(line$, k, ",") + "]")
Next

Output:

1: [name]
2: [age]
3: []
4: [city]
5: []

ReverseString ✅

Result$ = ReverseString(String$)

The characters in reverse order.

LSet ✅

Result$ = LSet(String$, Length [, Character$])

Pads the string on the right with Character$ (a space by default) to Length characters. A longer string is cut to its first Length characters.

RSet ✅

Result$ = RSet(String$, Length [, Character$])

Pads on the left. A longer string is cut to its first Length characters.

InsertString ✅

Result$ = InsertString(String$, StringToInsert$, Position)

Inserts a string before the character at Position (1 = the start).

OpenConsole()
PrintN("[" + LSet("ab", 5) + "][" + RSet("42", 5, "0") + "][" + LSet("abcdef", 3) + "]")
PrintN(ReverseString("Hello") + " " + InsertString("Hello !", "World", 7))

Output:

[ab   ][00042][abc]
olleH Hello World!

Hex ✅

Result$ = Hex(Value.q [, Type])

The value in hexadecimal, upper case, with no leading zeros. The value is treated as unsigned, of the size given by Type: #PB_Byte or #PB_Ascii (8 bits), #PB_Word, #PB_Unicode or #PB_Character (16), #PB_Long (32), #PB_Quad (64, the default).

Bin ✅

Result$ = Bin(Value.q [, Type])

As Hex, in binary.

StrU ✅

Result$ = StrU(Value.q [, Type])

The value as an unsigned decimal number, with Type as for Hex.

OpenConsole()
PrintN(Hex(255) + " " + Hex(-1, #PB_Byte) + " " + Hex(-1) + " " + Bin(10))
PrintN(StrU(-1, #PB_Byte) + " " + StrU(-1))

Output:

FF FF FFFFFFFFFFFFFFFF 1010
255 18446744073709551615

Use RSet for leading zeros: RSet(Hex(12), 4, "0") gives "000C".

StringByteLength ✅

Result = StringByteLength(String$ [, Format])

The number of bytes the string takes in Format: #PB_Unicode (the default, 2 per character), #PB_UTF8 or #PB_Ascii. The terminating 0 isn't counted.

OpenConsole()
PrintN(Str(StringByteLength("héllo")) + " " + Str(StringByteLength("héllo", #PB_UTF8)) + " " + Str(StringByteLength("héllo", #PB_Ascii)))

Output:

10 6 5

ValD ✅

Result.d = ValD(String$)

Reads a floating-point number: optional spaces and sign, digits, an optional . and fraction, and an optional exponent (e or E, optional sign, digits). Reading stops at the first character that doesn't fit. The result is the double nearest to the number written (as for number literals in a program).

ValF ✅

Result.f = ValF(String$)

As ValD, giving a float.

OpenConsole()
PrintN(StrD(ValD("10.25")) + " " + StrD(ValD("-1.5e3")) + " " + StrD(ValD("2.5E-2")) + " " + StrD(ValF("0.5")))

Output:

10.25 -1500 0.025 0.5

Notes. The last digit of very long numbers may differ from a correctly rounded conversion. NaN and Infinity aren't recognised yet.

FormatNumber ✅

Result$ = FormatNumber(Number.d [, Decimals [, DecimalPoint$ [, ThousandsSeparator$]]])

Returns the number rounded to Decimals places (2 by default), with DecimalPoint$ (".") and the whole part's digits in groups of three, separated by ThousandsSeparator$ (","). A number that rounds to 0 has no minus sign.

OpenConsole()
PrintN(FormatNumber(125400.25))
PrintN(FormatNumber(-1234567.891, 3, ",", "."))
PrintN(FormatNumber(1125.8372, 2, ".", "") + " " + FormatNumber(42, 0))

Output:

125,400.25
-1.234.567,891
1125.84 42

EscapeString, UnescapeString ✅

Result$ = EscapeString(String$ [, Mode])
Result$ = UnescapeString(String$ [, Mode])

EscapeString returns the string with its special characters written as escapes. UnescapeString turns them back.

Mode Escapes
#PB_String_EscapeInternal (default) \ " `

` `` , as in ~"..." strings | | #PB_String_EscapeXML | & < > " '. Unescaping also reads A and A | | #PB_String_EscapeJSON | \ " ``, and for the other control characters |

OpenConsole()
PrintN(EscapeString("say " + Chr(34) + "hi" + Chr(34) + #TAB$ + "now"))
PrintN(EscapeString("<b>Tom & Jerry</b>", #PB_String_EscapeXML))
PrintN(UnescapeString("&lt;&#65;&#x42;&gt;", #PB_String_EscapeXML))

Output:

say \"hi\"\tnow
&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;
<AB>

Ascii, UTF8, Unicode ✅

*Buffer = Ascii(String$)
*Buffer = UTF8(String$)
*Buffer = Unicode(String$)

Return a new memory block holding the string in that format, ending in a zero, for passing to libraries and the system. Free it with FreeMemory. Ascii writes ? for characters it can't hold.

OpenConsole()
*u = UTF8("Héllo€")
PrintN(PeekS(*u, -1, #PB_UTF8) + " " + Str(MemorySize(*u)) + " bytes")
FreeMemory(*u)

Output:

Héllo€ 10 bytes

StartsWith, EndsWith ✅ (x additions)

Result = StartsWith(String$, Prefix$)
Result = EndsWith(String$, Suffix$)

Return 1 if String$ starts (or ends) with the given text, otherwise 0. They tell case apart. As for every x addition, a program's own procedure of the same name is used instead.

OpenConsole()
PrintN(Str(StartsWith("eXperimental", "eX")) + Str(EndsWith("main.pb", ".pb")) + Str(EndsWith("main.pb", ".PB")))

Output:

110

Not yet available 🔜

ResetLength, ReplaceString with #PB_String_InPlace (removed in PureBasic 6.40).