C# 模拟字符串拆分函数

C# 模拟字符串拆分函数,c#,function,split,visual-c#-express-2010,C#,Function,Split,Visual C# Express 2010,我创建了一个拆分函数:句子中的每个单词都作为字符串数组中的一个元素添加 我对最后一句话有意见;未将其添加到输出数组: 代码: // the funcion static void splits(string str) { int i=0; int count=0; string[] sent = new string[2]; string buff= ""; while (i < str.Length) { if (str[i] == ' '

我创建了一个拆分函数:句子中的每个单词都作为
字符串
数组中的一个元素添加

我对最后一句话有意见;未将其添加到输出数组:

代码:

// the funcion
static void splits(string str)
{
   int i=0;
   int count=0;
   string[] sent = new string[2];
   string buff= "";
   while (i < str.Length)
   {
      if (str[i] == ' ')
      {
         sent[count] = buff;
         count++;
         buff = "";
      }
      buff += str[i];
      i++;
   }
     Console.WriteLine(sent[0]);
}
-----------------------------------------------
我找到了解决方案,非常简单
我希望每个人都能从中受益

static void splits(string str)
{
  int i = 0;
  int count = 0;
  string[] sent = new string[2];
  string buff = "";
  str += " ";         // the solution here, add space after last word
  while (i < str.Length)
  {
    if (str[i] == ' ')
    {
       sent[count] = buff;
       count++;
       buff = "";
    }
    buff += str[i];
    i++;
  }

  for (int z = 0; z < sent.Length; z++)
  {
     Console.WriteLine(sent[z].Trim());
  }
}

退出
while
循环后,需要手动添加剩余字符作为数组中的最终条目。这是因为您拆分了字符
(空格),但最后一个单词后面没有空格来表示要添加的单词:

...
}
if (!string.IsNullOrEmpty(buff))
    sent[count++] = buff;
样本:


但是,正如
@Matthew
所指出的,除非您需要实现某些特殊功能,否则您应该只使用内置的拆分功能:

"Hello World!".Split(' ');
这是一行开箱即用的代码,已经由.NET Framework的开发人员和无数用户进行了详尽的测试

if (str[i] == ' ')
永远不会为最后一句话而开火

while (i < str.Length)
{
   if (str[i] == ' ')
   {
      sent[count] = buff;
      count++;
      buff = "";
   }
   else
   {
       buff += str[i];
   }

   i++;
}
sent[count] = buff;
while(i
完成while循环后,您需要检查缓冲区,以确定是否还有最后一个字。此外,您只打印第一个元素

// the funcion
static void splits(string str)
{
   int i=0;
   int count=0;
   string[] sent = new string[2];
   string buff= "";
   while (i < str.Length)
   {
      if (str[i] == ' ')
      {
         sent[count] = buff;
         count++;
         buff = "";
      }
      buff += str[i];
      i++;
   }
   if (buff.Length > 0) {
       sent[count] = buff;
       count++;
   }

   for (i = 0; i < count; i++)
       Console.WriteLine(sent[i]);
}
//函数
静态空分(字符串str)
{
int i=0;
整数计数=0;
字符串[]已发送=新字符串[2];
字符串buff=“”;
而(i0){
发送[计数]=增益;
计数++;
}
对于(i=0;i
为什么不做这样的事情

 private Array SplitSentence(string splitMe)
 {
   if(string.IsNullOrEmpty(splitMe)) return null;
   var splitResult = splitMe.Split(" ".ToArray());
   return splitResult;
 }

为了使解决方案可重用,我首先计算了空间的数量;我还将最后一个单词作为一个特例处理(因此,它可以在不包含空格的输入中工作)。这应该很容易适应任何分隔符

  string s = "Test spaces in a sentence :)";
  int numSpaces = 1;
  foreach (char c in s)
  {
    if (c == ' ')
    {
      ++numSpaces;
    }
  }
  string[] output = new string[numSpaces];
  int spaceIndex = s.IndexOf(' ');
  int index = 0;
  int startIndex = 0;
  --numSpaces;
  while (index < numSpaces)
  {
    output[index] = s.Substring(startIndex, spaceIndex - startIndex);
    startIndex = spaceIndex + 1;
    spaceIndex = s.IndexOf(' ', startIndex);
    ++index;
  }
  output[index] = s.Substring(startIndex);
  foreach (string str in output)
  {
    Console.WriteLine(str);
  }
string s=“测试句子中的空格:)”;
int numSpaces=1;
foreach(字符c在s中)
{
如果(c='')
{
++numSpaces;
}
}
字符串[]输出=新字符串[numSpaces];
int spaceIndex=s.IndexOf(“”);
int指数=0;
int startIndex=0;
--numSpaces;
while(索引
string[]pieces=“Hello World!”.Split(“”)我很想看到性能比较…@Matthew:我知道c#中的(分割函数),但我想模拟这是一个学术练习,或者这背后有什么目的?@mellamokb:不是学术练习,但我想创建函数而不依赖内置函数来提高我的技能谢谢,我知道,但是我不想使用内置的split函数,我想模拟它,如果源字符串中有3个以上的空格呢?这个答案和OP一样,只使用3个索引初始化发送的数组。如果有3个以上的空格,则会引发异常。@ChrisDunaway True,我没有notice@ChrisDunaway:不幸的是,出现了一个错误,不能很好地工作。谢谢,但是正如我前面所说的,我不想使用内置的split函数。我想模拟一下
 private Array SplitSentence(string splitMe)
 {
   if(string.IsNullOrEmpty(splitMe)) return null;
   var splitResult = splitMe.Split(" ".ToArray());
   return splitResult;
 }
  string s = "Test spaces in a sentence :)";
  int numSpaces = 1;
  foreach (char c in s)
  {
    if (c == ' ')
    {
      ++numSpaces;
    }
  }
  string[] output = new string[numSpaces];
  int spaceIndex = s.IndexOf(' ');
  int index = 0;
  int startIndex = 0;
  --numSpaces;
  while (index < numSpaces)
  {
    output[index] = s.Substring(startIndex, spaceIndex - startIndex);
    startIndex = spaceIndex + 1;
    spaceIndex = s.IndexOf(' ', startIndex);
    ++index;
  }
  output[index] = s.Substring(startIndex);
  foreach (string str in output)
  {
    Console.WriteLine(str);
  }