Converting an string into Array

It seems so simple, yet it is making me “want” to kill myself after two days of constant battle with it.

I have a TextField, a Module, and a Button
The Module have a string array property with these settings (tell me if it’s the wrong way to define a property as an array):
name = Words()
Type = string
scope = protected

The user is supposed to enter many words inside the TextField, and upon clicking the button, the words are supposed to get stored into myModule.Words

The user is instructed to enter the words as a long single string, and separate the words with “,” without any spaces, like:
word1,word2,word3

But some users might not be careful and enter some spaces here and there, like:
word1, word2 , word3 ,word4 , word5

This is my code for doing it, but it is not working at all:

[code] dim Trimmed() as string
dim shreddedString() as string
dim singleCode as string

// shredding the long string into single characters
shreddedString = Split(TextField.text, “”)

// filtering whitespace and “,” characters
for each char as string in shreddedString
if char <> " " and char <> “,” then
singleCode = singleCode + char

// end of the current word, time to save it
else 
  if singleCode <> "" then  // to avoid saving "" (empty) words, if TextField.Text had ",word1" or ",," , etc
    Trimmed.Append(singleCode)
  end if
end if

next

myModule.Words = Trimmed[/code]

array=SPLIT(string,",")

Yes, I tried this too, without success:

[code] dim Untrimmed() as string

// shredding the long string into single words (might contain whitespaces)
Untrimmed = Split(TextField.text, “,”)

// removing whitespaces
for i as integer in Untrimmed.ubound
Untrimmed(i) = Trim(Untrimmed(i))
end for

// now Untrimmed is actually very Trimmed :slight_smile:
myModule.Words = Untrimmed[/code]

It’s making my head explode
Maybe the array definition as a property under myModule is wrong?

Your last code, as posted, wouldn’t compile, but otherwise, should be fine.

  dim Untrimmed() as string
  
  // shredding the long string into single words (might contain whitespaces)
  Untrimmed = Split(TextField.text, ",")
  
  // removing whitespaces
for i as integer = 0 to Untrimmed.ubound // <— This line changed
        Untrimmed(i) = Trim(Untrimmed(i))
end for


// now Untrimmed is actually very Trimmed :)
myModule.Words = Untrimmed

Save yourself a couple of steps and produce more compact code…

[code]dim Untrimmed() as string = Split(TextField.text, “,”)

// removing whitespaces
for i as integer = 0 to Untrimmed.ubound //
myModule.Words.Append = Trim(Untrimmed(i))
end i[/code]

You seem to be approaching it backwards from my perspective. I’d strip out the spaces first, then split it up.

[code]Dim Words() As String
Dim tempStr As String

tempStr = ReplaceAll(TextField1.Text, " ", “”)
Words = Split(tempStr, “,”)[/code]
Then you can loop the array into the module.

What if the user entered “start time, end time”? That approach would change the content.

[quote=164093:@Roger Clary]Save yourself a couple of steps and produce more compact code…

[code]dim Untrimmed() as string = Split(TextField.text, “,”)

// removing whitespaces
for i as integer = 0 to Untrimmed.ubound //
myModule.Words.Append = Trim(Untrimmed(i))
end i[/code][/quote]
Good tip,
Thanks

[quote=164094:@Dale Arends]You seem to be approaching it backwards from my perspective. I’d strip out the spaces first, then split it up.

[code]Dim Words() As String
Dim tempStr As String

tempStr = ReplaceAll(TextField1.Text, " ", “”)
Words = Split(tempStr, “,”)[/code]
Then you can loop the array into the module.[/quote]
Thanks, I didn’t know anything about Replace method.
Side Question:
How can I get a list of ALL methods for strings, or integers? I looked up string in Xojo help, and there wasn’t any Replace method listed there.

Actually, these are not words, but codes, used for encryption, so it doesn’t matter if a code is “starttime”

Look at the bottom of the page. Each one can be clicked to see the corresponding page :
http://documentation.xojo.com/index.php/String

Replace and ReplaceAll are there.

[quote=164121:@Michel Bujardet]Look at the bottom of the page. Each one can be clicked to see the corresponding page :
http://documentation.xojo.com/index.php/String

Replace and ReplaceAll are there.[/quote]
Thank you! nice,
Looks like Xojo doesn’t have any methods for finding the index of a character (or beginning of a word) within a string, right?

[quote=164122:@Radium Radiovich]Thank you! nice,
Looks like Xojo doesn’t have any methods for finding the index of a character (or beginning of a word) within a string, right?[/quote]
http://documentation.xojo.com/index.php/InStr

pos = s.InStr( "char" )

Also be aware that Replace and ReplaceAll do similar but different things.