how can you separate this string please

General FreeBASIC programming questions.
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

how can you separate this string please

Postby super_castle » Feb 28, 2019 13:11

how can you separate this string please:

str1 = "12345: 123.7: 98765.0: 77778:
Separator is this ":".

str2="12345"
str3="123.7"
str4="98765.0"
str5="77778"

Thank you.
greeting
SARG
Posts: 909
Joined: May 27, 2005 7:15
Location: FRANCE

Re: how can you separate this string please

Postby SARG » Feb 28, 2019 13:31

Code: Select all

Dim As String strg="12345: 123.7: 98765.0: 77778:",str_sep(10)
Dim As Long p1,p2,scount
p1=1
p2=InStr(strg,":")
While p2
   scount+=1
   Str_sep(scount)=LTrim(Mid(strg,p1,p2-p1))''ltrim to remove first blank
   p1=p2+1
   p2=InStr(p1,strg,":")
Wend
For icount As Long = 1 To scount
   Print Str_sep(icount)
Next
Sleep
fxm
Posts: 9257
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: how can you separate this string please

Postby fxm » Feb 28, 2019 13:50

If you search for the topic titles containing (for example) the terms "split" or "splitting", you will find several topics about your requirement.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how can you separate this string please

Postby MrSwiss » Feb 28, 2019 14:00

Additionally, there is strtok(), which is found in "crt/string.bi".

Code: Select all

declare function strtok (byval as zstring ptr, byval as const zstring ptr) as zstring ptr
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how can you separate this string please

Postby MrSwiss » Mar 02, 2019 13:03

After searching the forum, I've found my old StrSplit() routine, which uses
strtok() function, again.

After some polishing it up (there where certain things I didn't really like),
here it goes again:

Code: Select all

' StrSplit_Sub-test1.bas -- (c) 2018-04-09, MrSwiss
' updated: 2019-03-02   reordered code in Sub, added code to test/demo section
'
' compile: -s console
'

#Include "crt/string.bi"                ' IMPORTANT: won't work without it !!!

Declare Sub StrSplit(ByRef As Const String, ByRef As Const String, () As String)

' ===== start test/demo code =====
Dim As String   tst = "aaaaaa; bbbb_cccccc;dddd'eeeee, fff", _  ' source string
                schr = " ;_',", _       ' search character's
                res(Any), _             ' not initialized, dynamic array
                title = "Sub StrSplit() using strtok() from FB's CRT library"

Print title                             ' display title
Print String(Len(title), "~")           ' show underline
Print                                   ' add a LF
Print tst; " ""source String"""         ' show source string
Print schr; " ""search char's - the first, is a Space"""  ' show search characters
Print
Print "show the splitted string-tokens" ' sub-title

StrSplit(tst, schr, res())              ' Sub call (get the string's tokens)
For i As UInteger = 0 To UBound(res)    ' we don't know the array's size
    Print i + 1; Tab(6); res(i)         ' show result's (if any!)
Next
Print : Print "done ... ";

Sleep                                   ' wait for a user key press (prog. exit)
' ===== end test/demo code =====

' ----- implementation -----
Sub StrSplit( _                         ' uses CRT's strtok() Function
    ByRef ssrc  As Const String, _      ' string to be searched
    ByRef chrs  As Const String, _      ' character(s), to search for
          res() As String _             ' result String array (ByRef, implicit!)
    )
    If Len(chrs) = 0 Then Exit Sub      ' nothing to search for --> get out
    If Len(ssrc) = 0 Then Exit Sub      ' no source string --> get out

    Erase(res)                          ' delete array's content (if any)
    Dim As String       s = ssrc        ' local variables: s = keeps ssrc
    Dim As ZString Ptr  psz = 0         ' result ptr (from strtok())
    Dim As UInteger     i = 0           ' counter, used as: array-index

    psz = strtok(s, chrs)               ' destroys s (must be reset! before reuse)
    While psz                           ' just to get required array size
        i += 1 : psz = strtok(0, chrs)  ' i - 1 = UBound(res)
    Wend

    ReDim res(0 To i - 1)               ' size array (using i from above run)
    i = 0 : s = ssrc : psz = 0          ' reset i, s and psz (for next run)

    psz = strtok(s, chrs)               ' get first token (string-part)
    While psz                           ' run until psz is = 0
        res(i) = *psz                   ' assign token to array
        psz = strtok(0, chrs)           ' get next token (string-part)
        i += 1                          ' increment index
    Wend
End Sub
' ----- end implementation -----

' ----- EOF -----
dodicat
Posts: 5984
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: how can you separate this string please

Postby dodicat » Mar 02, 2019 14:21

Here is mine:
https://www.freebasic.net/forum/viewtopic.php?f=7&t=26695&p=247648&hilit=*split*#p247648
Off topic
By the way, after messing with GoRC (and the virus warning) in the new fb 1.06, and inserting the seperate GoRc download, my Win 10 has packed in.
A dll in Windows defender folder has been reduced to 0 bytes and I cannot replace it, no matter how I gain the permissions to the folder.
Win 10 boots OK, but I have no access to anything on the start menu.
To shut down I must use shutdown /s from the command prompt (which I can get from fbide).
So I am on Linux just now, and Mr Swiss's splitter works just fine.
And I know you will all be splitting your sides chuckling at my tribulations.
Anyway, great excuse for a brand new box from Ebay.
Carry on.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how can you separate this string please

Postby MrSwiss » Mar 02, 2019 14:39

also OT
dodicat wrote:A dll in Windows defender folder has been reduced to 0 bytes and I cannot replace it, no matter how I gain the permissions to the folder.
Can't you just "disable" Win-Defender, I've done that, with no problems resulting, from that?
This, since you've got Avira and, two or more anti-viruses usually "bite" each other!
end OT

dodicat wrote:So I am on Linux just now, and Mr Swiss's splitter works just fine.
Hurray, to multi-OS coding !!! Thanks for testing, dodicat.
deltarho[1859]
Posts: 2083
Joined: Jan 02, 2017 0:34
Location: UK

Re: how can you separate this string please

Postby deltarho[1859] » Mar 02, 2019 20:25

dodicat wrote:And I know you will all be splitting your sides chuckling at my tribulations.

I'm not.
Anyway, great excuse for a brand new box from Ebay.

With a system backup, you would not have to do that.

These are my backups, on an external SSD. I use a slight compression on a full backup in the early hours of the morning when the PC is sleeping and so am I. I keep my C: drive pretty lean and that is also a SSD. A friend of mine uses the same method as I do but a HDD to an external HDD and his C: drive has a lot more data on it than mine. However, his backups are also done when he is asleep in bed so the details are academic. On a restore, however, he will need to put the kettle on. <smile> He doesn't put his PC to sleep - he has some software running 24 hours a day.
Image
Here are the details of the last backup which took only 3m 17s.
Image
In the morning I need to wake up my machine.

If what happened to you happened to me I'd shut down, pop a flash drive in and boot. Just over 15 minutes later, or thereabouts, I'd be back at the desktop as of 04:00 that morning. So far, of many I should say, I have never had to go back more than one day - and I have 7 days to choose from.

If you want more details but don't want to discuss it on the forum, and we are now well off topic, then drop me a line at d r @ d e l t a r h o . u k (sans spaces) and I will answer any questions that you may have.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: how can you separate this string please

Postby MrSwiss » Mar 02, 2019 21:41

Back on topic, again ...

Converted Sub to Function, that returns arrays upper bound, or error code.
Apart from that, the same functionality and parameters (arguments):

Code: Select all

' StrSplit_Func-test1.bas -- (c) 2019-03-02, MrSwiss
'
' compile: -s console
'

#Include "crt/string.bi"                ' IMPORTANT: won't work without it !!!

Declare Function StrSplit(ByRef As Const String, ByRef As Const String, () As String) As Long

' ===== start test/demo code =====
Dim As String   tst = "aaaaaa; bbbb_cccccc;dddd'eeeee, fff", _  ' source string
                schr = " ;_',", _       ' search character's
                res(Any), _             ' not initialized, dynamic array
                title = "Function StrSplit() using strtok() from FB's CRT library"
Dim As Long     rval = 0                ' error number or UBound(res)

Print title                             ' display title
Print String(Len(title), "~")           ' show underline
Print                                   ' add a LF
Print tst; " ""source String"""         ' show source string
Print schr; " ""search char's - the first, is a Space"""  ' show search characters
Print
Print "show the splitted string-tokens" ' sub-title

rval = StrSplit(tst, schr, res())       ' Function call (get the string's tokens)
Select Case As Const rval               ' error checking
    Case -3 : Print "ERROR: both Strings empty!"
    Case -2 : Print "ERROR: search char(s) String empty!"
    Case -1 : Print "ERROR: source String empty!"
    Case Else                           ' OK, rval holds array's UBound()
        For i As UInteger = 0 To rval   ' we now know, the array's size
            Print i + 1; Tab(6); res(i) ' show result's (if any!)
        Next
End Select

Print : Print "done ... ";

Sleep                                   ' wait for a user key press (prog. exit)
' ===== end test/demo code =====

' ----- implementation -----
Function StrSplit( _                    ' uses CRT's strtok() Function
    ByRef ssrc  As Const String, _      ' string to be searched
    ByRef chrs  As Const String, _      ' character(s), to search for
          res() As String _             ' result String array (ByRef, implicit!)
    ) As Long                           ' negative = error | >= 0 = OK
    Dim As Long     retv = 0            ' to hold return value

    If Len(chrs) = 0 Then retv -= 2     ' nothing to search for, set err
    If Len(ssrc) = 0 Then retv -= 1     ' no source string, set err
    If retv < 0 Then Return retv        ' return ERROR code (negative value)

    Erase(res)                          ' delete array's content (if any)
    Dim As String       s = ssrc        ' local variables: s = keeps ssrc
    Dim As ZString Ptr  psz = 0         ' result ptr (from strtok())
    Dim As UInteger     i = 0           ' counter, used as: array-index

    psz = strtok(s, chrs)               ' destroys s (must be reset! before reuse)
    While psz                           ' just to get required array size
        i += 1 : psz = strtok(0, chrs)  ' i - 1 = UBound(res)
    Wend

    retv = i - 1                        ' set arrays upper bound (return value)
    ReDim res(0 To retv)                ' size array (using retv)
    i = 0 : s = ssrc : psz = 0          ' reset i, s and psz (for next run)

    psz = strtok(s, chrs)               ' get first token (string-part)
    While psz                           ' run until psz is = 0
        res(i) = *psz                   ' assign token to array
        psz = strtok(0, chrs)           ' get next token (string-part)
        i += 1                          ' increment index
    Wend

    Return retv                         ' upper array bound (lower = 0 = default)
End Function
' ----- end implementation -----

' ----- EOF -----

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest