Escribí esta extensión de cadena un tiempo atrás, y en realidad estoy sacando bastante provecho de ella.¿Alguna forma de mejorar este método de corte de cadena?
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();
}
Dado que ahora está en todos mis proyectos, me pregunto si podría haberlo hecho mejor. ¿Más eficiente, o produciría resultados inesperados en cualquier caso?
Rebanar. Funciona como la notación de matriz de Python.
"string"[start:end:step]
Muchos otros idiomas tienen algo como esto también. string.Slice(1)
es equivalente a string.Substring(1)
. string.Substring(1,-1)
recorta el primer y el último carácter. string.Substring(null,null,-1)
invertirá la cadena. string.Substring(step:2)
devolverá una cadena con cualquier otro carácter ... también similar a JS's slice pero con un argumento adicional.
Re-revisada en función de sus sugerencias:
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();
}
¿Qué se supone que haga? Sé que podría solucionarlo, pero me siento un poco flojo ... – ChrisF
es esa subcadena? – Fredou
¿Me intriga saber para qué lo usa? El paso es intrigante. Entiendo lo que hace, pero ¿cuál es la aplicación práctica? Solo interesado. –