StringField ignore the last field

Just starting out? Need help? Post your questions and find answers here.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

StringField ignore the last field

Post by Saki »

Hi, have you ever noticed that the StringField function ignores the last field ?

Only if you enter 7 instead of 6, the last field will be recognized.

Or do I see this wrong ?

Code: Select all

string$=" Hello I am a splitted string "

 For k = 1 To 6
 Debug StringField ("_Hello_I_am_a_splitted_string_", k , "_")
 Next
地球上の平和
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: StringField ignore the last field

Post by mk-soft »

All right ;)

Code: Select all


string$ = " Hello I am a splitted string "
cnt = CountString(string$, " ")
Debug string$
For k = 1 To cnt
  Debug "" + k + " > [" + StringField(string$, k , " ") + "]"
Next

string$ = "_Hello_I_am_a_splitted_string_"
cnt = CountString(string$, "_")
Debug string$
For k = 1 To cnt
  Debug "" + k + " > [" + StringField(string$, k , "_") + "]"
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: StringField ignore the last field

Post by Saki »

Hi, yes, thank you.
I'm making myself a memory based version right now and was a bit baffled by the behavior.
地球上の平和
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: StringField ignore the last field

Post by NicTheQuick »

That is the usual behaviour in all programming languages I know. Empty fields are allowed.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: StringField ignore the last field

Post by Olli »

I think there are eight fields in these examples. The separator is used to separe, but not to nest, as the brackets.
Example :

Code: Select all

a$="Field1+Field2+Field3"
Sep = CountString(a$, "+")
For I = 1 to Sep +1 ; don't forget to either increase the last step or...
Debug StringField(a$, I, "+")
Next

For I = 0 to Sep ; ... decrease the first step and shift the index below.
Debug StringField(a$, I +1, "+")
Next
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: StringField ignore the last field

Post by Saki »

The manual says that the count starts at 1, just like with the strings.
I must now just look how I do it, since it should already be compatible.

This is simple wrong.

Code: Select all

string$="_aaaaaaaaaa_Hello_I_am_a_splitted_"
Debug StringField(string$, 1 , "_")
地球上の平和
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: StringField ignore the last field

Post by mk-soft »

Olli wrote: Sun May 02, 2021 12:48 pm I think there are eight fields in these examples. The separator is used to separe, but not to nest, as the brackets.
Ups ...

Thats right. There are eight fields
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: StringField ignore the last field

Post by Olli »

@Saki

You can ignore the null fields in a string list, being given that you nest all your fields between two '_'. And add the option to trim, in the way one or any space characters was been inserted.
Just consider the quantity like this example :

Code: Select all

newList F$()

A$ = "_monday_tuesday_wednesday_"
Fmax = CountString(A$, "_") + 1

F = 1
Repeat
 F$ = Trim(StringField(A$, F, "_") )
 If F$
  AddElement(F$() )
  F$() = F$
 EndIf
 F + 1
Until F > Fmax
Here is compatible with all the following ways :

_monday_tuesday_wednesday_
monday_tuesday_wednesday_
_monday_tuesday_wednesday
monday_tuesday_wednesday
_monday__tuesday_wednesday_
monday__tuesday_wednesday_
_monday__tuesday_wednesday
monday__tuesday_wednesday
_monday_tuesday_wednesday_
monday_tuesday _wednesday_
_monday_tuesday_ wednesday
monday_tuesday _ wednesday
monday_ _tuesday_wednesday

Logically, this code will ever return the same list without artefact :

"monday"
"tuesday"
"wednesday"
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: StringField ignore the last field

Post by Saki »

Hi Olli,
thank you very much.
I'll have to take a look at it all at my leisure.

Best Regards Saki
地球上の平和
Post Reply