<br><br><div class="gmail_quote">On Mon, Feb 2, 2009 at 8:46 AM, Sebastian Nozzi <span dir="ltr">&lt;<a href="mailto:sebnozzi@googlemail.com">sebnozzi@googlemail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello List,<br>
<br>
I&#39;ve been struggling a bit with some basics about Strings for which I<br>
couldn&#39;t find an answer (yet).<br>
<br>
1) How to construct a String from Characters?<br>
<br>
For example, I want to construct a String from the Chatacter literals:<br>
<br>
$H $I<br>
Character cr<br>
$T $H $E $R $E<br>
</blockquote><div><br>If you&#39;re dealing with less than four characters, you could do:<br><br>a := String with: $H with: $I with: Character cr with: $T.<br>&nbsp;<br>Otherwise, I&#39;m not aware of any trivial way to do this.<br>
<br>The first issue is that you need to have all those characters available as a collection somehow (without using a String). My best effort, without adding convenience methods to the String class, is:<br><br>a := #( $H $I $- $T $H $E $R $E ). &quot;Makes an array of literals - I never liked this syntax though&quot;<br>
<br>You have to manually put the &quot;Character cr&quot; in there; I&#39;m not aware of any way to declare it as a literal like that:<br><br>a at: 3 put: Character cr.<br><br>And then you can do Smalltalk magic with it:<br>
<br>b := a inject: &#39;&#39; into: [ :each :sum | each, sum asString]. &quot;Makes 8 copies of a String; inefficient&quot;<br><br>If there were more than just a handful of characters (e.g. writing to a file), then you&#39;d want to use streams instead:<br>
<br>c := WriteStream on: (String new: a size).&nbsp; &quot; It&#39;s important to make a good estimate of size here &quot;<br>c nextPutAll: a.<br>b := c contents.<br><br>c is a stream with a String as a target, so &quot;nextPutAll:&quot; will accept any collection of characters and &quot;nextPut:&quot; will accept any individual character.<br>
<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
2) How to replace a sequence of Characters in a String for others?<br>
<br>
For exaple, I want to replace every &#39;HI&#39; (in this case only one) for<br>
&#39;HELLO&#39; in the String above (not necesarily destructively, getting a<br>
new String is also ok). Is there a quick way to do that?;;<br>
</blockquote><div><br><br>This is something that I don&#39;t know off the top of my head, so I&#39;m going to give you a small insight as to how I work this out.<br><br>First, I bring up the browser and intuitively go to the String class. I have a quick look at the method categories, and see &quot;converting&quot; and &quot;copying&quot; which might be useful. I notice &quot;copyReplaceTokens:with:&quot; which invokes &quot;copyReplaceAll:with:asTokens&quot;.<br>
<br>I highlight the invocation of &quot;copyReplaceAll:with:asTokens&quot; and press alt-m to see which classes implement this. But then a little lightbulb goes off in my head; perhaps &quot;copyReplaceAll:with:&quot; exists. It does - in class SequencableCollection. <br>
<br>I also discover &quot;replaceAll:with&quot; on a hunch as well, but it doesn&#39;t work and doesn&#39;t give any errors. A bug maybe?<br><br>So you can do this:<br><br>d := b copyReplaceAll: &#39;HI&#39; with: &#39;HELLO&#39;.<br>
<br></div></div>Now, I know that there are also more advanced things you can do, such as replacing using regular expressions, but I think that is in a downloadable package somewhere. You&#39;ll need to Google for it.<br><br>
Gulik.<br clear="all"><br>-- <br><a href="http://people.squeakfoundation.org/person/mikevdg">http://people.squeakfoundation.org/person/mikevdg</a><br><a href="http://gulik.pbwiki.com/">http://gulik.pbwiki.com/</a><br>