Print a String in Single and Double Quotes in C#


Today, I have provided an article showing you how to print a string in single and double quotes in C#. There are many ways in which a user can print a string in single and double Quotes. Today we give away a simple form, it's easy to implement. First of all start Visual Studio .NET and make a new ASP.NET web site using Visual Studio 2010. Drag and drop two Button controls and two Label controls onto the form. When you click on the first Button, first the Label displays the single quote and when you click on the second Button the second Label displays the double quote in C#.

Now you have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

img5.gif

Now add a new page to the website.

  • Go to the Solution Explorer
  • Right-click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK

img6.gif

Now drag and drop two Button and two Label controls onto the form. One is used to show the message with single quotes. The second one is used to show the message with double quote. Let's take a look at a practical example.

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

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <br />

        <br />

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

        <br />

        <br />

        <br />

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="DisplaySinglequetes" />

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="DisplayDoublequetes" />

    </div>

    </form>

</body>

</html>

 

Print a string with single Quotation mark

 

Now double-click on the first Button control and write the following code to show the message with single quotes. For example we take a string variable named strSingleQuote which contains a message.

 

protected void Button1_Click(object sender, EventArgs e)

    {

        string strSingleQuote = "'Hello Rohatash Kumar'";

        Label1.Text = strSingleQuote;

    }

 

Print a string with double Quotation mark

 

Now double-click on the second Button control and write the following code to show the message with double quotes. For example we take a string variable named strDoubleQuote which contains a message.

 

protected void Button2_Click(object sender, EventArgs e)

    {

        string strDoubleQuote = "Hi ''I am fine'' Rohatash";

        Label2.Text = strDoubleQuote;

    }

 

In code-behind write the following code.
 

Code-behind

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class Default2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string strSingleQuote = "'Hello Rohatash Kumar'";

        Label1.Text = strSingleQuote;

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        string strDoubleQuote = "Hi ''I am fine'' Rohatash";

        Label2.Text = strDoubleQuote;

    }

}

 

Now run the application and test it.

 

img1.gif

 

Now click on the Button DisplaySinleQuotes to show the message with single quotes.

 

img2.gif

 

Now click on the Button DisplayDoubleQuotes to show the message with double quotes.

 

img3.gif

 

Some Helpful Resources

Next Recommended Readings