MID strings
MID strings
The Mid function only seems to work in one direction such as:-
Ans$ = Mid(afirst$, 3, 1)
It would be very useful if it followed normal convention in other basics and worked in reverse such as:-
Mid(afirst$, 3, 1) = "4"
Appreciate that ReplaceString is not an answer since this function seems to require that the character being replaced in the original string is known.
At the moment it would seem one has to deconstruct the string in order to insert a different character, or alternatively, find the character using the Mid function in order to identify the character one is replacing using ReplaceString!.
Ans$ = Mid(afirst$, 3, 1)
It would be very useful if it followed normal convention in other basics and worked in reverse such as:-
Mid(afirst$, 3, 1) = "4"
Appreciate that ReplaceString is not an answer since this function seems to require that the character being replaced in the original string is known.
At the moment it would seem one has to deconstruct the string in order to insert a different character, or alternatively, find the character using the Mid function in order to identify the character one is replacing using ReplaceString!.
Regards,
Ernest
Ernest
-
- Enthusiast
- Posts: 613
- Joined: Tue May 06, 2003 2:50 pm
- Location: Germany
- Contact:
Re: MID strings
I consider that dirty styled code. Why should I assign values to a function? Fred would have to make an exception only for this function, only to implement dirty code that causes the compiler to be bloatier, uglier and that could cause other bugs undreamt of. As to the convention: PureBasic <> BASIC (well, I know about the advertisingeevee wrote: The Mid function only seems to work in one direction such as:-
Ans$ = Mid(afirst$, 3, 1)
It would be very useful if it followed normal convention in other basics and worked in reverse such as:-
Mid(afirst$, 3, 1) = "4"

What's wrong about a command like
Code: Select all
result$ = InSet(original$, replacement$, position)
<°)))o><²³
- Fangbeast
- PureBasic Protozoa
- Posts: 4789
- Joined: Fri Apr 25, 2003 3:08 pm
- Location: Not Sydney!!! (Bad water, no goats)
opinions
freedimension, this is the " Feature Requests and Wishlists" section where people may ask Fred for whatever they like without being told what they can and cannot have by an opinionated 3'rd party and Fred may or may not consider it but that is his perogative and not yours.. Evee didn't ask for your opinion of his request and this is the wrong section for it.
Regards.
Regards.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
well, the question has been raised before, and though dirty it's regular basic syntax in most other basics 
fortunately this one is easy to write

fortunately this one is easy to write

Code: Select all
Procedure.s x_mid(x.s,p.l,l.l,y.s)
x=Left(x,p-1)+y+Mid(x,p+l,Len(x))
ProcedureReturn x
EndProcedure
Debug x_mid("1234",2,2,"A")
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 499
- Joined: Wed Sep 17, 2003 9:17 pm
- Location: Southern California
- Contact:
@blueznl
I didn't notice your post and I came up with...the exact same thing...
I didn't notice your post and I came up with...the exact same thing...

Code: Select all
Procedure.s InSet(OriginalString$,Position,Length,ReplacementString$)
ProcedureReturn Left(OriginalString$,Position-1)+ReplacementString$+Right(OriginalString$,Len(OriginalString$)-(Position+Length-1))
EndProcedure
String$="Hello, my name is PolyVector :)"
Debug InSet(String$,8,7,"your master")
-
- Enthusiast
- Posts: 613
- Joined: Tue May 06, 2003 2:50 pm
- Location: Germany
- Contact:
Re: opinions
Well, than consider my post as the wish NOT TO IMPLEMENT THISFangbeast wrote:freedimension, this is the " Feature Requests and Wishlists" section where people may ask Fred for whatever they like without being told what they can and cannot have by an opinionated 3'rd party and Fred may or may not consider it but that is his perogative and not yours.. Evee didn't ask for your opinion of his request and this is the wrong section for it.
If you can't stand discussion what the h... are you doing in a discussion board?
If you can't see it, I wanted to help but now I'm really pissed off. Thx, you made my day.
<°)))o><²³
Hey folks!
Stop throwing things at each other!
You are all obviously real programmers and not just code sloggers like me!
All I know is that the sheer simplicity of:-
is not available.
I now have to use something as convoluted as:-
or a construct based on:-
Use MID to detect existing character in the string
Use ReplaceString to do the replacement.
If the suggested InSet is the 'proper' way to do things, that' fine - it would still give me a single line of code that can be inserted anywhere in the program without much thought.
As it is, in the example above, one has to be careful to repeat the position (the two 3s) or set it as a Procedure (with the parameter-passing overheads that would incur) as others have indicated.
Now do you want a real battle? My pet dislike?? The Keyboard functions???
Stop throwing things at each other!
You are all obviously real programmers and not just code sloggers like me!
All I know is that the sheer simplicity of:-
Code: Select all
Mid(afirst$, 3, 1) = "4"
I now have to use something as convoluted as:-
Code: Select all
afirst$=Left(afirst$,3-1)+"4"+Right(afirst$,Len(afirst$)-3)
Use MID to detect existing character in the string
Use ReplaceString to do the replacement.
If the suggested InSet is the 'proper' way to do things, that' fine - it would still give me a single line of code that can be inserted anywhere in the program without much thought.
As it is, in the example above, one has to be careful to repeat the position (the two 3s) or set it as a Procedure (with the parameter-passing overheads that would incur) as others have indicated.
Now do you want a real battle? My pet dislike?? The Keyboard functions???

Regards,
Ernest
Ernest
-
- Enthusiast
- Posts: 499
- Joined: Wed Sep 17, 2003 9:17 pm
- Location: Southern California
- Contact:
@freedimention
Fangbeast is just bein' weird, your comments are always welcome... I totally agree that you can't change the syntax of an entire language for a single command... Every BASIC has it's own dialect and this one simply wouldn't fit...
@eevee
It's a good suggestion, but isn't likely to be practical to impliment... But that's up to Fred
Fangbeast is just bein' weird, your comments are always welcome... I totally agree that you can't change the syntax of an entire language for a single command... Every BASIC has it's own dialect and this one simply wouldn't fit...
@eevee
It's a good suggestion, but isn't likely to be practical to impliment... But that's up to Fred

-
- Enthusiast
- Posts: 613
- Joined: Tue May 06, 2003 2:50 pm
- Location: Germany
- Contact:
Very good ideas.GPI wrote:Btw, i would prefer the name MidSet(a$,pos.l,len.l,replace$,[flag])
Flag=
#PB_MidSet_Direct = change direct in a$, do not create a new string and return it. the length of replace$ must be len.l Like ReplaceString.
One Question though: What is the use of the additional len.l parameter? Isn't the length given by the length of replace$ ? I'm (really) sure you thought something about that, but I can't get it.
@eevee: I didn't meant to be rude to you, I hope you know that. I only wanted to help the cause. Sharing thoughts is part of that process that leads to progress

<°)))o><²³
for example
Code: Select all
a$="Ärger"
midset(a$,1,1,"Ae")
-
- Enthusiast
- Posts: 613
- Joined: Tue May 06, 2003 2:50 pm
- Location: Germany
- Contact:
Code: Select all
Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s)
aMemPos.l=*PtrStr+DestPos-1
If DestLen>1
LastChar.b=PeekB(aMemPos+DestLen)
PokeS(aMemPos, ReplaceStr, DestLen)
PokeB(aMemPos+DestLen,LastChar)
Else
PokeB(aMemPos,PeekB(@ReplaceStr))
EndIf
EndProcedure
strTest.s="Hello"
MessageRequester("Before",strTest)
MidSet(@strTest, 3, 2, "Booh")
MessageRequester("After",strTest)
Last edited by Andras on Tue Feb 01, 2005 8:07 pm, edited 1 time in total.
Undank ist der Welten Lohn
Or with ErrorChecking:
Code: Select all
Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s)
aMemPos.l=*PtrStr+DestPos-1
StrLen.l=MemoryStringLength(*PtrStr)
If aMemPos>=*PtrStr
If aMemPos<*PtrStr+StrLen
If DestLen>Len(ReplaceStr)
DestLen=Len(ReplaceStr)
EndIf
If DestPos+DestLen>StrLen
DestLen=StrLen-DestPos+1
EndIf
If DestLen>1
LastChar.b=PeekB(aMemPos+DestLen)
PokeS(aMemPos, ReplaceStr, DestLen)
PokeB(aMemPos+DestLen,LastChar)
ElseIf DestLen=1
PokeB(aMemPos,PeekB(@ReplaceStr))
EndIf
EndIf
EndIf
EndProcedure
strTest.s="Hello"
MessageRequester("Before",strTest)
MidSet(@strTest, 3, 2, "Booh")
MessageRequester("After",strTest)
Undank ist der Welten Lohn
or
Code: Select all
Procedure.s MidSet(string$,position,length,ReplaceString$); - Replace a part in the string and return the result
ProcedureReturn Left(string$,position-1)+ReplaceString$+Right(string$,Len(string$)-position-length+1)
EndProcedure
Procedure MidSetDirect(*string.BYTE,position,*ReplaceString.BYTE); - Replace a part in the string direct!
*string+(position-1)
While *ReplaceString\b
*string\b=*ReplaceString\b:*string+1:*ReplaceString+1
Wend
EndProcedure