From Test-Scratch-Wiki

A 字串 is a series of computer characters, commonly known as letters, numbers, and symbols, that are used in many programming aspects. Strings can be broken apart into words with a fairly easy method. It requires two 变量:

And it requires one 清单:

  • words

Programming

The following script is used for breaking a string apart and separating the words into a list called words.

定義 separate (string) into words
變數 [letter# v] 設為 (1) // this variable will represent an individual letter in "string"
刪除第 [全部 v] 項 \( [words v] \) // clears all the words
新增項目 [] \( [words v] \) // adds a blank item as the basis for the first word
重複 (字串長度\( (string) \)) 次 
  如果 <(字串中第 (letter#) 字\( (string) \)) = [ ]> 那麼 
    新增項目 [] \( [words v] \) // adds a new, blank item to the list if the current letter is a space
  
    替換第 (last v) 項於 [words v] 成 (字串組合 (清單第 (last v) 項項目\( [words v] \) :: list) 和 (字串中第 (letter#) 字\( (string) \))) // adds the letter to the current word
  end // note that a space is inserted into this string, not nothing
  變數 [letter# v] 改變 (1) // moves on to the next letter
end // repeats the amount of letters in the string
Note Note: This same script can be used to separate a String into list items, not just on spaces. All that needs to be changed is instead of checking for a space, check for the separator.

How the Script Works

First off, the variable letter# is set to 1, the list of words cleared, and one item resembling the first word is added to the list. Then, repeating the length of string, the script first checks if the current letter (represented by the variable letter#) is a space. If the letter is a space, it is not wanted to be included in the word, so instead of adding the space to the current word (item number 1) on the list, it creates a new item, representing the next word in the string. If the letter isn't a space, it just adds it to the current word by 组合 the incomplete word and the current letter. Finally, the script moves on to the next letter and repeats the process.

参见

Cookies help us deliver our services. By using our services, you agree to our use of cookies.