Page 1 of 1

Simple string handling for newbies

Posted: Wed Aug 04, 2004 9:31 pm
by GeoTrail
I've made a very simple example showing how to do some basic string manipulation in PureBasic.
Hopefully this will be usefull for people wanting to get started and learn PB :)

Code: Select all

; Simple string manipulation example by GeoTrail.

string.s    = "PureBasic Rocks!" ; Enter a string with a space here.
                                 ; Like a first and lastname.
Lenght.l    = Len(string.s)                 ; Get length of the string.
Left.s      = Left(string.s,1)              ; Get the first character in the string.
Mid.s       = Mid(string.s, Lenght.l/2, 1)  ; Get the middle character.
Right.s     = Right(string.s,1)             ; Get the right character.
Position    = FindString(string.s, " ", 1)  ; Get the position of the space in
                                            ; the string.

If Mid.s = " "          ; Check if there is a space in the middle of the string.
    Mid.s = "<space>"   ; If so, replace it with some informative text.
EndIf

Debug Left.s + " - " + Mid.s + " - " + Right.s + " - " + Str(Lenght.l)


Msg.s = "First part: " + Mid(string.s, 1, Position-1)
Msg.s + Chr(10) + "Second part: " + Mid(string.s, Position+1, Lenght-Position+1)
MessageRequester("Result", Msg.s)