Home > Language Reference > Functions

Strings

The String data type in conjunction with the functions described in this section allows you to manipulate character strings. It should be remembered that all operations on strings are costly both in terms of memory usage and calculation time; their use should be reserved for cases where they are necessary. Notably, strings should not be used to store or compare numeric values or dates.

Representation of strings

Basically, strings are blocks of bytes. However, how these bytes are interpreted depends on the code page of the device your application is running on. The following table summarizes this:

DeviceCode PageRemarks
WesternISO Latin1This code page is almost identical to the Microsoft® Windows® code page 1252. Each character is represented by one byte.
ChineseGB2312This code page is almost identical to the Microsoft® Windows® code page 936. In this encoding, latin characters are still represented by one byte, but ideographs require two bytes each.
JapaneseShift JISThis code page is almost identical to the Microsoft® Windows® code page 932. In this encoding, latin and katakana characters are still represented by one byte, but other ideographs require two bytes each.

HB++ also checks for the PiLoc utility, a small program you can use to display non latin scripts such as cyrillic or greek on your Palm OS device. In case this utility was found running, HB++ uses the character encoding the user specified in the PiLoc's configuration panel.

Because different devices may use different character encodings, the same block of data may represent different actual strings. For example, a string containing the two bytes &hCD and &hC1 will be interpreted as ÍÁ on a Western device and as 土 on a Far East device. This may cause troubles when beaming data from a device to another, when synchronizing data with a desktop computer, or when sending data to a web service. The Utf8 function may be used to convert a string back and forth between the current encoding and unicode.

While the device relies on a specific code page, the HB++ IDE uses unicode. This means your source code can virtually contain any character from any character set. When compiling, HB++ uses localisation information to encode with the proper code page the strings it encounters in your source code. For more information about code page selection and about writing portable applications, refer to the Localisation section in the Programmer's Guide.

You can use the Asc and Chr functions to determine respectively the code of a given character and the character corresponding to a given code. Because both Palm OS® and HB++ have to cooperate with external functions written in C, all character strings are internally null terminated. This implies a null character (a character whose code is zero) cannot be embeded into a string.

The maximum length of a string is limited to 32767 bytes. Building a longer string results in a runtime error. The Len function determines the number of characters in a string, while the LenB function returns the number of bytes a string occupies.

Manipulating strings

The comparison of two strings is carried out using the regular relational operators. Comparison is case sensitive, that is to say it takes upper and lower case into account. The order of two given characters is that of the respective codes of the two characters.

Sometimes, it is necessary to carry out a comparison that is case insensitive, ie not differentiate between upper and lower case. You can use the function StrComp which accepts a parameter that allows you to specify the comparison mode.

Concatenation of strings can be performed using the operator &.

The majority of regular functions for manipulating character strings are available. You can refer to the detailed list below for more information.

Converting numeric values

It is often useful to convert a character string entered into a text field into a numeric value, or conversely to convert a numeric value into a character string to display on screen.

The type conversion function CStr allows the conversion of numeric values into strings. The resulting string does not contain thousands separator, and the decimal separator is the decimal point. If you want to take into account regional settings preferences, you should use the function Format.

Conversely, if you want to convert a string containing a number into a numeric value, you can use the type conversion functions CByte, CInt, CLng, CSng, and CDbl. The string should not contain thousands separators and the decimal separator must be a decimal point. The function DelocalizeNumber returns a string without thousands separators or decimal points from a string formatted using the regional settings of the system.

If the string does not contain a number, a runtime error will occur. To avoid this, you should test the validity of the string in advance using the function IsNumeric. The following example shows how to extract a numeric value from a variable of type Double entered by the user into a text field.

Private Sub Button1_Click()
  Dim s As String, d As Double

  s = DelocalizeNumber(Field1.Text)
  if IsNumeric(s) Then
    d = CDbl(s)
    MsgBox "The value entered is : " & CStr(d)
  Else
    MsgBox "The string entered is not a number"
  End If
End Sub

When you want to convert a whole number, of which the range of validity is more narrow than that of type Double, you should check for overflow. A more efficient method that avoids explicitly testing is to use error handling:

Private Sub Button1_Click()
  Dim i As Integer

  On Error Goto ERR
  i = CInt(DelocalizeNumber(Field1.Text))
  MsgBox "The value entered is : " & CStr(i)
  Exit Sub

ERR:
  MsgBox "The string entered is not a number"
End Sub

It is also possible to convert whole numbers into their binary, octal or hexadecimal values using the functions Bin, Oct, and Hex. Converting binary, octal or hexadecimal values back is possible using the type conversion functions CByte, CInt, and CLng. Refer to the relevant pages for more information.

Reference

The functions described in this chapter are listed below.

Function Description
Asc Returns the ANSI code corresponding to the first letter in a string.
Bin Returns a string containing the binary representation of an integer.
BuildURL Builds an URL from a base address, a path and a filename.
Chr Returns a string containing the character associated to the given code.
CreatorToLong Converts a 4-characters string to a long value.
CreatorToString Converts a long value to a 4-characters string.
DelocalizeNumber Converts a string containing a number in a local format into a string containing a number in US format.
FilterAddress Tests whether an address matches a filter.
Format Returns a string containing a numeric expression or a date formatted according to the instructions contained in a formatting expression.
Hex Returns a string containing the hexadecimal representation of an integer.
InStr Returns a value specifying the position of the first occurrence of one string within another.
LCase Converts a string to lower case.
Left Extracts characters from the left side of a string.
Len Returns the number of characters in a string.
LenB Returns the number of bytes in a string.
LTrim Returns a copy of a string without leading spaces and tabs.
Mid (Get) Extracts part of a string.
Mid (Let) Replace part of a string with another.
Oct Returns a string containing the octal representation of an integer.
RemoveAccents Removes all accents in a string.
Replace Returns a string in which a specified substring has been replaced with another substring a specified number of times.
Right Extracts characters from the right side of a string.
RTrim Returns a copy of a string without trailing spaces and tabs.
Space Returns a string consisting of the specified number of spaces.
SplitURL Splits an URL in its constituents.
StrComp Compares two strings.
String Returns a string containing a repeating character string of the length specified.
Trim Removes spaces and tabs at the beginning and the end of a string.
TrimCrlf Returns a copy of a string without trailing carriage returns and line feeds.
UCase Converts a string to upper case.
UrlDecode Decodes a URL-encoded string.
UrlEncode Encodes an URL according to RFC 1738
Utf8 Conversion to UTF-8.