[Gambas-user] Split Function ???

Bruce Steers bsteers4 at gmail.com
Tue Jan 12 21:27:16 CET 2021


To be fair that was Fabiens reccomendation not mine.
Great tip using Chr() though :)

I'd implement my own function if using Replace() wasn't enough...
a simple function like below will split like Split() but use any length
string separator.

' tested with this..


* Debug SplitS("this text<br>split this way<br>like html",
"<br>").Join(",")*

* Debug Split("this text<br>split this way<br>like html", "<br>").Join(",")*


















*Public Sub SplitS(sArg As String, sFind As String) As String[]    Dim
sReturn As New String[]  Dim iCurPos, iLastPos As Integer  While iLastPos <
sArg.Len    iCurPos = InStr(sArg, sFind, iLastPos)    If iCurPos = 0 Then
    sReturn.Add(sArg[iLastPos, sArg.Len - iLastPos])      Break    Else
  sReturn.Add(sArg[iLastPos, iCurPos - 1 - iLastPos])      iCurPos =
iCurPos + sFind.Len - 1      iLastPos = iCurPos    Endif  Wend  Return
sReturnEnd*
Returns like this...

SplitS output: this text,split this way,like html

Split output: this text,,,,split this way,,,,like html

I'm sure the experts here could make that function better/faster :)

BruceS

On Tue, 12 Jan 2021 at 19:17, Tony Morehen <tmorehen at ajm-software.com>
wrote:

> Bruce, your recommended approach of Split(Replace ("Test - test2 - test3
> test4", " - ","|"), "|") is the correct approach.  You just have to be
> careful that the substitute split character will not be found in the text
> to split.  I'd recommend using:
>
> Split(Replace ("Test - test2 - test3 test4", " - ",Asc(31), Asc(31)).  You
> could use almost any low ascii character in place of Asc(31) but I think
> using "unit separator" is appropriate.
>
>
> On 2021-01-12 12:35 p.m., Bruce Steers wrote:
>
> ',' is the default delimiter for Split() if no separator is given.
>
> Using Replace ("Test - test2 - test3 test4", " - ",",")  will replace all
> ' - ' occurrence for ','
> then Split() without using a separator splits at every ','
>
> If you wanted to use your own delimiter char like '|' use...
> Split(Replace ("Test - test2 - test3 test4", " - ","|"), "|")
>
> BruceS
>
> On Tue, 12 Jan 2021 at 17:25, Olivier Coquet <ocoquet at 3d-phenomen.fr>
> wrote:
>
>> thank's Fabien, but I don't understand what is the "," ???
>>
>>
>> Olivier
>>
>> Le 12/01/2021 à 18:13, Fabien Bodard a écrit :
>> > Well
>> >
>> > split(Replace ("Test - test2 - test3 test4", " - ",","))
>> >
>> > will do the job
>> >
>> > Le mar. 12 janv. 2021 à 15:26, ocoquet <ocoquet at 3d-phenomen.fr> a
>> écrit :
>> >> Thank's Charlie, i'll try this....
>> >>
>> >> Regards
>> >>
>> >> Olivier
>> >>
>> >>
>> >> Envoyé depuis mon appareil Galaxy
>> >>
>> >>
>> >> -------- Message d'origine --------
>> >> De : Charlie Ogier <charlie at cogier.com>
>> >> Date : 12/01/2021 14:47 (GMT+01:00)
>> >> À : user at lists.gambas-basic.org
>> >> Objet : Re: [Gambas-user] Split Function ???
>> >>
>> >> Hi Olivier,
>> >>
>> >> Try this: -
>> >>
>> >> '****************************
>> >> Public Sub Form_Open()
>> >>
>> >>    Dim s As String = "Test - test2 - test3 test4"
>> >>    Dim sArray As String[] = Split(s, "-") 'NOT " - "
>> >>    Dim iLoop As Integer
>> >>
>> >>    For iLoop = 0 To sArray.Max
>> >>      Print Trim(sArray[iLoop])
>> >>    Next
>> >>
>> >> End
>> >> '****************************
>> >>
>> >> Charlie
>> >>
>> >> On 12/01/2021 13:03, Olivier Coquet wrote:
>> >>
>> >> Hi all,
>> >>
>> >> I have a problem with Split function.
>> >>
>> >> If I have a string like this:
>> >>
>> >> "Test - test2 - test3 test4"
>> >>
>> >> and I split with " - " separation string, I obtain
>> >>
>> >> "Test" "test2" "test3" "test4"
>> >>
>> >> and I want:
>> >>
>> >> "Test" "test2" "test3 test4"
>> >>
>> >> How can I make this ?
>> >>
>> >>
>> >> regards
>> >>
>> >> Olivier
>> >>
>> >>
>> >> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>> >>
>> >>
>> >>
>> >> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>> >
>> >
>>
>> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210112/d6a6d3e3/attachment-0001.htm>


More information about the User mailing list