Page 1 of 1

am I using Stringfield() incorrectly?

Posted: Wed Jul 17, 2024 8:37 pm
by dave@idea-tech.com
Hi, I am parsing some text and want to extract words that are surrounded by spaces.
I am using StringField() to do this, but the number of spaces before the first word and between each word
makes a difference to the output.

I am hoping that someone would please explain what I am not understanding about the Stringfield function.

Thank You!

Here's my test code:

Code: Select all

;Purebasic 6.11 LTS(Windows-x64)

For k = 1 To 6
  Debug "single space between words, k = " + Str(k) + ": " + StringField("Hello I am a splitted string", k, " ")
Next

Debug " "

For k = 1 To 6
  Debug "double space between words, k = " + Str(k) + ": " + StringField("Hello  I  am  a  splitted  string", k, " ")
Next

Debug " "

For k = 1 To 6
  Debug "three spaces between words, k = " + Str(k) + ": " + StringField("Hello   I   am   a   splitted   string", k, " ")
Next

Debug " "

For k = 1 To 6
  Debug "one space before 'Hello', k = " + Str(k) + ": " + StringField(" Hello I am a splitted string", k, " ")
Next

Debug " "

For k = 1 To 6
  Debug "two spaces before 'Hello', k = " + Str(k) + ": " + StringField("  Hello I am a splitted string", k, " ")
Next

Re: am I using Stringfield() incorrectly?

Posted: Wed Jul 17, 2024 8:47 pm
by HeX0R

Code: Select all

a$ = "   Hello    I    am    a    splitted string"

For k = 1 To CountString(a$, " ") + 1
	If StringField(a$, k, " ")
		Debug StringField(a$, k, " ")
	EndIf
Next

Debug "----"

;plan B
a$ = Trim(a$)
While FindString(a$, "  ")
	a$ = ReplaceString(a$, "  ", " ")
Wend
For k = 1 To CountString(a$, " ") + 1
	Debug StringField(a$, k, " ")
Next

Re: am I using Stringfield() incorrectly?

Posted: Wed Jul 17, 2024 8:54 pm
by dave@idea-tech.com
Thank You!

Re: am I using Stringfield() incorrectly?

Posted: Wed Jul 17, 2024 9:15 pm
by AZJIO

Code: Select all

Define String$ = "   Hello    I    am    a    splitted string"
; https://www.purebasic.fr/english/viewtopic.php?p=588707#p588707
String$ = TrimCharsToString(String$, " ", #CR$ + #LF$ + #TAB$ + " ")
Debug String$

Re: am I using Stringfield() incorrectly?

Posted: Thu Jul 18, 2024 12:02 am
by boddhi
With regular expression:

Code: Select all

String.s="This"+Space(3)+"is"+Chr(160)+"example "+Chr(160)+" of    string that contains   spaces and non-breaking spaces"
Debug "String is: "+String
;      
If CreateRegularExpression(0,"[^"+Chr(32)+Chr(160)+"]+")
  If ExamineRegularExpression(0,String)
    While NextRegularExpressionMatch(0)
      Debug "Position: "+RegularExpressionMatchPosition(0)+" "+"String: "+RegularExpressionMatchString(0)
    Wend
  EndIf
  FreeRegularExpression(0)
EndIf
"Special dedication" to AZJIO for the "+", which I mistook for a "*".

Re: am I using Stringfield() incorrectly?

Posted: Thu Jul 18, 2024 12:48 am
by AZJIO