Repository navigation
Operator String Index
Home › Operators › Operator [] (String Index)
Returns a reference to the numeric value of a character in a string
Declare Operator [] ( ByRef lhs As String, ByRef rhs As Integer ) ByRef As UByte
Declare Operator [] ( ByRef lhs As ZString, ByRef rhs As Integer ) ByRef As UByte
Declare Operator [] ( ByRef lhs As WString, ByRef rhs As Integer ) ByRef As TlhsrhsT
This operator returns a reference to the numeric value of a specific character in a string:
a UByte (containing the ASCII value of the character).
a numeric type depending on platform, for example UShort for Windows or ULong for Linux and macOS (containing the numeric value of the character).
This operator must not be used in case of empty string because reference is undefined (inducing runtime error).
Otherwise, the user must ensure that the index does not exceed the range "[0, Len(lhs) - 1]". Outside this range, results are undefined.
Unlike 'return by value' (where only a copy is returned), 'return by reference' allows you to also modify the referenced variable.
'Return by reference' is implemented under the hood as a pointer implicitly dereferenced:
- In case of a String or a ZString 's':
s[n] is equivalent to *CPtr(Ubyte Ptr, StrPtr(s) + n)
- In case of a WString 's':
s[n] is equivalent to *CPtr(T Ptr, StrPtr(s) + n)
Note: The fact that this operator returns a reference greatly differentiates it from Asc( str [, position ] ) which allows to return the numeric representation of a character, but not to modify it.
result = lhs [ rhs ]
or
lhs [ rhs ] = value- New to FreeBASIC
Dim a As String = "Hello, world!"
Dim i As Integer
For i = 0 To Len(a) - 1
Print Chr(a[i]) & " ";
Next i
Print
Print
For i = 1 To 4
a[i] = a[i] - 32 ' converting lowercase alphabetic characters to uppercase
Next i
For i = 7 To 11
a[i] = a[i] - 32 ' converting lowercase alphabetic characters to uppercase
Next i
Print aWill print:
H e l l o , w o r l d !
HELLO, WORLD!
Category: Operators
Kind: operator
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing