Lesson 2.15

String Data Type C# - Programming Example


Objective

What is string data type in c#?
How to declare and initialize
Programming examples

What is string Data Types in C#?

"A string is a sequence of zero or more Unicode characters. It allows you to assign string values to its variable. This string value can be a word, characters or sentence. It is a reference type data type and derived from System.String class."

TypeRangeSize.Net Framework type
char0-2 billion Unicode characters--System.String

Declaration and Initialization

You can declare string variable as follows:

		  
 string s1=”Welcome to CompileCode.Net”; //Keep value under double quotes (")
		  

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            Console.Write("Enter Your String Value: ");
            str = Convert.ToString(Console.ReadLine());            
            Console.WriteLine("\n\nOutput is : " + str);
            Console.ReadKey();
        }
    }
}

		  

Output

Enter Your String Value: CompileCode.Net

Output is : CompileCode.Net
_

Uses of Operators with string

1.   = (equal to), == (equal equal to), != (not equal to)

= (equal to) operator is used for assign values to string variable.
==(equal equal to) operator is used for comparing two string variables.
!= (not equal to) operator is also used for comparing two string variables.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string password;
            password = "P1234";

            Console.Write("Enter Your Password : ");
            string userpass = Convert.ToString(Console.ReadLine());

            if (password == userpass)
            {
                Console.WriteLine("Authorized Access");
            }
            else if (password != userpass)
            {
                Console.WriteLine("Unauthorized Access");
            }
            else
            {
                Console.WriteLine("Please Try Again");
            }
            Console.ReadKey();
        }
    }
}

		 

Output

Enter Your Password : P1234
Authorized Access
__

2.   + (plus), += (plus equal to)

+ and += operator is used for adding string variable into one variable. It combines values into a single string.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            name = "your name is ";
            Console.Write("Enter your name: ");
            name += Convert.ToString(Console.ReadLine());
            Console.WriteLine(name);
            Console.ReadKey();
        }
    }
}

	 

Output

Enter your name: Mark Clark
your name is Mark Clark
__

3.   [] Operator

[] Operator is used for randomly access to individual character of strings.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            name = "CompileCode.Net";
            char ch = name[4]; 
            //It will return 4th position character of string starting with 0
            Console.WriteLine(ch);
            Console.ReadKey();
        }
    }
}
		 

Output

i
__

4.   \\ escape sequence

\\ escape sequence is used for adding back slash to string.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            //str="4\2=2"; //Raise Error
            str="4\\2=2"; //Correct Way
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

		 

Output

4\2=2
__

5.   @ literals

@ literals helps to convert complex path into string into simple path string. For example
If you have to write following path into string C:\Data\Program\CompileCode.txt then either you may write this

String str1=” C:\\Data\\Program\\CompileCode.txt”;

Or

String str2 = @”C:\Data\Program\CompileCode.txt”;

Programming Example

		 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace string_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            string path;
            [email protected]"C:\Data\Program\CompileCode.txt";
            Console.WriteLine(path);
            Console.ReadKey();
        }
    }
}

		 

Output

C:\Data\Program\CompileCode.txt __

Summary

string data type in c# is used for storing string values to its variable. These string values may be a sentence or word. See programming example how to use string data type in c sharp program. In the next chapter we will discuss on bool data type

BackNext