Squeak(smalltalk) subSrings 忽略字符串末尾的空字符串 [英] Squeak(smalltalk) subSrings ignores empty strings at end of string

查看:65
本文介绍了Squeak(smalltalk) subSrings 忽略字符串末尾的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试识别字符串末尾的换行符使用子字符串.

I'm trying to identify the newlines at the end of a string using subStrings.

问题:

check := 's
' subStrings: Character cr asString.

Transcript show: check size ; cr.
"prints: 1"
Transcript show: check ; cr.
"prints: #('s')"

我会期望有 2 个字符串,因为我的检查字符串包含一个 cr 字符,并且 subStrings 返回 #('s''')

I would expect to have 2 strings as my check string contains a cr character, and subStrings to return #('s''')

当检查字符串中存在换行符时

while the newlines are present in the check string

check := 's
'.
Transcript show: check size ; cr.
"prints: 2"

推荐答案

在 Squeak 中,有 splitBy:.它在 SequenceableCollection 中定义.
subStringsfindTokens: 搜索任何一组定界符时,splitBy: 将定界符视为要匹配的整个序列.
而且,这样的分隔符序列总是分割一个左右子集合,可能是空的.

In Squeak, there is splitBy:. It is defined in SequenceableCollection.
While subStrings and findTokens: do search for any of a set of delimiters, splitBy: consider the delimiters as a whole sequence to be matched for.
Also, such delimiter sequence always split a left and right sub-collection, possibly empty.

因此您可以使用:

check := 's
' splitBy: String cr.

这篇关于Squeak(smalltalk) subSrings 忽略字符串末尾的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆