为什么文件路径字符串不拆分
Why file path string is not splitting
我想在目录中查找文件,然后拆分路径名并将路径的每一部分打印在单独的行上:
(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)
错误信息真的又长又复杂!
findTokens
和 splitOn
均无效。
问题出在哪里,如何解决。
消息可能很长,但行中说明了原因:
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' )
当你在 collection 中找到你的路径时,你从哪里得到它。你可以从头开始,也可以从尾开始。
例如,您可以这样从头开始:
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
]
我想在目录中查找文件,然后拆分路径名并将路径的每一部分打印在单独的行上:
(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)
错误信息真的又长又复杂!
findTokens
和 splitOn
均无效。
问题出在哪里,如何解决。
消息可能很长,但行中说明了原因:
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' )
当你在 collection 中找到你的路径时,你从哪里得到它。你可以从头开始,也可以从尾开始。
例如,您可以这样从头开始:
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
]