Page 1 of 2
Stupido string questions..
Posted: Sun Dec 25, 2005 10:48 am
by josku_x
Hello!
I just cannot imagine that I cannot get the solution to this.. my problem is, I want to remove everything before a string in a string.
That is like:
to remove everything before "shave", to get this result:
and vice versa to remove everything after a string, like:
to remove everything after "after", so to get this result:
RemoveString() is my friend, but I don't know how to use it in this situation, I also know there may be good use of Mid().
Thank you very very much, any hlep is really appreciated!
Posted: Sun Dec 25, 2005 11:28 am
by e2robot
Just replace with null string.....
Code: Select all
test$="aftershave"
test$=ReplaceString(test$,"after","")
Debug (test$)
test$="aftershave"
test$=ReplaceString(test$,"shave","")
Debug (test$)
Posted: Sun Dec 25, 2005 11:31 am
by e2robot
or using RemoveString
Code: Select all
test$="aftershave"
test$=RemoveString(test$, "after")
Debug (test$)
test$="aftershave"
test$=RemoveString(test$,"shave")
Debug (test$)
Posted: Sun Dec 25, 2005 11:31 am
by josku_x
A good solution, but if I want to remove everything before "shave" without knowing there is "after"?
Posted: Sun Dec 25, 2005 11:38 am
by Trond
Code: Select all
mystr.s = "aftershave"
pos = FindString(mystr, "shave", 0)
mystr = Mid(mystr, pos, Len("shave"))
Posted: Sun Dec 25, 2005 1:08 pm
by josku_x
good one Trond! Thanks!
Posted: Sun Dec 25, 2005 1:09 pm
by josku_x
but Trond, is there a way doing that same but without knowing the length of "shave"?
Thanks!
Posted: Sun Dec 25, 2005 1:38 pm
by okasvi
Code: Select all
Procedure.s RemPartOfStr(Str.s, StrToLeave.s)
Pos.l = FindString(Str, StrToLeave, 0)
ProcedureReturn Mid(Str, Pos, Len(StrToLeave))
EndProcedure
Debug RemPartOfStr("AfterShave","Shave")
you need to know atleast what you want to leave...
Re: Stupido string questions..
Posted: Sun Dec 25, 2005 1:51 pm
by PB
Here's how I'd do it:
> to remove everything before "shave"
Code: Select all
Procedure.s CropBefore(string$,what$)
ProcedureReturn Mid(string$,FindString(string$,what$,1),99999)
EndProcedure
Debug CropBefore("aftershave","shave") ; Returns "shave"
> to remove everything after "after"
Code: Select all
Procedure.s CropAfter(string$,what$)
ProcedureReturn Mid(string$,Len(what$)+FindString(string$,what$,1),99999)
EndProcedure
Debug CropAfter("aftershave","after") ; Returns "shave"
(@Fred: The above shows why Mid would be nice without having to specify
a length parameter... because right now we need to use something large
like 99999 to get the rest of the string).
Re: Stupido string questions..
Posted: Sun Dec 25, 2005 2:13 pm
by Trond
PB wrote:(@Fred: The above shows why Mid would be nice without having to specify a length parameter... because right now we need to use something large like 99999 to get the rest of the string).
Or just drop Mid entirely.
Code: Select all
Procedure.s CropBeforeA(string.s, what.s)
ProcedureReturn PeekS(@string.s + FindString(string.s, what.s, 1)-1)
EndProcedure
Procedure.s CropBefore(string.s, what.s)
ProcedureReturn Right(string.s, Len(string.s)-FindString(string.s, what.s, 1)+1)
EndProcedure
Debug CropBeforeA("aftershave","shave") ; Returns "shave"
Debug CropBefore("aftershave","shave") ; Returns "shave"
Posted: Sun Dec 25, 2005 2:34 pm
by josku_x
Thanks people!
Those are good answers.
(@Fred have you something to add to PB4 about this?

)
Posted: Sun Dec 25, 2005 2:49 pm
by josku_x
Trond do you have a CropAfter() procedure that doesn't need Mid()?
Thank you very very much and a merry christmas!
Posted: Sun Dec 25, 2005 4:10 pm
by Trond
Code: Select all
Procedure.s CropAfterA(string.s, what.s)
ProcedureReturn RemoveString(string.s, what.s)
EndProcedure
Procedure.s CropAfterB(string.s, what.s)
ProcedureReturn PeekS(@string.s+Len(what.s))
EndProcedure
Procedure.s CropAfterC(string.s, what.s)
ProcedureReturn Right(string.s, Len(string.s)-Len(what.s))
EndProcedure
Debug CropAfterA("aftershave","after") ; Returns "shave"
Debug CropAfterB("aftershave","after") ; Returns "shave"
Debug CropAfterC("aftershave","after") ; Returns "shave"
Posted: Sun Dec 25, 2005 4:18 pm
by josku_x
Thanks thanks!!
Posted: Mon Dec 26, 2005 1:15 am
by PB
Using RemoveString is flawed because it doesn't actually crop. Observe:
Code: Select all
Procedure.s CropAfterA(string.s, what.s)
ProcedureReturn RemoveString(string.s, what.s)
EndProcedure
Debug CropAfterA("two lots of after in here aftershave","after")