<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Strings on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/</link><description>Recent content in Strings on 100 Exercises to Learn Go</description><generator>Hugo</generator><language>en</language><atom:link href="https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/index.xml" rel="self" type="application/rss+xml"/><item><title>Splitting and Joining</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/fields/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/fields/</guid><description>&lt;h1 id="splitting-and-joining"&gt;Splitting and Joining&lt;a class="anchor" href="#splitting-and-joining"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Real input never arrives neatly. Users double-space, paste tabs, leave
trailing newlines — and somewhere inside the mess sit the words you
actually want. This exercise splits mess into words and joins words back
into order, with the standard library doing both directions.&lt;/p&gt;
&lt;h2 id="two-splitters-different-promises"&gt;Two splitters, different promises&lt;a class="anchor" href="#two-splitters-different-promises"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Fields&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34; go is fun &amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// [&amp;#34;go&amp;#34; &amp;#34;is&amp;#34; &amp;#34;fun&amp;#34;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Split&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34; go is fun &amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34; &amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// [&amp;#34;&amp;#34; &amp;#34;&amp;#34; &amp;#34;go&amp;#34; &amp;#34;&amp;#34; &amp;#34;is&amp;#34; &amp;#34;fun&amp;#34; &amp;#34;&amp;#34;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Same input, wildly different output. &lt;code&gt;strings.Fields&lt;/code&gt; splits on &lt;em&gt;runs&lt;/em&gt; of
whitespace — spaces, tabs, newlines, any length — and drops the empties.
&lt;code&gt;strings.Split&lt;/code&gt; cuts on one &lt;em&gt;exact&lt;/em&gt; separator and keeps every piece,
including the empty strings between adjacent separators.&lt;/p&gt;</description></item><item><title>Trimming</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/trim/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/trim/</guid><description>&lt;h1 id="trimming"&gt;Trimming&lt;a class="anchor" href="#trimming"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Dirt collects at the edges. Pasted input trails newlines, filenames carry
extensions, config values arrive padded — and the interesting part sits
untouched in the middle. Trimming is the art of naming exactly which edges
to cut, and reaching past the right tool ruins the middle along with them.&lt;/p&gt;
&lt;h2 id="three-cutters-three-promises"&gt;Three cutters, three promises&lt;a class="anchor" href="#three-cutters-three-promises"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;TrimSpace&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34; hi\n&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// &amp;#34;hi&amp;#34; — all surrounding whitespace, gone&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;TrimSuffix&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;notes.txt&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;.txt&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// &amp;#34;notes&amp;#34; — one exact piece, if present&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Trim&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;...hi...&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;.&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// &amp;#34;hi&amp;#34; — any of the cutset chars, repeatedly&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;TrimSpace&lt;/code&gt; is the blunt instrument you&amp;rsquo;ll use most: every Unicode
whitespace character, both ends, no questions. &lt;code&gt;TrimSuffix&lt;/code&gt; is the scalpel:
it removes one &lt;em&gt;exact&lt;/em&gt; trailing literal, and only if it&amp;rsquo;s actually there —
&lt;code&gt;&amp;quot;notes.md&amp;quot;&lt;/code&gt; passes through untouched, no error, no partial cut.&lt;/p&gt;</description></item><item><title>Substring Checks</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/contains/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/contains/</guid><description>&lt;h1 id="substring-checks"&gt;Substring Checks&lt;a class="anchor" href="#substring-checks"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Most string questions are yes-or-no: does it contain this, start with
that, end with the other? The &lt;code&gt;strings&lt;/code&gt; package answers each with a
function named like the question itself — and this exercise asks the
simplest one through a deliberately naive email check.&lt;/p&gt;
&lt;h2 id="questions-not-answers"&gt;Questions, not answers&lt;a class="anchor" href="#questions-not-answers"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Contains&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;a@b.co&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;@&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// true — piece anywhere inside&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;HasPrefix&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;go.mod&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;go&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// true — piece at the start&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;HasSuffix&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;notes.txt&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;.txt&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// true — piece at the end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Contains&lt;/code&gt;, &lt;code&gt;HasPrefix&lt;/code&gt;, &lt;code&gt;HasSuffix&lt;/code&gt;: the call reads as the question,
which means call sites rarely need comments. When the check grows teeth —
case-insensitivity (&lt;code&gt;EqualFold&lt;/code&gt;), counting occurrences (&lt;code&gt;Count&lt;/code&gt;), locating
positions (&lt;code&gt;Index&lt;/code&gt;) — the same package holds them, same naming rhythm.&lt;/p&gt;</description></item><item><title>Replacement</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/replace/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/replace/</guid><description>&lt;h1 id="replacement"&gt;Replacement&lt;a class="anchor" href="#replacement"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Scrubbing a word from text sounds like deletion. It&amp;rsquo;s actually
&lt;em&gt;substitution&lt;/em&gt;: every occurrence swapped for something safer, the rest of
the string untouched. And the emphasis falls on &lt;em&gt;every&lt;/em&gt; — replacing only
the first match is the classic almost-fix, passing the eyeball test while
leaking secrets further down the page.&lt;/p&gt;
&lt;h2 id="all-of-them-not-the-first"&gt;All of them, not the first&lt;a class="anchor" href="#all-of-them-not-the-first"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;strings&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;ReplaceAll&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;my secret here&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;secret&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;[redacted]&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// &amp;#34;my [redacted] here&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;strings.ReplaceAll&lt;/code&gt; rewrites every match. Its sibling &lt;code&gt;Replace&lt;/code&gt; takes a
count — &lt;code&gt;Replace(s, old, new, 1)&lt;/code&gt; swaps only the first hit, negative means
all (making it &lt;code&gt;ReplaceAll&lt;/code&gt;&amp;rsquo;s verbose twin). When you truly want one
replacement, say &lt;code&gt;1&lt;/code&gt; explicitly; reaching for &lt;code&gt;ReplaceAll&lt;/code&gt; by default keeps
the common case — scrub everything — impossible to under-apply.&lt;/p&gt;</description></item><item><title>Building Strings Efficiently</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/builder/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/builder/</guid><description>&lt;h1 id="building-strings-efficiently"&gt;Building Strings Efficiently&lt;a class="anchor" href="#building-strings-efficiently"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Immutability has a price, and loops collect it. Each &lt;code&gt;+=&lt;/code&gt; builds a brand
new string and copies everything so far — one concatenation is invisible,
ten thousand is a quadratic swamp. This exercise repeats a string &lt;code&gt;n&lt;/code&gt;
times, which is exactly the shape where naive building collapses.&lt;/p&gt;
&lt;h2 id="counting-the-copies"&gt;Counting the copies&lt;a class="anchor" href="#counting-the-copies"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;out&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt; &amp;lt; &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt;; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt;&lt;span style="color:#f92672"&gt;++&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;out&lt;/span&gt; &lt;span style="color:#f92672"&gt;+=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;s&lt;/span&gt; &lt;span style="color:#75715e"&gt;// copy #i moves i*len(s) bytes — total O(n²)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Every iteration copies the entire accumulated result plus the new piece.
For &lt;code&gt;ConcatN(&amp;quot;ab&amp;quot;, 3)&lt;/code&gt; nobody cares; for a thousand fragments in a request
handler, the profiler lights up. The cost hides because each line looks
cheap — the quadratic behavior is emergent, visible only in aggregate.
Whenever a loop grows a string, hear a small alarm.&lt;/p&gt;</description></item><item><title>String/Number Conversion</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/strconv/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/09_strings/strconv/</guid><description>&lt;h1 id="stringnumber-conversion"&gt;String/Number Conversion&lt;a class="anchor" href="#stringnumber-conversion"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Programs live on a border: humans speak text, machines compute numbers,
and every input field plus every display line is a crossing. This exercise
works the checkpoint in both directions — parse what arrived, add it, and
report honestly when the text isn&amp;rsquo;t a number at all.&lt;/p&gt;
&lt;h2 id="crossing-with-a-receipt"&gt;Crossing with a receipt&lt;a class="anchor" href="#crossing-with-a-receipt"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;strconv&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;strconv&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Atoi&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;42&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// 42, nil — the crossing succeeded&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;_&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; = &lt;span style="color:#a6e22e"&gt;strconv&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Atoi&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;x&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// 0, err — junk gets a receipt, not a panic&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;strconv.Atoi&lt;/code&gt; (ASCII-to-integer) returns the value &lt;em&gt;and&lt;/em&gt; a verdict,
paired returns doing their eternal duty. Valid text converts; junk yields
zero plus a non-nil error describing the failure. The zero is meaningless
by design — callers check &lt;code&gt;err&lt;/code&gt; before touching &lt;code&gt;n&lt;/code&gt;, the unforgivable-sin
rule from the error-assertion lesson, now at a border crossing.&lt;/p&gt;</description></item></channel></rss>