弦按长度分开,仅由最近的空间拆分[英] String Split by Length and split only by nearest space

本文是小编为大家收集整理的关于弦按长度分开,仅由最近的空间拆分的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个像

这样的文字
var data = "âô¢¬ôè÷¢ : ªîø¢è¤ô¢ - ã¿ñ¬ô ñèù¢ ªð¼ñ£÷¢ ï¤ôñ¢,«ñø¢è¤ô¢ - ªð¼ñ£÷¢ ñèù¢ ÝÁºèñ¢ ï¤ô袰ñ¢ ñ¤ì¢ì£ Üò¢òñ¢ ªð¼ñ£ñ¢ð좮 è¤ó£ñ âô¢¬ô袰ñ¢,õìè¢è¤ô¢ - ÝÁºèñ¢ ï¤ôñ¢,è¤öè¢è¤ô¢ - ô좲ñ¤ ï¤ôñ¢ ñø¢Áñ¢ 1,22 ªê ï¤ôñ¢ ð£î¢î¤òñ¢";

,我有扩展方法拆分字符串

public static IEnumerable<string> EnumByLength(this string s, int length)
{
    for (int i = 0; i < s.Length; i += length)
    {
        if (i + length <= s.Length)
        {
            yield return s.Substring(i, length);
        }
        else
        {
            yield return s.Substring(i);
        }
    }
}
public static string[] SplitByLength(this string s, int maxLen)
{
    var v = EnumByLength(s, maxLen);
    if (v == null)
        return new string[] { s };
    else
        return s.EnumByLength(maxLen).ToArray();
}

现在我的问题是

要将此字符串拆分为最大长度150,并且必须仅由其中的最近空间进行./p>

如何?

推荐答案

我的版本:

// Enumerate by nearest space
// Split String value by closest to length spaces
// e.g. for length = 3 
// "abcd efghihjkl m n p qrstsf" -> "abcd", "efghihjkl", "m n", "p", "qrstsf" 
public static IEnumerable<String> EnumByNearestSpace(this String value, int length) {
  if (String.IsNullOrEmpty(value))
    yield break;

  int bestDelta = int.MaxValue;
  int bestSplit = -1;

  int from = 0;

  for (int i = 0; i < value.Length; ++i) {
    var Ch = value[i];

    if (Ch != ' ')
      continue;

    int size = (i - from);
    int delta = (size - length > 0) ? size - length : length - size;

    if ((bestSplit < 0) || (delta < bestDelta)) {
      bestSplit = i;
      bestDelta = delta;
    }
    else {
      yield return value.Substring(from, bestSplit - from);

      i = bestSplit;

      from = i + 1;
      bestSplit = -1;
      bestDelta = int.MaxValue;
    }
  }

  // String's tail
  if (from < value.Length) {
    if (bestSplit >= 0) {
      if (bestDelta < value.Length - from)
        yield return value.Substring(from, bestSplit - from);

      from = bestSplit + 1;
    }

    if (from < value.Length)
      yield return value.Substring(from);
  }
}

...

var list = data.EnumByNearestSpace(150).ToList();

其他推荐答案

我的版本

var data = "âô¢¬ôè÷¢ : ªîø¢è¤ô¢ - ã¿ñ¬ô ñèù¢ ªð¼ñ£÷¢ ï¤ôñ¢,«ñø¢è¤ô¢ - ªð¼ñ£÷¢ ñèù¢ ÝÁºèñ¢ ï¤ô袰ñ¢ ñ¤ì¢ì£ Üò¢òñ¢ ªð¼ñ£ñ¢ð좮 è¤ó£ñ âô¢¬ô袰ñ¢,õìè¢è¤ô¢ - ÝÁºèñ¢ ï¤ôñ¢,è¤öè¢è¤ô¢ - ô좲ñ¤ ï¤ôñ¢ ñø¢Áñ¢ 1,22 ªê ï¤ôñ¢ ð£î¢î¤òñ¢";

var indexes = new List<int>();
var lastFoundIndex = 0;
while((lastFoundIndex = data.IndexOf(' ', lastFoundIndex + 1)) != -1)
{
    indexes.Add(lastFoundIndex);
}

int intNum = 150;
int index;
var newList = new List<string>();
while ((index = indexes.Where(x => x > intNum - 150 &&  x <= intNum).LastOrDefault()) != 0)
{
    var firstIndex = newList.Count == 0 ? 0 : index;
    var lastIndex = firstIndex + 150 >= data.Length ? data.Length - 150 : intNum;
    newList.Add(data.Substring(intNum - 150, lastIndex));
    intNum += 150;
}

newList包含拆分字符串

其他推荐答案

您去那里:

 for (int i = 0; i < s.Length; i += length)
    {
        int index=s.IndexOf(" ",i, s.Length-i)

        if (index!=-1 && index + length <= s.Length)
        {
            i =index;           
            yield return s.Substring(index, length);
        }
        else
        {
            index= s.LastIndexOf(" ", 0, i);
            if(index==-1)
                yield return s.Substring(i);
            else
            {
                i = index;
                yield return s.Substring(i);
            }
        }
    }

本文地址:https://www.itbaoku.cn/post/1557050.html

问题描述

I am having a Text Like

var data = "âô¢¬ôè÷¢ : ªîø¢è¤ô¢ - ã¿ñ¬ô ñèù¢ ªð¼ñ£÷¢ ï¤ôñ¢,«ñø¢è¤ô¢ - ªð¼ñ£÷¢ ñèù¢ ÝÁºèñ¢ ï¤ô袰ñ¢ ñ¤ì¢ì£ Üò¢òñ¢ ªð¼ñ£ñ¢ð좮 è¤ó£ñ âô¢¬ô袰ñ¢,õìè¢è¤ô¢ - ÝÁºèñ¢ ï¤ôñ¢,è¤öè¢è¤ô¢ - ô좲ñ¤ ï¤ôñ¢ ñø¢Áñ¢ 1,22 ªê ï¤ôñ¢ ð£î¢î¤òñ¢";

and I am Having the Extension Method to split string

public static IEnumerable<string> EnumByLength(this string s, int length)
{
    for (int i = 0; i < s.Length; i += length)
    {
        if (i + length <= s.Length)
        {
            yield return s.Substring(i, length);
        }
        else
        {
            yield return s.Substring(i);
        }
    }
}
public static string[] SplitByLength(this string s, int maxLen)
{
    var v = EnumByLength(s, maxLen);
    if (v == null)
        return new string[] { s };
    else
        return s.EnumByLength(maxLen).ToArray();
}

Now my question is

To split this string by Maximum Length 150 and the splitting must be done only by the Nearest Spaces in it..(either before 150 or after 150.. not in the middle of a word.

How?

推荐答案

My version:

// Enumerate by nearest space
// Split String value by closest to length spaces
// e.g. for length = 3 
// "abcd efghihjkl m n p qrstsf" -> "abcd", "efghihjkl", "m n", "p", "qrstsf" 
public static IEnumerable<String> EnumByNearestSpace(this String value, int length) {
  if (String.IsNullOrEmpty(value))
    yield break;

  int bestDelta = int.MaxValue;
  int bestSplit = -1;

  int from = 0;

  for (int i = 0; i < value.Length; ++i) {
    var Ch = value[i];

    if (Ch != ' ')
      continue;

    int size = (i - from);
    int delta = (size - length > 0) ? size - length : length - size;

    if ((bestSplit < 0) || (delta < bestDelta)) {
      bestSplit = i;
      bestDelta = delta;
    }
    else {
      yield return value.Substring(from, bestSplit - from);

      i = bestSplit;

      from = i + 1;
      bestSplit = -1;
      bestDelta = int.MaxValue;
    }
  }

  // String's tail
  if (from < value.Length) {
    if (bestSplit >= 0) {
      if (bestDelta < value.Length - from)
        yield return value.Substring(from, bestSplit - from);

      from = bestSplit + 1;
    }

    if (from < value.Length)
      yield return value.Substring(from);
  }
}

...

var list = data.EnumByNearestSpace(150).ToList();

其他推荐答案

My version

var data = "âô¢¬ôè÷¢ : ªîø¢è¤ô¢ - ã¿ñ¬ô ñèù¢ ªð¼ñ£÷¢ ï¤ôñ¢,«ñø¢è¤ô¢ - ªð¼ñ£÷¢ ñèù¢ ÝÁºèñ¢ ï¤ô袰ñ¢ ñ¤ì¢ì£ Üò¢òñ¢ ªð¼ñ£ñ¢ð좮 è¤ó£ñ âô¢¬ô袰ñ¢,õìè¢è¤ô¢ - ÝÁºèñ¢ ï¤ôñ¢,è¤öè¢è¤ô¢ - ô좲ñ¤ ï¤ôñ¢ ñø¢Áñ¢ 1,22 ªê ï¤ôñ¢ ð£î¢î¤òñ¢";

var indexes = new List<int>();
var lastFoundIndex = 0;
while((lastFoundIndex = data.IndexOf(' ', lastFoundIndex + 1)) != -1)
{
    indexes.Add(lastFoundIndex);
}

int intNum = 150;
int index;
var newList = new List<string>();
while ((index = indexes.Where(x => x > intNum - 150 &&  x <= intNum).LastOrDefault()) != 0)
{
    var firstIndex = newList.Count == 0 ? 0 : index;
    var lastIndex = firstIndex + 150 >= data.Length ? data.Length - 150 : intNum;
    newList.Add(data.Substring(intNum - 150, lastIndex));
    intNum += 150;
}

newList contains the split string

其他推荐答案

There you go:

 for (int i = 0; i < s.Length; i += length)
    {
        int index=s.IndexOf(" ",i, s.Length-i)

        if (index!=-1 && index + length <= s.Length)
        {
            i =index;           
            yield return s.Substring(index, length);
        }
        else
        {
            index= s.LastIndexOf(" ", 0, i);
            if(index==-1)
                yield return s.Substring(i);
            else
            {
                i = index;
                yield return s.Substring(i);
            }
        }
    }