为什么文件路径字符串未拆分

时间:2019-05-10 12:36:31

标签: string split smalltalk gnu-smalltalk

我想在目录中查找文件,然后拆分路径名并将路径的每个部分打印在单独的行上:

(Directory working: '.')
allFilesMatching: '*.st' do: [ :ff | (ff name)
    findTokens: '/'     "Linux separator"
    "splitOn: '/'        -this also does not work"   
    do: [ :i|
        i displayNl ]]

但是它给出了以下错误:

$ gst firstline.st 
"Global garbage collection... done"
Object: '/home/abcd/firstline.st' error: did not understand #findTokens:do:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
String(Object)>>doesNotUnderstand: #findTokens:do: (SysExcept.st:1448)
optimized [] in UndefinedObject>>executeStatements (firstline.st:3)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline.st:2)

错误消息真的很长很复杂!

findTokenssplitOn都不起作用。

问题出在哪里,如何解决。

1 个答案:

答案 0 :(得分:1)

消息可能很长,但该行说明了原因:

Object: '/home/abcd/firstline.st' error: did not understand #findTokens:do

您可能想以不同的方式使用拆分,可能使用subStrings: $character。我只是在GNU Smalltalk Windows版本上尝试过:

命令:

'C:\prg_sdk\GNU Smalltalk(x86)\share\smalltalk\unsupported\torture.st' subStrings: $\

结果:

OrderedCollection ('C:' 'prg_sdk' 'GNU Smalltalk(x86)' 'share' 'smalltalk' 'unsupported' 'torture.st' )

在集合中找到路径的位置。您从头开始或从头开始。

例如,您可以像这样从头开始:

resultPath := nil.
pathCollection := 'C:\prg_sdk\GNU Smalltalk(x86)\share\smalltalk\unsupported\torture.st' subStrings: $\.
pathCollection do: [ :eachPartPath |
     resultPath := (resultPath isNil) ifTrue: [
        eachPartPath
    ] ifFalse: [
        resultPath, '\', eachPartPath
    ].
    resultPath displayNl
]