Stupido string questions..

Just starting out? Need help? Post your questions and find answers here.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Stupido string questions..

Post 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:

Code: Select all

aftershave
to remove everything before "shave", to get this result:

Code: Select all

shave
and vice versa to remove everything after a string, like:

Code: Select all

aftershave
to remove everything after "after", so to get this result:

Code: Select all

after
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!
e2robot
New User
New User
Posts: 5
Joined: Sun Dec 25, 2005 11:17 am
Location: Australia

Post 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$)

e2robot
New User
New User
Posts: 5
Joined: Sun Dec 25, 2005 11:17 am
Location: Australia

Post by e2robot »

or using RemoveString

Code: Select all

test$="aftershave"
test$=RemoveString(test$, "after")
Debug (test$)


test$="aftershave"
test$=RemoveString(test$,"shave")
Debug (test$)
Last edited by e2robot on Sun Dec 25, 2005 11:32 am, edited 1 time in total.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

A good solution, but if I want to remove everything before "shave" without knowing there is "after"?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

mystr.s = "aftershave"
pos = FindString(mystr, "shave", 0)
mystr = Mid(mystr, pos, Len("shave"))
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

good one Trond! Thanks!
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

but Trond, is there a way doing that same but without knowing the length of "shave"?

Thanks!
okasvi
Enthusiast
Enthusiast
Posts: 150
Joined: Wed Apr 27, 2005 9:41 pm
Location: Finland

Post 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...
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Stupido string questions..

Post 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).
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Stupido string questions..

Post 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"
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Thanks people!

Those are good answers.

(@Fred have you something to add to PB4 about this? :lol: )
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Trond do you have a CropAfter() procedure that doesn't need Mid()?

Thank you very very much and a merry christmas!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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"
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Thanks thanks!!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply