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://www.w3.org/1999/xhtml"> <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>

-------------Problems Reply------------

A couple of comments:

  1. if (!Page.IsValid) - are you sure that's what you meant? Maybe if (Page.IsValid) is more like it.
  2. sentenceArray[i] = sentenceArray[i].Substring(0, 1).ToUpper() + sentenceArray[i].Substring(1).ToLower(); - what will happen if sentenceArray is empty (possible unless StringSplitOptions.RemoveEmptyEntries is given to String.Split)? - Either add StringSplitOptions.RemoveEmptyEntries as a parameter to String.Split - or add a check here.

Your code is executed only when page is not valid:

if (!Page.IsValid)

And your only validator is

<asp:RequiredFieldValidator ID="letterValidator"
runat="server" ErrorMessage="Required field" ControlToValidate="phrase" />

So, just remove the if (!Page.IsValid) condition and it will work correctly.

P.S.: And as it has been already pointed in another answer, you will have to be careful with Substring. At least use String.Split(string, StringSplitOptions) with StringSplitOptions.RemoveEmptyEntries to avoid index exception when someone enters string with two sequential spaces.

Category:c# Views:3 Time:2018-03-10
Tags: asp.net

Related post

  • Force capitalised input to title case in CSS using text-transform 2009-06-23

    I'm creating a theme for a CMS, and only have the ability to change the CSS associated with the page (no Javascript, PHP, etc). Is there a way to change this list of users that we have: JOHN SMITH DAVID JONES ... into proper title case (John Smith, e

  • How to set Title case for label using Typographic case in flex 2012-05-23

    Is there is any possible to implement title case in typographic case inside the spark.Thanks in advance --------------Solutions------------- Hi, I think we can not set the Titlecase in Typographic case property directly. You can do it in action scrip

  • Converting upper-case string into title-case using Ruby 2009-11-24

    I'm trying to convert an all-uppercase string in Ruby into a lower case one, but with each word's first character being upper case. Example: convert "MY STRING HERE" to "My String Here". I know I can use the .downcase method, but that would make ever

  • Title of manually recorded programs use annoying Title Case 2013-01-03

    Not really a question, but I don't know where to give feedback to the MS Media Center team. As part of a manually planned recording, it's possible to change the title of that recording during filling out of the form (chosing channel, date, time an so

  • Convert string to title case with javascript 2008-10-13

    Is there a simple way to convert a string to title case? E.g. john smith becomes John Smith. I'm not looking for something complicated like John Resig's solution, just (hopefully) some kind of one- or two-liner. --------------Solutions------------- T

  • Is there a simple way to convert MySQL data into Title Case? 2009-07-28

    I have a MySQL table where all the data in one column was entered in UPPERCASE, but I need to convert in to Title Case, with recognition of "small words" akin to the Daring Fireball Title Case script. I found this excellent solution for transforming

  • Converting string to title case in C# 2009-07-30

    I have a string which contains a mixture of upper and lower case characters, for example "a Simple string" . What I want to do is to convert first character of each word ( I can assume that words are separated by spaces) into upper case. So I want th

  • In Java how do you Title Case a String while preserving some common abbreviations 2009-10-31

    There are many ways to Title Case in java. But how do you prevent some of the common abbreviations from being converted. For Example; System.out.println(makeProper("JAMES A SWEET"); System.out.println(makeProper("123 MAIN ST SW"); James A Sweet 123 M

  • How can I put names into title case in JavaScript? 2010-11-09

    I've been looking around on how to use JavaScript to set names to proper case, e.g. george mchall would become George McHall. I was able to find a write up on Codeproject on how to do this, as well as a person that intended it to do this: function to

  • "Title Case" Handled Incorrectly in Word 2013 2013-11-02

    In older versions of Word one could select a sentence, or a group of words, and then ask Word to change the case. You can still do this except with title case, which has been changed to "Capitalize Each Word," so users can no longer quickly change to

  • converting strings to Title case in JSTL 2009-01-27

    Is there any way to convert a string to Title case, using JSTL tags? Thanks in Advance. --------------Solutions------------- An alternative to transforming the string on the server is to let CSS do the work: text-transform: capitalize An idea: In a c

  • String conversion to Title Case 2009-07-06

    Is there any built in methods available to convert a string into Title Case format as such? --------------Solutions------------- Apache Commons StringUtils.capitalize() there are no capitalize() or titleCase() methods in String class. You have two ch

  • String parsing in Java with delimeter tab "\t" using split 2009-10-28

    I have to parse a line which is tab delimited. I parse it using the split function, and it works in most situations. The problem occurs when some field is missing, so instead of getting null in that field I get next value. I was store parsed values i

  • SQL Server CASE WHEN without using CASE WHEN 2009-11-05

    Is there a way to rewrite a Transact SQL statement that uses a CASE WHEN structure to do the same without using the CASE WHEN? I'm using a product that has a built-in query designer and its own pseudo-SQL. It has limitations on what I can use with SQ

  • I want to split string without using split function? 2010-05-30

    I want to split string without using split . can anybody solve my problem I am tried but I cannot find the exact logic. --------------Solutions------------- I'm going to assume that this is homework, so I will only give snippets as hints: Finding ind

  • Data Cleanup, post conversion from ALLCAPS to Title Case 2010-07-13

    Converting a database of people and addresses from ALL CAPS to Title Case will create a number of improperly capitalized words/names, some examples follow: MacDonald, PhD, CPA, III Does anyone know of an existing script that will cleanup all the comm

  • T-Sql function to convert a varchar - in this instance someone's name - from upper to title case? 2010-07-20

    Does anyone have in their back pocket a function that can achieve this? --------------Solutions------------- Found this here :- create function ProperCase(@Text as varchar(8000)) returns varchar(8000) as begin declare @Reset bit; declare @Ret varchar

  • Using split with \r\n\r\n 2010-10-12

    UPDATE: I did not realise that Thomas Levesque had posted a solution and worked on my own below one in the meantime. I’m not using C# / VS2010 for long but I must say that C# is unbelievably productive and I am kicking myself that I stuck with C++ an

  • How do I convert strings to title case in OpenEdge ABL / Progress 4GL? 2011-04-21

    How do I convert a string to title case in OpenEdge ABL (aka Progress 4GL)? I know I can get upper case with CAPS(), and lower case with LC(), but I can't find the title case (sometimes called proper case) function. Examples: Input Output -----------

  • Unicode-correct title case in Java 2011-09-09

    I've been looking through all StackOverflow in the bazillion of questions about capitalizing a word in Java, and none of them seem to care the least about internationalization and as a matter of fact none really seem to work in an international conte

  • Need to split a string into substrings but can't use split 2011-10-03

    I have a string that looks like this: connected to EDS via 10.98.765.432-4300. I need to split it like so: " connected to EDS via 10.98.765.432-4300." -----+------- --+- -+- -----+------- --+- | | | | | ClientIP |

  • convert string to title case with non-English chars (unicode) 2011-12-11

    Im trying to convert non-English string (greek) to title string I did try as this link suggest but with no luck, all the chars got Upper case Converting string to title case in C# how can i work with Unicode chars ? --------------Solutions-----------

  • Proper / title case in t-sql 2012-03-07

    I'm using SQL 2012 RC0, and looking for a function that can convert a column that holds first and last names into proper/title case. I've had a look around SO but can't seem to find anything, if there is another post please let me know and I will clo

  • How do you use Split() on a hyphen? 2012-04-13

    I have a string that contains "-" near the end. I want to return everything to the left of that hyphen. I don't know how to use Split() or Regex() to do that. --------------Solutions------------- Two methods that handle removing the hyphen and a non-

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.299 (s). 11 q(s)