Gambas/String Manipulation

From Wikibooks, the open-content textbooks collection

< Gambas
Jump to: navigation, search

Contents

[edit] String Concatenation

The concatenation operator represented by an ampersand symbol is used to join two strings together:

DIM bestclub AS String
bestclub = "Liverpool" & " Football Club"    ' Liverpool Football Club

[edit] The addition operator cannot be used to concatenate strings

In gambas, unlike traditional basic, the plus symbol cannot be used as concatenation operator. If an attempt is made to use a plus symbol for this purpose, a type mismatch error will occur when an attempt is made to run the program:

' This does not work in gambas
PRINT "I have 3 cats" + " and 2 dogs"    ' You cannot concatenate strings by using a plus symbol

[edit] Concatenation combination assignment operator

The concatenation combination assignment operator represented by a andequals symbol, can also be used to concatenate strings:

DIM breakfast AS String
breakfast = "bacon"
breakfast &= " and eggs"    ' breakfast = breakfast & " and eggs" (using the andequals symbol)


[edit] Search and Replace

hello i have made a try to search in a text file -two function : 1- seach by line and column 2-search by pos

-first function returns an array with results it will be for example like this ["1,12" , "2,1"] , the first element is the line the second is the column

-second function returns also an array but single one ex:[1,2,45,455] every element is the string pos


here you are ! :

AUTHOR: abom MAIL: abom@linuxac.org


STATIC PUBLIC SUB searchfile(filename AS String, strser AS String) AS String[]

 DIM icounter AS Integer, fn AS stream, ln AS String, wpos AS Integer, rstr AS NEW String[]
 DIM spos AS Integer
 icounter = 0
 
 IF Exist(filename) THEN 
    fn = OPEN filename FOR READ
    
     WHILE NOT Eof(fn) 
     
       LINE INPUT #fn, ln
       icounter = icounter + 1
         IF strser = "" THEN RETURN 
         spos = 0
         wpos = 0
         DO
          wpos = InStr(ln, strser, spos, gb.Text) 
         
         IF wpos <> 0 THEN 
            spos = wpos + Len(strser)
            rstr.add(icounter & "," & wpos) 
         END IF
         LOOP UNTIL (wpos = 0) 
     WEND
 END IF
 RETURN rstr 
 

END




STATIC PUBLIC SUB searchfilebypos(filename AS String, strser AS String) AS String[]

 DIM icounter AS Integer, fn AS stream, txt AS String, wpos AS Integer, rstr AS NEW String[]
 DIM spos AS Integer
 icounter = 0
 
 IF Exist(filename) THEN 
      wpos = 0 
      spos = 0
      
      txt = file.Load(filename)
      
      IF strser = "" THEN RETURN 
      DO  
       
          wpos = InStr(txt, strser, spos, gb.Text) 
 
         IF wpos <> 0 THEN 
            spos = wpos + Len(strser)  
            rstr.add(wpos) 
         END IF
         
      LOOP UNTIL (wpos = 0)
 END IF
 RETURN rstr 
 

END