Skip to main content

Characters, Strings, and Things

Sebastian Nozzi asked on the beginners list:
I've been struggling a bit with some basics about Strings for which I
couldn't find an answer (yet).

1) How to construct a String from Characters?

For example, I want to construct a String from the Chatacter literals:

$H $I
Character cr
$T $H $E $R $E

2) How to replace a sequence of Characters in a String for others?

For exaple, I want to replace every 'HI' (in this case only one) for
'HELLO' in the String above (not necesarily destructively, getting a
new String is also ok). Is there a quick way to do that?

Thanks in advance!
Smalltalk isn't known for being a scripting language. The runtime and object model are very similar to Ruby, but it is lacking all of the little Perl-isms that we associate with scripting languages.

To deal with the input characters, it is useful to have them in an array. A traditional Smalltalk array with characters would look like this #($a $b $c) but it isn't obvious what to do with the carriage return. For this, the Squeak brace array is handy and also works in Pharo.

characters := {$H.$I.Character cr. $T.$H.$E.$R.$E}.

It isn't terribly portable to other Smalltalks, but it sure is easy to type. Now we've got an array of characters, how to create the new String? Another Squeak-ism is the class method #streamContents:.
It takes a one argument block. The argument is a writeable stream that will return its contents at the end. I have to admit I was thrown by it the first time I saw it.

string := String streamContents: [:writeStream | characters do: [:c | writeStream nextPut: c]].
Like the brace array, it isn't very portable to other Smalltalks, but it is pretty handy.

That leaves us with replacing. I opened the method finder and typed replace into the search box.



That gives us the following complete solution:

| characters string |
characters := {
$H.$I.Character cr. $T.$H.$E.$R.$E}.
string := String streamContents: [:writeStream | characters do: [:c | writeStream nextPut: c]].
string copyReplaceAll: 'HI' with: 'HELLO'

Enjoy!

Comments

Popular posts from this blog

Installing Dart and CoffeeScript

Giving a talk on Monday, June 18 on CoffeeScript and Dart that will mostly be a code-along.

For CoffeeScript, make sure you can run coffee from a command line:

node, download here: http://nodejs.org/npm, now included in nodeCoffeeScript, use the CoffeeScript with Node and npm instructions (basically one line, but...)Some kind of programmer's text editor. I'll be using jEdit, but I'd recommend VIM or TextMate as they both have great CoffeeScript integration.
For Dart, install the Dart editor, which is an IDE with a browser (Dartium).

Dart editor, download here: http://www.dartlang.org/docs/editor/getting-started/Unpack the archive someplace and run the program from there.

Apple Hater Converted? I want an iPad!

I've tweeted several times about my misgivings on the Apple iPad.
Still not getting an iPad (I'm a h8r), but Gruber's got a point against my main beef (the closed platform) http://bit.ly/a0vO6j
Apr 02 2010No scripting/coding on the iPhone was pad enough. But the iPad could have been great 4 kids to learn 2 code. Too bad. http://bit.ly/ayKHYM
Jan 31stif I had an iPad rather than a real computer as a kid, I’d never be a programmer today -- Alex Payne http://al3x.net/2010/01/28/ipad.html
Jan 29thI'd prefer an open platform. I think the changes to section 3.2.2 of the developer agreement are a small step in the right direction, and  I'd like to see more openness. From Boy Genius:
Apple has again tweaked section 3.2.2 of the agreement, adding in a loophole which will allow them to approve certain interpretive code tools. The old section 3.2.2 read like this: No interpreted code may be downloaded or used in an Application except for code that is interpreted and run by Appl…

Pride and Shame: A simple code review format for developing developers

Gave this talk at BarCamp Omaha this September. Thanks to TechOmaha for recording!