Note that there are some explanatory texts on larger screens.

plurals
  1. POAny way to improve this string slice method?
    primarykey
    data
    text
    <p>I wrote this string extension awhile back, and I'm actually getting quite a bit of use out of it.</p> <pre><code>public static string Slice(this string str, int? start = null, int? end = null, int step = 1) { if (step == 0) throw new ArgumentException("Step cannot be zero.", "step"); if (start == null) { if (step &gt; 0) start = 0; else start = str.Length - 1; } else if (start &lt; 0) { if (start &lt; -str.Length) start = 0; else start += str.Length; } else if (start &gt; str.Length) start = str.Length; if (end == null) { if (step &gt; 0) end = str.Length; else end = -1; } else if (end &lt; 0) { if (end &lt; -str.Length) end = 0; else end += str.Length; } else if (end &gt; str.Length) end = str.Length; if (start == end || start &lt; end &amp;&amp; step &lt; 0 || start &gt; end &amp;&amp; step &gt; 0) return ""; if (start &lt; end &amp;&amp; step == 1) return str.Substring((int)start, (int)(end - start)); int length = (int)(((end - start) / (float)step) + 0.5f); var sb = new StringBuilder(length); for (int i = (int)start, j = 0; j &lt; length; i += step, ++j) sb.Append(str[i]); return sb.ToString(); } </code></pre> <p>Since it's in all my projects now, I'm wondering if I could have done it better. More efficient, or would it produce unexpected results in any case?</p> <hr> <p>Slice. It works like Python's array notation.</p> <pre><code> "string"[start:end:step] </code></pre> <p>Many other languages have something like this too. <code>string.Slice(1)</code> is equivalent to <code>string.Substring(1)</code>. <code>string.Substring(1,-1)</code> trims off the first and last character. <code>string.Substring(null,null,-1)</code> will reverse the string. <code>string.Substring(step:2)</code> will return a string with every other character... also similar to <a href="http://www.w3schools.com/jsref/jsref_slice_array.asp" rel="nofollow">JS's slice</a> but with an extra arg.</p> <hr> <p>Re-revised based on your suggestions:</p> <pre><code>public static string Slice(this string str, int? start = null, int? end = null, int step = 1) { if (step == 0) throw new ArgumentException("Step size cannot be zero.", "step"); if (start == null) start = step &gt; 0 ? 0 : str.Length - 1; else if (start &lt; 0) start = start &lt; -str.Length ? 0 : str.Length + start; else if (start &gt; str.Length) start = str.Length; if (end == null) end = step &gt; 0 ? str.Length : -1; else if (end &lt; 0) end = end &lt; -str.Length ? 0 : str.Length + end; else if (end &gt; str.Length) end = str.Length; if (start == end || start &lt; end &amp;&amp; step &lt; 0 || start &gt; end &amp;&amp; step &gt; 0) return ""; if (start &lt; end &amp;&amp; step == 1) return str.Substring(start.Value, end.Value - start.Value); var sb = new StringBuilder((int)Math.Ceiling((end - start).Value / (float)step)); for (int i = start.Value; step &gt; 0 &amp;&amp; i &lt; end || step &lt; 0 &amp;&amp; i &gt; end; i += step) sb.Append(str[i]); return sb.ToString(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload