am I using Stringfield() incorrectly?

Just starting out? Need help? Post your questions and find answers here.
dave@idea-tech.com
New User
New User
Posts: 6
Joined: Sat Mar 11, 2023 12:14 am

am I using Stringfield() incorrectly?

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1219
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: am I using Stringfield() incorrectly?

Post 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
dave@idea-tech.com
New User
New User
Posts: 6
Joined: Sat Mar 11, 2023 12:14 am

Re: am I using Stringfield() incorrectly?

Post by dave@idea-tech.com »

Thank You!
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: am I using Stringfield() incorrectly?

Post 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$
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: am I using Stringfield() incorrectly?

Post 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 "*".
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply