[Gambas-user] Split and escape characters

Mike Keehan Mike at ...680...
Tue Apr 8 13:52:11 CEST 2008


> richard terry wrote:
>> "Escape is an escape character. Any separator characters enclosed between two 
>> escape characters are ignored in the splitting process. If Escape contains 
>> two characters, then the first is the starting escape character, and the 
>> second the ending one. "
>>
>> So says the Docs, escape is even underlined.
>>
>> For a dummy line me What are escape characters? (I mean not just what they do, 
>> but can someone list them, show an example)
>>
>>
>> Thanks.
>>
> 
> 
> Hey Richard - Try this:
> 
> Dim SPL as String[]
> Dim MSG as string
> 
> MSG = "This Line,Is Split at,The Commas"
> 
> SPL = Split(MSG, ",")
> 
> You can now read the contents of SPL[0] etc
> 
> The comma is used as an escape char in this case, although it can be 
> quite a few other symbols. Be careful though, because not all of the 
> symbols you can type can be used it seems, but # @ ~ ^ all seem to work.
> 

Er, no.  Those commas are separators.

An escape is given as the third parameter to Split().  You can choose 
what character you want as an escape.  A single quote is used as an 
escape in this version:-


  Dim SPL as String[]
  Dim MSG as String
  DIM Sout as String

  MSG = "This Line, Is Split at, The Commas, 'except, for, this,' bit."

  SPL = Split(MSG, ",", "'")

  FOR EACH Sout in SPL
    PRINT Sout
  NEXT


Should print something like -

"This Line"
" Is Split at"
" The Commas"
" except, for, this, bit."

What the pair of these single quote escape characters do is to prevent 
any splitting occurring to the text between them.

Using two escapes is similar :-


  Dim SPL as String[]
  Dim MSG as String
  DIM Sout as String

  MSG = "This Line, Is Split at, The Commas, (except, for, this,) bit."

  SPL = Split(MSG, ",", "()")

  FOR EACH Sout in SPL
    PRINT Sout
  NEXT


Should print something like -

"This Line"
" Is Split at"
" The Commas"
" except, for, this, bit."

Which characters you use for escapes depends on the contents of the 
text that you are splitting.  You might not need escapes at all.

Mike.






More information about the User mailing list