can I divide a string into part of exactly n length

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

can I divide a string into part of exactly n length

Pharo Smalltalk Users mailing list
Hello,

I wonder if it is possible in Pharo to divide a string in lets say part
of 3.
so this :  'abcdefgh'

would be  'abc def gh`

Regards,

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: can I divide a string into part of exactly n length

Richard Sargent
Administrator
On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

I wonder if it is possible in Pharo to divide a string in lets say part
of 3.
so this :  'abcdefgh'

would be  'abc def gh`

Do you really want a single string with spaces inserted or do you want a collection of substrings, each three characters (or less, for the last one)?



Regards,

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: can I divide a string into part of exactly n length

Pharo Smalltalk Users mailing list
Op 27-4-2020 om 19:05 schreef Richard Sargent:
On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

I wonder if it is possible in Pharo to divide a string in lets say part
of 3.
so this :  'abcdefgh'

would be  'abc def gh`

Do you really want a single string with spaces inserted or do you want a collection of substrings, each three characters (or less, for the last one)?


I need a single string with spaces inserted.
So maybe I have formulate my question wrong. Very difficult if English is not your mother language and you were bad at school in languages a very very long ago.

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: can I divide a string into part of exactly n length

Pharo Smalltalk Users mailing list
This works

| s1 s2 |
s1 := 'abcdefghijk'.
s2 := String streamContents: [:s || in len | 
   in := ReadStream on: s1.
   len := 0.
   [in atEnd] whileFalse: [ 
      s nextPut: in next.
      len := len + 1.
      (in atEnd not and: [ (len \\ 3) = 0]) ifTrue: [ s space ] ] ]

On Apr 27, 2020, at 10:07 AM, Roelof Wobben via Pharo-users <[hidden email]> wrote:


From: Roelof Wobben <[hidden email]>
Subject: Re: [Pharo-users] can I divide a string into part of exactly n length
Date: April 27, 2020 at 10:07:41 AM PDT
To: Richard Sargent <[hidden email]>, Any question about pharo is welcome <[hidden email]>


Op 27-4-2020 om 19:05 schreef Richard Sargent:
On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

I wonder if it is possible in Pharo to divide a string in lets say part
of 3.
so this :  'abcdefgh'

would be  'abc def gh`

Do you really want a single string with spaces inserted or do you want a collection of substrings, each three characters (or less, for the last one)?


I need a single string with spaces inserted.
So maybe I have formulate my question wrong. Very difficult if English is not your mother language and you were bad at school in languages a very very long ago.

Roelof




Reply | Threaded
Open this post in threaded view
|

Re: can I divide a string into part of exactly n length

Richard O'Keefe
|s1 s2|
s1 := 'abcdefghijk'.
s2 := String streamContents: [:s |
  s1 keysAndValuesDo: [:index :each |
    (index \\ 3 = 1 and: [1 < index])
      ifTrue: [s space].
    s nextPut: each]].

is perhaps a little simpler.  I generally prefer to have Smalltalk
do the counting for me.

On Tue, 28 Apr 2020 at 07:25, Todd Blanchard via Pharo-users
<[hidden email]> wrote:

>
> This works
>
> | s1 s2 |
> s1 := 'abcdefghijk'.
> s2 := String streamContents: [:s || in len |
>    in := ReadStream on: s1.
>    len := 0.
>    [in atEnd] whileFalse: [
>       s nextPut: in next.
>       len := len + 1.
>       (in atEnd not and: [ (len \\ 3) = 0]) ifTrue: [ s space ] ] ]
>
> On Apr 27, 2020, at 10:07 AM, Roelof Wobben via Pharo-users <[hidden email]> wrote:
>
>
> From: Roelof Wobben <[hidden email]>
> Subject: Re: [Pharo-users] can I divide a string into part of exactly n length
> Date: April 27, 2020 at 10:07:41 AM PDT
> To: Richard Sargent <[hidden email]>, Any question about pharo is welcome <[hidden email]>
>
>
> Op 27-4-2020 om 19:05 schreef Richard Sargent:
>
> On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <[hidden email]> wrote:
>>
>> Hello,
>>
>> I wonder if it is possible in Pharo to divide a string in lets say part
>> of 3.
>> so this :  'abcdefgh'
>>
>> would be  'abc def gh`
>
>
> Do you really want a single string with spaces inserted or do you want a collection of substrings, each three characters (or less, for the last one)?
>
>
> I need a single string with spaces inserted.
> So maybe I have formulate my question wrong. Very difficult if English is not your mother language and you were bad at school in languages a very very long ago.
>
> Roelof
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: can I divide a string into part of exactly n length

Ben Coman
In reply to this post by Pharo Smalltalk Users mailing list
On Tue, 28 Apr 2020 at 01:08, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Op 27-4-2020 om 19:05 schreef Richard Sargent:
On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

I wonder if it is possible in Pharo to divide a string in lets say part
of 3.
so this :  'abcdefgh'

would be  'abc def gh`

Do you really want a single string with spaces inserted or do you want a collection of substrings, each three characters (or less, for the last one)?


I need a single string with spaces inserted.
So maybe I have formulate my question wrong. Very difficult if English is not your mother language and you were bad at school in languages a very very long ago.

I didn't previously know of a method to do this, but I "guessed" what you needed might be related to "grouping", so I did...
    'abcd' inspect.
then in the Meta tab, starting at Collection, filtered on.... 
     group* 
and worked up the hierarchy discovering #groupsOf:atATimeDo: in SequenceableCollection,
which guesswork found could work like this...
```
input := 'abcdefghijk'.
stream := String streamContents: [:s | input groupsOf: 3 atATimeDo: [ :group | s nextPutAll: group; nextPutAll: ' ']].
stream contents inspect.
```

One of the skills a new programmer needs to develop is to make those educated "guesses" 
and know where/how to test them.  

So I learnt a new method today. thx.
cheers -ben