Capitalise first letter of each word in a string
Thursday 06 August 2009
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:
- Imports System.Globalization
Now near where you are using the string, but above it enter the below line of code:
- Dim strCITI As TextInfo = New CultureInfo("en-GB", False).TextInfo
Now where your using the string, do it like below:
- Dim strName As String = strCITI.ToTitleCase(txtName.Text)
That's it.
An example of this in action would be:
- Imports System.Data
- Imports System.Data.SqlClient
- Imports System.Globalization
-
- Partial Class templates_home
- Inherits System.Web.UI.Page
-
- Protected Sub btnSubmit_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
-
- Dim MyConnection As SqlConnection
- MyConnection = New SqlConnection()
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
-
- Dim SQL As String = "INSERT INTO TABLE (name) VALUEs(@name)"
-
- Dim Cmd As New SqlCommand(SQL, MyConnection)
-
- Dim strCITI As TextInfo = New CultureInfo("en-GB", False).TextInfo
-
- Dim strName As String = strCITI.ToTitleCase(txtName.Text)
-
- Cmd.Parameters.Add(New SqlParameter("@Name", strName))
-
- MyConnection.Open()
- Cmd.ExecuteNonQuery()
- MyConnection.Close()
-
- End Sub
-
- 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)
|
|
|