[SOLVED] Essential string handling functions

Just starting out? Need help? Post your questions and find answers here.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

[SOLVED] Essential string handling functions

Post by Oso »

I'm looking for several everyday string handling functions. Most I've been able to find in the PureBasic documentation, but not the following. Hope someone can advise if there are equivalents.

1. PureBasic has var.s=space(n) to generate n spaces, but I can’t find something similar to generate n specified repeating characters or repeating strings.
The language I’ve used in the past, which is PICK DataBasic, has the following, which will generate AbAbAbAbAb

Code: Select all

result.s = STR(“Ab”,5)
2. Extracting an item of delimited text based on a delimiter/separator in a string

Code: Select all

value.s = “VAL1/VAL2/VAL3/VAL4”
If I want the third value, how do I specify that?
Again, in the language I’m more familiar with, this would be done like so…

Code: Select all

result.s = FIELD(value.s, ”/”, 3)
3.a. Padding a value so that it appears justified in a column. For instance, each of the set of numbers, 4 45 128 needs to be padded with leading spaces to make each of them six characters in width.

Code: Select all

     4
    45
   128
The language I’ve been using provides what’s called a ‘format string’ for which incidentally the R means right-justify and 6 is the justification width.

Code: Select all

value.s = “45”
result.s = value’R#6’
3.b. It also provides the following for inserting commas between thousands…

Code: Select all

value.s = “978000”
result.s = value’R,#6’
3.c. And we could left-justify with the below, which will place the padding on the end of the output.

Code: Select all

result.s = value’L,#6’
These string handling features tend to be appropriate and have very practical uses for developing business apps. A lot of other languages I've used, such as C# provide comparatively very little by way of string handling when it comes to this kind of simple requirement. Like a lot of modern languages, they over-complicate what should be an everyday need, a case in point being Regex. Hoping that PB provides something non-complicated anyway. Thanks.
Last edited by Oso on Fri Jan 27, 2023 8:46 am, edited 2 times in total.
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Essential string handling functions

Post by Bitblazer »

Oso wrote: Tue Aug 09, 2022 12:43 pm I'm looking for several everyday string handling functions. Most I've been able to find in the PureBasic documentation, but not the following. Hope someone can advise if there are equivalents.

1. PureBasic has var.s=space(n) to generate n spaces, but I can’t find something similar to generate n specified repeating characters or repeating strings.
The language I’ve used in the past, which is PICK DataBasic, has the following, which will generate AbAbAbAbAb

Code: Select all

result.s = STR(“Ab”,5)
Look into Macros. The quicker and dirty way would be to use a loop. But with a macro it would be more elegant and readable.
Oso wrote: Tue Aug 09, 2022 12:43 pm2. Extracting an item of delimited text based on a delimiter/separator in a string

Code: Select all

value.s = “VAL1/VAL2/VAL3/VAL4”
If I want the third value, how do I specify that?
Again, in the language I’m more familiar with, this would be done like so…

Code: Select all

result.s = FIELD(value.s, ”/”, 3)
Check out StringField
Oso wrote: Tue Aug 09, 2022 12:43 pm4 45 128 [/b]needs to be padded with leading spaces to make each of them six characters in width.

Code: Select all

     4
    45
   128
The language I’ve been using provides what’s called a ‘format string’ for which incidentally the R means right-justify and 6 is the justification width.

Code: Select all

value.s = “45”
result.s = value’R#6’
See FormatNumber
Oso wrote: Tue Aug 09, 2022 12:43 pm3.b. It also provides the following for inserting commas between thousands…

Code: Select all

value.s = “978000”
result.s = value’R,#6’
3.c. And we could left-justify with the below, which will place the padding on the end of the output.

Code: Select all

result.s = value’L,#6’
These string handling features tend to be appropriate and have very practical uses for developing business apps. A lot of other languages I've used, such as C# provide comparatively very little by way of string handling when it comes to this kind of simple requirement. Like a lot of modern languages, they over-complicate what should be a simple need. Hoping that PB provides something non-complicated anyway. Thanks.
Check out the String functions.
webpage - discord chat links -> purebasic GPT4All
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Essential string handling functions

Post by RASHAD »

Hi Oso
Why do you expect PB to do what you have been using in other languages if you can do it with PB in too many wayes
See for your first exam as example I can do it at least with 5 ways and as simple more than you expect

Code: Select all

result.s = LSet("A",20,"A")
Debug ReplaceString(result,"A","Ab")
Egypt my love
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Essential string handling functions

Post by Oso »

Bitblazer wrote: Tue Aug 09, 2022 1:03 pm Check out StringField
Thanks, I'd missed that one - StringField is a perfect functional match for the example I used.
Bitblazer wrote: Tue Aug 09, 2022 1:03 pm See FormatNumber
That's similar, I just need to take care of the padding. Thanks for the others.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Essential string handling functions

Post by Demivec »

Here are some answers to your questions but as RASHAD pointed out there is more than one possible solution:

Code: Select all

; 1. PureBasic has var.s=space(n) to generate n spaces, but I can’t find something similar to generate n specified repeating characters or repeating strings.
; The language I’ve used in the past, which is PICK DataBasic, has the following, which will generate AbAbAbAbAb
; result.s = STR(“Ab”,5)
result.s = ReplaceString(Space(5), " ", "Ab")
Debug "1: " + result

; 2. Extracting an item of delimited text based on a delimiter/separator in a string
; value.s = “VAL1/VAL2/VAL3/VAL4”
; If I want the third value, how do I specify that?
; Again, in the language I’m more familiar with, this would be done like so…
; result.s = FIELD(value.s, ”/”, 3)
value.s = "VAL1/VAL2/VAL3/VAL4"
result.s = StringField(value.s, 3, "/")
Debug "2: " + result

; 3.a. Padding a value so that it appears justified in a column. For instance, each of the set of numbers, 4 45 128 needs to be padded with leading spaces to make each of them six characters in width.
;      4
;     45
;    128
; The language I’ve been using provides what’s called a ‘format string’ for which incidentally the R means right-justify and 6 is the justification width.
; value.s = “45”
; result.s = value’R#6’
result.s = RSet(Str(4), 6) ;defaults to padding with a space
Debug "3.a: " + result + "; length: " + Len(result)
result.s = RSet(Str(45), 6)
Debug "3.a: " + result + "; length: " + Len(result)
result.s = RSet(Str(128), 6)
Debug "3.a: " + result + "; length: " + Len(result)

; 3.b. It also provides the following for inserting commas between thousands…
; value.s = “978000”
; result.s = value’R,#6’

;=====  This will require writing your own code To accomplish =========

; 3.c. And we could left-justify with the below, which will place the padding on the end of the output.
; result.s = value’L,#6’
numValue = 25
result.s = LSet(Str(numValue), 6) ;defaults to padding with a space
Debug "3.c: " + result + "; length: " + Len(result)
Output from above:

Code: Select all

1: AbAbAbAbAb
2: VAL3
3.a:      4; length: 6
3.a:     45; length: 6
3.a:    128; length: 6
3.c: 25    ; length: 6
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Essential string handling functions

Post by Oso »

RASHAD wrote: Tue Aug 09, 2022 1:18 pm

Code: Select all

result.s = LSet("A",20,"A")
Great, LSet and RSet give me exactly the padding I need - thanks for that.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Essential string handling functions

Post by Oso »

Demivec wrote: Tue Aug 09, 2022 1:31 pm

Code: Select all

result.s = ReplaceString(Space(5), " ", "Ab")
The above is neat and very easy, I've got it. And LSet and RSet from Rashad's post, they make life much easier in so many instances. Between these two and StringField() I can do everything I need. Thanks for the examples.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Essential string handling functions

Post by Oso »

Demivec wrote: Tue Aug 09, 2022 1:31 pm ; 3.b. It also provides the following for inserting commas between thousands…
; value.s = “978000”
; result.s = value’R,#6’
;===== This will require writing your own code To accomplish =========
This turned out to be straightforward...

Code: Select all

Result$ = RSet( FormatNumber(123320.85, 2 , "." , ",")  ,20," ")
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: Essential string handling functions

Post by AZJIO »

Code: Select all

EnableExplicit

Procedure.s RepeatStrN(str$, n)
	Protected *mem, *pos, i, Text$, Length
	Length = StringByteLength(str$, #PB_Unicode)
	If Length = 0 Or n = 0
		ProcedureReturn ""
	EndIf
	*mem = AllocateMemory(n * Length + 2, #PB_Memory_NoClear)
	If *mem
		*pos = *mem
		For i = 1 To n
			PokeS(*pos, str$, Length, #PB_String_NoZero + #PB_Unicode)
			*pos + Length
		Next
		PokeC(*pos , 0)
		Text$ = PeekS(*mem, -1, #PB_Unicode)
		FreeMemory(*mem)
	EndIf
	ProcedureReturn Text$
EndProcedure

Debug RepeatStrN( "_1_2", 5)

Code: Select all

EnableExplicit

Procedure.s RepeatCharN(a.c, n)
    Protected *mem, Text$
    If a = 0 Or n = 0
        ProcedureReturn ""
    EndIf
    *mem = AllocateMemory((n + 1) * 2)
    If *mem
        FillMemory(*mem , n * 2, a, #PB_Unicode)
        Text$ = PeekS(*mem)
        FreeMemory(*mem)
    EndIf
    ProcedureReturn Text$
EndProcedure

Debug RepeatCharN( 's', 5)
Debug RepeatCharN(65, 8)
Debug RepeatCharN(Asc("h"), 3)
Debug RepeatCharN( '0', 7) + "1"


viewtopic.php?p=564535#p564535
SplitL viewtopic.php?f=12&t=65159&p=486382&hil ... tL#p486382
LTrimChar viewtopic.php?t=79183
IsDigital viewtopic.php?p=583304#p583304
RegexReplace2 viewtopic.php?p=575871
StringBetween viewtopic.php?t=61645
help.chm
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Essential string handling functions

Post by chris319 »

Here is something I came up with for inserting commas into whole values.

It will have to be reworked if you want to have decimal fractions (cents), e.g. 1234.56.

Code: Select all

TR$ = StrD(TR,0)
;TR$= "1000" FOR TESTING

If Len(TR$) > 3: TR$ = InsertString(TR$, ",",Len(TR$) - 2):EndIf
If Len(TR$) > 7: TR$ = InsertString(TR$, ",",Len(TR$) - 6):EndIf
If Len(TR$) > 12: TR$ = InsertString(TR$, ",",Len(TR$) - 11):EndIf
If Len(TR$) > 11: TR$ = InsertString(TR$, ",",Len(TR$) - 10):EndIf
PrintN(" FINAL VALUE: "+"$"+TR$)
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Essential string handling functions

Post by Oso »

chris319 wrote: Thu Jan 26, 2023 6:57 pm Here is something I came up with for inserting commas into whole values.

Code: Select all

If Len(TR$) > 3: TR$ = InsertString(TR$, ",",Len(TR$) - 2):EndIf
If Len(TR$) > 7: TR$ = InsertString(TR$, ",",Len(TR$) - 6):EndIf
If Len(TR$) > 12: TR$ = InsertString(TR$, ",",Len(TR$) - 11):EndIf
This was an old post of mine during my first few days of learning about PB, but thanks for sending a reply @chris319
I now use FormatNumber() for financials...

Result$ = FormatNumber(Number.d [, NbDecimals [, DecimalPoint$ [, ThousandSeparator$]]])
chris319 wrote: Thu Jan 26, 2023 6:57 pm It will have to be reworked if you want to have decimal fractions (cents), e.g. 1234.56.
I think it would need to be reworked for certain currencies :D

Image
Post Reply