1

分割関数を作成しました。文中のすべての単語がstring配列の要素として追加されます。

最後の言葉に問題があります。出力配列に追加されていません:

コード:

// 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]);
}

// When used
splits("Hello world!");

-----------------------------------------------


私は解決策を見つけました、そしてそれは非常に簡単
です私は皆が恩恵を受けることを願っています

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());
  }
}

結果は次のようになります(ここでは視覚的な結果

Hello  
world!
4

5 に答える 5

2

ループを終了したらwhile、残りの文字を配列の最後のエントリとして手動で追加する必要があります。これは、文字(スペース)で分割したが、最後の単語の後に追加する単語を示すスペースがないためです。

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

サンプル: http: //ideone.com/81Aw1


ただし、@Matthew指摘したように、実装する必要のある特別な機能がない限り、組み込みの分割関数を使用する必要があります。

"Hello World!".Split(' ');

これは、1行のコードですぐに機能し、.NETFrameworkの開発者と数え切れないほどのユーザーによってすでに徹底的にテストされています。

于 2012-04-09T15:13:15.413 に答える
0
if (str[i] == ' ')

最後の言葉のために発砲することはありません

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

   i++;
}
sent[count] = buff;
于 2012-04-09T15:13:36.500 に答える
0

なぜこのようなものではないのですか?

 private Array SplitSentence(string splitMe)
 {
   if(string.IsNullOrEmpty(splitMe)) return null;
   var splitResult = splitMe.Split(" ".ToArray());
   return splitResult;
 }
于 2012-04-09T15:20:18.917 に答える
0

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]);
}
于 2012-04-09T15:15:48.910 に答える
0

ソリューションを再利用可能にするために、最初にスペースの数を計算しました。また、最後の単語を特別なケースとして処理しました (結果として、スペースを含まない入力でこれが機能します)。これを任意の区切り記号で機能するように適応させるのは簡単なはずです。

  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);
  }
于 2012-04-09T16:25:49.557 に答える