samedi 25 avril 2015

Title case in C# using split()


I am writing a simple program that lets the user input a sentence and it gets converted to title case. The first letter in each word is made capital. When it runs I don't get any errors, but it doesn't convert. Can someone let me know what I am missing? Thanks in advance!

This is my .cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
 {
    if (Page.IsPostBack)
    {
        Page.Validate();
        if (!Page.IsValid)
        {
            string sentence = phrase.Text;
            String[] sentenceArray = sentence.Split(' ');
            for (int i = 0; i < sentenceArray.Length; i++)
            {
                sentenceArray[i] = sentenceArray[i].Substring(0,      1).ToUpper() + sentenceArray[i].Substring(1).ToLower();
            }
            phrase.Text = String.Join(" ", sentenceArray);
        }
    }
  }
  }

If needed here is the HTML code:

<html xmlns="http://ift.tt/lH0Osb">
<head runat="server">
    <title>Convert to Title Case</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>
        Enter a phrase and click the Title Case button.</p>
        <p>
            <asp:TextBox ID="phrase" runat="server" /> <asp:RequiredFieldValidator ID="letterValidator"
                runat="server" ErrorMessage="Required field" ControlToValidate="phrase" />
        </p>
        <p>
            <asp:HiddenField ID="progress" runat="server" Value="**********" />
            <asp:Button ID="convertToTitleCase" runat="server" Text="Title Case" />
        </p>
    
    </div>
    </form>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire