Capitalise first letter of each word in a string




Capitalise first letter of each word in a string
If like me you get fed up of forms being filled in either all in CAPITALS or lower case, then this could be the solution:
 
At the below line to the top of you .vb page:
  1. Imports System.Globalization  
Now near where you are using the string, but above it enter the below line of code:
  1. Dim strCITI As TextInfo = New CultureInfo("en-GB", False).TextInfo  
Now where your using the string, do it like below:
  1. Dim strName As String = strCITI.ToTitleCase(txtName.Text)  
That's it.

An example of this in action would be:
  1. Imports System.Data   
  2. Imports System.Data.SqlClient   
  3. Imports System.Globalization   
  4.   
  5. Partial Class templates_home   
  6.     Inherits System.Web.UI.Page   
  7.   
  8.     Protected Sub btnSubmit_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)   
  9.   
  10.         Dim MyConnection As SqlConnection   
  11.         MyConnection = New SqlConnection()   
  12.         MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString   
  13.   
  14.        Dim SQL As String = "INSERT INTO TABLE (name) VALUEs(@name)"  
  15.   
  16.         Dim Cmd As New SqlCommand(SQL, MyConnection)   
  17.   
  18.         Dim strCITI As TextInfo = New CultureInfo("en-GB", False).TextInfo   
  19.   
  20.         Dim strName As String = strCITI.ToTitleCase(txtName.Text)   
  21.   
  22.         Cmd.Parameters.Add(New SqlParameter("@Name", strName))   
  23.   
  24.         MyConnection.Open()   
  25.         Cmd.ExecuteNonQuery()   
  26.         MyConnection.Close()   
  27.   
  28.     End Sub  
  29.   
  30. End Class  
Where 'en-GB' is used, you will need to change this to your relavent country.  Below are two examples:

en-GB = English, United Kingdom
en-US = English, United States


6 people like this article, click here to say you like this article too (6)
Send a link to this page via email Send a link to this page