2017-10-26 2 views
2

はパスカルケースでは、Word本出願は、書かれた文字列を取ることになっているセパレータアプリケーション

Hello how are you?

現在のところ、このコードは各単語が別の文字で表示されている場合にのみ機能します。例文HelloHowAreYouは、hellohow are you?

となっています。どうしてですか?

private void btnSeparate_Click(object sender, EventArgs e) 
    { 
     // Get the sentence from the text box 
     string sentence = txtWords.Text; 
     int upperCase; // to hold the index of an uppercase letter 

     foreach (char up in sentence) 
     { 

      if (char.IsUpper(up)) 
      { 

       // Find the index of the uppercase letter 
       upperCase = sentence.IndexOf(up); 

       // Insert a space at the appropriate index 
       sentence = sentence.Insert(upperCase, " "); 
      } 
     } 
     // Make all the letters lowercase 
     sentence = sentence.ToLower(); 
     // Capitalize the first letter of the sentence. 
     sentence = sentence[1].ToString().ToUpper() + sentence.Substring(2); 

     // Display the separeted words 
     lblSeparatedWords.Text = sentence; 

    } 
} 
+2

'IndexOf'(F9)にブレークポイントを入れて、あなたは文字によって、文字列の文字を反復処理として何が起こるかを参照してください。 – xxbbcc

答えて

1

あなたのために、この動作します:Hがで繰り返されているあなたの例のようにFiddle


Hello how are you 

チェック:

var test = "HelloHowAreYou"; 
var final = ""; 
bool firstCharacterCheckIsDone = false; 
foreach (char c in test) 
{ 
    if (char.IsUpper(c)) 
    { 
     if (test.IndexOf(c) == 0 && !firstCharacterCheckIsDone) 
     { 
      final += " " + c.ToString(); 
      firstCharacterCheckIsDone = true; 
     } 
     else 
      final += " " + c.ToString().ToLower(); 
    } 
    else 
     final += c.ToString(); 
} 

Console.WriteLine(final.Trim()); 

出力Hello & Howあなたが望む出力を得ることができません。 Fiddle

0

Hello how are you? 

チェックあなたの元のバージョンが同じ文字で問題を抱えていた理由は次のとおりです。

あなたは私の上記溶液から方法を行うことができます。

public static void Main() 
{ 
    Console.WriteLine(FinalOutput("HelloHowAreYou?")); 
} 

static string FinalOutput(string test) 
{ 
    var final = ""; 
    bool firstCharacterCheckIsDone = false; 
    foreach (char c in test) 
    { 
     if (char.IsUpper(c)) 
     { 
      if (test.IndexOf(c) == 0 && !firstCharacterCheckIsDone) 
      { 
       final += " " + c.ToString(); 

       //This here will make sure only first character is in Upper case 
       //doesn't matter if the same character is being repeated elsewhere 
       firstCharacterCheckIsDone = true; 
      } 
      else 
       final += " " + c.ToString().ToLower(); 
     } 
     else 
      final += c.ToString(); 
    } 

    return final.Trim(); 
} 

出力IndexOfを使用しました。そのメソッドは文字の最初のインデックスのみを返すので、同じ文字で始まる複数の単語がある場合、バージョンは最初のオカレンスだけを変更します。ここで

は文字列ビルダを使用して、別のオプションです:

string SeparateToWords(string pascalSentence) 
{ 
    if(string.IsNullOrEmpty(pascalSentence)) 
    { 
     return pascalSencentce; 
    } 
    var sb = new StringBuilder(); 
    // note I'm starting from 1 not from 0 here 
    for(var i = 1; i < pascalSentence.Length; i++) 
    { 
     if(char.IsUpper(pascalSentence[i])) 
     { 
      sb.Append(" ").Append(pascalSentence[i].ToString().ToLower()); 
     } 
     else 
     { 
      sb.Append(pascalSentence[i]); 
     } 
    } 
    sb.Insert(0, pascalSentence[0]); 
    return sb.ToString(); 
} 

Live demo or rextester

+0

@o_Oありがとう、固定。 –

関連する問題