Wednesday 7 November 2018

c# - Any way to improve this string slice method?

Answer


I wrote this string extension awhile back, and I'm actually getting quite a bit of use out of it.



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 > 0) start = 0;
else start = str.Length - 1;
}
else if (start < 0)
{

if (start < -str.Length) start = 0;
else start += str.Length;
}
else if (start > str.Length) start = str.Length;

if (end == null)
{
if (step > 0) end = str.Length;
else end = -1;
}

else if (end < 0)
{
if (end < -str.Length) end = 0;
else end += str.Length;
}
else if (end > str.Length) end = str.Length;

if (start == end || start < end && step < 0 || start > end && step > 0) return "";
if (start < end && 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 < length; i += step, ++j)
sb.Append(str[i]);
return sb.ToString();
}


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?







Slice. It works like Python's array notation.



 "string"[start:end:step]


Many other languages have something like this too. string.Slice(1) is equivalent to string.Substring(1). string.Substring(1,-1) trims off the first and last character. string.Substring(null,null,-1) will reverse the string. string.Substring(step:2) will return a string with every other character... also similar to JS's slice but with an extra arg.







Re-revised based on your suggestions:



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 > 0 ? 0 : str.Length - 1;
else if (start < 0) start = start < -str.Length ? 0 : str.Length + start;
else if (start > str.Length) start = str.Length;


if (end == null) end = step > 0 ? str.Length : -1;
else if (end < 0) end = end < -str.Length ? 0 : str.Length + end;
else if (end > str.Length) end = str.Length;

if (start == end || start < end && step < 0 || start > end && step > 0) return "";
if (start < end && 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 > 0 && i < end || step < 0 && i > end; i += step)

sb.Append(str[i]);
return sb.ToString();
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...