yes, that's correct BUT the question is:This is usually called Remove.
Remove from the end of the string
OR
Remove all occurrences within the string.
yes, that's correct BUT the question is:This is usually called Remove.
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
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"
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
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
Not a big deal, actually:owen wrote:the question is:This is usually called Remove.
Remove from the end of the string
OR
Remove all occurrences within the string.