Я написал это расширение строки некоторое время назад, и я на самом деле получаю от этого много пользы.Любой способ улучшить этот метод среза строки?
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();
}
Поскольку это сейчас во всех моих проектах, мне интересно, смогу ли я сделать это лучше. Более эффективны или приведут к неожиданным результатам в любом случае?
Кусочек. Он работает как нотация массива Python.
"string"[start:end:step]
Многие другие языки имеют что-то подобное. string.Slice(1)
эквивалентен string.Substring(1)
. string.Substring(1,-1)
обрезает первый и последний символ. string.Substring(null,null,-1)
изменит строку. string.Substring(step:2)
вернет строку с любым другим символом ... также похож на JS's slice, но с дополнительным аргументом.
Заново пересматриваются на основе ваших предложений:
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();
}
Что он должен делать? Я знаю, что смогу это исправить, но я чувствую себя немного ленивым ... – ChrisF 2010-11-24 19:54:46
- это подстрока !? – Fredou 2010-11-24 19:56:03
Я заинтригован, чтобы знать, для чего вы его используете? Шаг бит интригует. Я понимаю, что он делает, но какое практическое применение. Просто интересно. – 2010-11-24 19:56:15