On 6 Mar 2019, at 12:48, Richard O'Keefe <raoknz@gmail.com> wrote:As someone else already pointed out, the standard way to copypart of any array-like sequence isaSequence copyFrom: firstIncludedIndex to: lastIncludedIndexHow could you have found this by yourself?From the background, eitherClick-select Tools-select Playgroundor hold Ctrl down while typing OW (for Open Workspace).In the new Playground/workspace, typeStringthen Ctrl-B. You now have a five-pane browser open onString.About half way down the third panel on the top you will seeconvertingcopyingdisplayingClick on copying.Guess what? You WON'T see anything that looks relevant!Maybe it's in an ancestral class. So look at the buttonsunder the top row of panes:All packages Scoped view | Flat Hier | Inst side Class side | ...Hier looks promising. Click it.Now in the second pane you will seeProtoObjectObjectCollectionSequenceableCollectionArrayedCollectionString...Select the parent class, ArrayedCollection.Nope, nothing promising there either!Select the grandparent class, SequenceableCollection.And now the 'copying' method category has quite a fewpotentially interesting methods, including #copyFrom:to:.There are four other related methods that I would argueare in the wrong category:allButFirstallButFirst: countallButLastallButLast: countare in the 'accessing' category.If you want something a bit more flexible,you could adddrop: d take: t
"Discard the first |d| elements of the receiver if d >= 0.
Discard the last |d| elements of the receiver if d <= 0.
Return the first |t| elements of the result if t >= 0.
Return the last |t| elements of the result if t <= 0.
The result is the same kind of collection as the receiver."
|lb ub n|
n := self size.
d abs >= n ifTrue: [^self copyEmpty].
d < 0
ifTrue: [lb := 1. ub := n + d]
ifFalse: [lb := d+1. ub := n].
ub - lb + 1 <= t abs ifFalse: [
t < 0
ifTrue: [lb := ub + t + 1]
ifFalse: [ub := lb + t - 1]].
^self copyFrom: lb to: ubNow I would like to suggest that you not use anything like this*directly*. Go back to the Playground, and evaluateString with: (Character codePoint: 256) <Ctrl-P>The answer is '��'. Pharo supports Unicode. Now tryString with: $A with: (Character codePoint: 16r0304)In Pharo, the result looks like A followed by a separatemacron, but when it's pasted into HTML it displayscorrectly as 'A��'. Pharo doesn't *quite* support Unicode.If it did, the two strings with different lengths and nocodepoint in common would display exactly the same.The Unicode standard finds it necessary to distinguishbetween characters, glyphs, graphemes, grapheme clusters,codepoints, and a couple of other things. A SmalltalkString is a sequence of *codepoints*, not a sequence of"characters". There is no upper bound on the number ofcodepoints that may be needed to encode one "character"as the end user sees it, and from there on it gets*complicated*.For over 20 years, it hasn't really made sense to thinkof a string as a simply indexed sequence of characters.Integer indices are a useful implementation-level detailfor remembering bounds from some "higher level" matchingtechnique, but are much less useful than you might expect.On Wed, 6 Mar 2019 at 12:00, Craig Johnson <craig@hivemind.net> wrote:Hi All,
I was trying to figure an elegant way to slice strings (Python style), and came up empty.
What is the simplest way to copy characters between positions 4 and 8 from a string in Pharo?
Craig