Skip to main content

Convert String To Upper,Lower & Title(Proper) Case in ASP.Net C#.Net & VB.Net Using TextInfo Class

In this tutorial i am going to explain about how to Convert String To Upper,Lower & Title(Proper) Case Using TextInfo Class in System.Globalization Namespace in ASP.Net,C#.Net and VB.Net. Using the ToLower(),ToUpper() and ToTitleCase() static methods in the TextInfo class can be used to achieve this.


In my previous article i have explained about Build Products Comparision Table/Grid In ASP.Net , Store Custom Objects In Configuration File , Create Simple Mathmatical Captcha In ASP.Net , Visual Studio Keyboard Shortcuts , How To get data from WEB API , How To Create Your First WEB API Project and many articles in C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.


Open the visual studio and create a new empty website. Add a web form to the website and add the below html markup in the aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Convert String To Upper,Lower  & Title Case Using
                <br />TextInfo Class in System.Globalization Namespace
            </h2>
            <table border="1" style="border-collapse: collapse;">
                <tr>
                    <td style="width: 150px;">Actual Text</td>
                    <td>
                        <b>
                            <asp:Label ID="lblActual" runat="server" />
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>Upper Case </td>
                    <td>
                        <b>
                            <asp:Label ID="lblUpper" runat="server" />
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>Lower Case</td>
                    <td>
                        <b>
                            <asp:Label ID="lblLower" runat="server" />
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>Title Case</td>
                    <td>
                        <b>
                            <asp:Label ID="lblTitle" runat="server" />
                        </b>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


Now open the code behind file and include the necessary namespaces shown below if it is not already added.

using System.Globalization;
using System.Threading;

Then now i am creating object of TextInfo Class from the CurrentThread. And now i am assigning the converted string to each label. Below is the entire code.

C#.Net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Include this namespace if it is not already there
using System.Globalization;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string myString = "ThiS iS SaMpLe TexT fOr TeStiNG";

        //Get the culture property of the thread.
        CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
       
        //Create TextInfo object.
        TextInfo objTextInfo = cultureInfo.TextInfo;

        lblActual.Text = myString;
        // Changes a string to lowercase.
        lblLower.Text = objTextInfo.ToLower(myString);

        // Changes a string to uppercase.
        lblUpper.Text = objTextInfo.ToUpper(myString);

        // Changes a string to titlecase.
        lblTitle.Text = objTextInfo.ToTitleCase(myString);
    }
}

VB.Net:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
' Include this namespace if it is not already there
Imports System.Globalization
Imports System.Threading

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim myString As String = "ThiS iS SaMpLe TexT fOr TeStiNG"

        'Get the culture property of the thread.
        Dim cultureInfo As CultureInfo = Thread.CurrentThread.CurrentCulture

        'Create TextInfo object.
        Dim objTextInfo As TextInfo = cultureInfo.TextInfo

        lblActual.Text = myString
        ' Changes a string to lowercase.
        lblLower.Text = objTextInfo.ToLower(myString)

        ' Changes a string to uppercase.
        lblUpper.Text = objTextInfo.ToUpper(myString)

        ' Changes a string to titlecase.
        lblTitle.Text = objTextInfo.ToTitleCase(myString)
    End Sub
End Class

Now if you run the website the code will convert the string to different cases and bind it to corresponding labels. Below is the sample output.
Output of Convert String To Upper,Lower & Title(Proper) Case in ASP.Net C#.Net & VB.Net Using TextInfo Class

SourceCode:

Sourcecode of Convert String To Upper,Lower & Title(Proper) Case in ASP.Net C#.Net & VB.Net Using TextInfo Class
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..


Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.