简体   繁体   English

VisualWorks-查找字符串中的字符并替换它们

[英]VisualWorks - Find characters in string and replace them

I have one word. 我只有一个字。 At first, instead of letters, there are only ?. 起初,只有字母,而不是字母。 So for example, word 'town' would be shown like this '????'. 因此,例如,单词“ town”将显示为“ ????”。 Then user guesses letter and if he nails it, it changes from ? 然后用户猜到字母,如果他钉了,它从变为? to actual letter. 到实际的字母。 For example, if he guesses t, it would look like this 't???'. 例如,如果他猜到t,它将看起来像是“ t ???”。

The problem is, that I have no idea, how to go through string and divide it to characters. 问题是,我不知道如何遍历字符串并将其划分为字符。 And if I somehow do it, I cannot change it in the new string. 而且,如果我以某种方式执行此操作,则无法在新字符串中进行更改。

Should look somehow like this. 应该看起来像这样。

word do: [ :c | 
          c = guessedChar
                ifTrue[mask := mask, guessedChar]
                ifFalse[mask := mask, '?']
            ].

mask is initialized to nil, because the word length can change and word is String . mask初始化为nil,因为word长度可以更改并且wordString guessedChar is connected to inputField, however it contains only one character at a time. guessedChar连接到inputField,但是一次仅包含一个字符。

And would it be better, do it once for every guessedChar or hold collection of all guessed characters and run it over every time? 会更好吗,对每个guessedChar一次执行一次,还是保留所有猜测的字符的集合并每次运行一次?

A String is a Collection of Character objects. 一个String是一个CollectionCharacter对象。 So you can use the same methods that apply to other collections, too (like #select: , #collect: or #reject: ) 因此,您也可以使用适用于其他集合的相同方法(例如#select:#collect:#reject:

guessedCharacters := 'ts'.
mask := word collect:[:each | (guessedCharacters includes: each)
                                 ifTrue:[each]
                                 ifFalse:[$?]].

Please note that 't' is a String with the Character t. 请注意, 't'是带有Character t的String A Character can be written with $ prefix as literal character $t . 一个Character可以写成$前缀文字字符$t

As String is a subclass of SequenceableCollection you can concatenate two Strings via , . 由于StringSequenceableCollection的子类,因此可以通过,连接两个String。 You cannot however concatenate a String and a Character . 但是,您不能连接StringCharacter

Instead you could use #copyWith: to append a Character to a String . 相反,您可以使用#copyWith:Character追加到String The result is a new String , it wouldn't modify the existing instance. 结果是一个新的String ,它将不会修改现有实例。

You could use 你可以用

word doWithIndex: [:c :i | c = guess ifTrue: [mask at: i put: c]]

which is equivalent to: 等效于:

i := 1.
word do: [:c |
  c = guess ifTrue: [mask at: i put: c].
  i := i + 1]

except that you don't have to initialize and increment i (which is a little bit more error prone, and more verbose) 除了您不必初始化和递增i (这更容易出错和更冗长)外,


Addendum 附录

Given that instances of String cannot grow or change their size, which is immutable, I assume that what might change is the variable word . 鉴于String实例无法增长或更改其大小(这是不可变的),我假设可能发生变化的是变量word In that case you should initialize mask accordingly, so both strings keep always the same length. 在这种情况下,您应该相应地初始化mask ,因此两个字符串始终保持相同的长度。 Like this: 像这样:

 word: aString
   word := aString.
   mask := word copy atAllPut: $?

If you also want to preserve the characters already guessed: 如果您还想保留已经猜到的字符:

 word: aString
   | guessed |
   word := aString.
   guessed := mask reject: [:c | c = $?].
   mask := word copy atAllPut: $?.
   guessed do: [:c | self try: c].

Where #try: is the method we had before 其中#try:是我们以前使用的方法

try: aCharacter
  word doWithIndex: [:c :i | c = aCharacter ifTrue: [mask at: i put: c]]

(you may want to uppercase things if required) (如果需要,您可能需要uppercase

"initially"
actualWord := 'phlebotomy'.
actualMask := actualWord collect: [:ch| $?].

"after each guess"
word := actualWord readStream.
mask := actualMask readStream.
newMask := WriteStream on: String new.
[ word atEnd
] whileFalse:
    [ nextCh := word next = guessedCharcter
                  ifTrue: [mask skip. guessedCharacter]
                  ifFalse: [mask next].
      newMask nextPut: nextCh
    ].
actualMask := newMask contents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM