Concatenate-UNCONCATENATE

General FreeBASIC programming questions.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: Concatenate-UNCONCATENATE

Post by owen »

This is usually called Remove.
yes, that's correct BUT the question is:
Remove from the end of the string
OR
Remove all occurrences within the string.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Concatenate-UNCONCATENATE

Post by Josep Roca »

You can write your own functions to do whatever you wish.

Code: Select all

''
''  FF_REMOVE (by Paul Squires)
''  Return a copy of a string with characters or strings removed
''
''  If MatchPattern is not present in MainString, all of MainString is returned intact.
''  The replacement can cause MainString to grow or condense in size. 
''  This function is case-sensitive. 
''
Function StrRemove( ByRef sMainString   As String, _
                           ByRef sMatchPattern As String _
                         ) As String

    Dim i As Long 
    Dim s As String 
   
    If Len(sMainString) = 0 Then Return sMainString

    s = sMainString
    Do
        i = Instr(s, sMatchPattern)
        If i > 0 Then 
           s = Left(s, i - 1) & Mid(s, i + Len(sMatchPattern))
        End If   
    Loop Until i = 0
   
    Return s

End Function

FUNCTION StrRemoveAtEnd (BYREF sMainString AS STRING, BYREF sMatchPattern AS STRING) AS STRING
   DIM p AS LONG = InStrRev(sMainString, sMatchPattern)
   IF p THEN FUNCTION = MID(sMainString, 1, p - 1)
END FUNCTION

DIM s AS STRING
s = StrRemove("Hello World. Welcome to the Freebasic World", "World")
print s

s = "Hello World. Welcome to the Freebasic World"
s = StrRemoveAtEnd(s, "World")
print s

PRINT
PRINT "Press any key..."
SLEEP
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Concatenate-UNCONCATENATE

Post by xlucas »

This is a very interesting topic. It's... uhm... "philosophic" :P
In my opinion, a string concatenation operator in FreeBasic is irreversible as it is because information is being lost in the process, so it cannot be reproduced. What concatenations suggests is not insertion, but appending a string at the end of another. A string in FB contains a text, but also contains a length. When you concatenate two strings, the texts are both kept, but the lengths are added, so their original values become undetermined. Because operator & (or +) is a binary string operator that returns a string, the opposite should be a unary string operator that returns two strings. Let's call this operator $:

Code: Select all

Dim As String a, b, c

c = a & b  'This is how we concatenate
(a, b) = $c  'This should be, in theory, how we'd "unconcatenate"
Of course, this is both syntactically incompatible with FB and logically impossible (because of information being lost). We can solve the first problem like this:

Code: Select all

Type TwoStrings
   s1 As String
   s2 As String
End Type

Dim d As TwoStrings
Dim As String a, b, c

c = a & b
d = $c

Print d.s1, d.s2
To solve the second problem, we should somehow prevent the information from being lost. One way would be to redefine the way in which strings are being interpreted (printed, for example). Say an original string (one that has been created by assigning a constant to a variable) always end in a Chr(0). We could make Print not print such characters, so that now c = a & b is reversible. This kind of thing is sometimes useful. I've made programs in the past that used strings that contained an end marker or a length prefix precisely to be able to concatenate them, for instance, to save them to a file, and then retrieve them. "Uninserting" a string from another may be useful at some point, but I don't think it'd be enough as to define an operator for that purpose. Just thoughts... ha, ha
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Concatenate-UNCONCATENATE

Post by dodicat »

You could remember the removed characters thus:

Code: Select all



Sub string_split(byval s As String,chars As String,result() As String)
    redim result(0)
    dim as long L=len(chars),pst
    Dim As String var1,var2 
      #macro split(stri)
    pst=Instr(stri,  chars)
    var1="":var2=""
    If pst<>0 Then
    var1=Mid(stri,1,pst-1)
    var2=Mid(stri,pst+L)
    Else
    var1=stri
     End if
    if len(var1) then 
    redim preserve result(1 to ubound(result)+1)
    result(ubound(result))=var1
    end if
    #endmacro
   Do
   split(s):s=var2
Loop Until var2=""
End Sub

function Remove(byval s as string,chars as string,flag as boolean=0) as string
    static as string g()
      dim as string res
    if flag=0 then
    string_split(s,chars,g())
    for n as long=lbound(g) to ubound(g)
        res+=g(n)
    next
else
    for n as long=lbound(g) to ubound(g)
        res+=g(n)+chars
    next
    res=rtrim(res,chars)
end if
return res
end function

function insert(byval s as string,chars as string) as string
    return remove(s,chars,1)
    end function

dim as string g="Hello World! "

for n as long=1 to 6
    g+=g
next
print "The string"
print g
print


dim as string s= remove(g,"World")
print """World"" out"
print s
print
print """World"" back in"
print insert(s,"World")
sleep
    

     
But is it really worth it?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Concatenate-UNCONCATENATE

Post by jj2007 »

owen wrote:
This is usually called Remove.
the question is:
Remove from the end of the string
OR
Remove all occurrences within the string.
Not a big deal, actually:

from end = Left$(src$, numchars)
within = Replace$(src$, find$, repl$, options)
Post Reply