snipt

Ctrl+h for KB shortcuts

C

strdup

1
2
3
4
5
6
char *strdup (const char *s) {
    char *d = malloc (strlen (s) + 1);   // Space for length plus nul
    if (d == NULL) return NULL;          // No memory
    strcpy (d,s);                        // Copy the characters
    return d;                            // Return the new string
}
https://snipt.net/embed/98bd3d6e4f796d17643b72f40a95bfc6/
/raw/98bd3d6e4f796d17643b72f40a95bfc6/
98bd3d6e4f796d17643b72f40a95bfc6
c
C
6
2019-08-11T16:05:43
True
False
False
Jul 21, 2019 at 02:59 AM
/api/public/snipt/180944/
strdup
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kt">char</span> <span class="o">*</span><span class="nf">strdup</span> <span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kt">char</span> <span class="o">*</span><span class="n">d</span> <span class="o">=</span> <span class="n">malloc</span> <span class="p">(</span><span class="n">strlen</span> <span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">// Space for length plus nul</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span> <span class="p">(</span><span class="n">d</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="c1">// No memory</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">strcpy</span> <span class="p">(</span><span class="n">d</span><span class="p">,</span><span class="n">s</span><span class="p">);</span> <span class="c1">// Copy the characters</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">return</span> <span class="n">d</span><span class="p">;</span> <span class="c1">// Return the new string</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
String

Java

How to replace “\” with “\\” in java

1
2
3
4
5
6
// Don't use String.replaceAll in this case - that's specified in terms of regular expressions,
// which means you'd need even more escaping. This should be fine:
String escaped = original.replace("\\", "\\\\");

// You could use replaceAll:
String escaped = original.replaceAll("\\\\", "\\\\\\\\");
https://snipt.net/embed/8f55002405ba79b4dc5d4fe8294ba615/
/raw/8f55002405ba79b4dc5d4fe8294ba615/
8f55002405ba79b4dc5d4fe8294ba615
java
Java
6
2019-07-28T18:34:20
True
False
False
Mar 01, 2016 at 02:23 AM
/api/public/snipt/144510/
how-to-replace-with-in-java
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// Don&#39;t use String.replaceAll in this case - that&#39;s specified in terms of regular expressions,</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// which means you&#39;d need even more escaping. This should be fine:</span> </span><span id="L-3"><a name="L-3"></a><span class="n">String</span> <span class="n">escaped</span> <span class="o">=</span> <span class="n">original</span><span class="o">.</span><span class="na">replace</span><span class="o">(</span><span class="s">&quot;\\&quot;</span><span class="o">,</span> <span class="s">&quot;\\\\&quot;</span><span class="o">);</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="c1">// You could use replaceAll:</span> </span><span id="L-6"><a name="L-6"></a><span class="n">String</span> <span class="n">escaped</span> <span class="o">=</span> <span class="n">original</span><span class="o">.</span><span class="na">replaceAll</span><span class="o">(</span><span class="s">&quot;\\\\&quot;</span><span class="o">,</span> <span class="s">&quot;\\\\\\\\&quot;</span><span class="o">);</span> </span></pre></div> </td></tr></table>
String, replace

C#

C#, Create Delimited string from Array

string.Join(",", TargetList.ToArray());
https://snipt.net/embed/b82e8c432589c5ede6a3e57deba684e9/
/raw/b82e8c432589c5ede6a3e57deba684e9/
b82e8c432589c5ede6a3e57deba684e9
csharp
C#
1
2019-08-13T19:44:36
True
False
False
/api/public/snipt/53988/
c-create-delimited-string-from-array
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kt">string</span><span class="p">.</span><span class="n">Join</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">,</span> <span class="n">TargetList</span><span class="p">.</span><span class="n">ToArray</span><span class="p">());</span> </span></pre></div> </td></tr></table>
C#, String