• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Introduction » Type of String in C#
Next →
← Prev

Type of String in C#

By Dinesh Thakur

A string is an empty space, a character, a word, or a group of words.

A string is an empty space, a character, a word, or a group of words that you want the compiler to consider “as is”, that is, not to pay too much attention to what the string is made of, unless you explicitly ask it to. This means that, in the strict sense, you can put in a string anything you want.

Primarily, the value of a string starts with a double quote and ends with a double-quote.

An example of a string is “Welcome to the World of C# Programming!”. You can include such a string in the Console.Write () method to display it on the console. Here is an example:

We’ll be covering the following topics in this tutorial:

  • Simple string
  •                          
  • Handling of Strings
  • Mutable String

Simple string

using System;

class Bookclub

{

static void Main()

{

console.writeline(“welcome to the world of c# programming!”) ;

}

}

OUTPUT:

welcome to the world of c# programming!

Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the string keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:

string Msg;

After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. Here are examples of string variables declared and initialized:

string Empty = “”;

string Gender = “F”;

string FName = “Mahatama Gandhi”;

string Msg = “welcome to the world of c# programming! “;

After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using the Console.Write () or the Console.WriteLine () methods. Here is an example:

Use of string variable

using system;

class Bookclub

{

static void Main()

{

string Msg = “welcome to the world of C# programming! “;

console.writeLine(Msg);

 

}

}

 

OUTPUT:

Welcome to the World of C# Programming!

There are two types of string in C#:

1. Immutable strings

2. Mutable strings

The immutable strings are can’t be modify while mutable strings are modifiable.C# also supports a feature of regular expression that can be used for complex strings manipulations and pattern matching.

                          The immutable strings are can't be modify while mutable strings are modifiable.

Handling of Strings

We can create immutable strings using string or String objects in a number of ways.

There are some techniques to handling the immutable strings:

Assigning String

string s1;

s1 = “welcome”;

 

or

string s1 =”welcome”;

Copying String

string s2 = s1;

or

 

string s2 = string.copy(s1);

Concatenating Strings

string s3 =s1 + s2;

 

or

 

string s3 = string.Concat(sl,s2);

Reading from Console

string sl = console.ReadLine(); .

Converting Number to String

int num = 100 ;

string s1= num.Tostring();

Inserting String

string s1 = “wel”

string s2 = s1.Insert(3,”come”);

II s2 = welcome

string s3 = s1.Insert(3,”don”);

/ / s3 = we1don;

Comparing Strings

int n = string.Compare(sl,s2);

This statement will perform case-sensitive comparison and returns integer values for different conditions. Such as:

• If sl is equal to s2 it will return zero.

• If sl is greater than s2 it will return positive integer (1).

• If sl is less than s2 it will return negative integer(-l).

Or you can use following statement:

 

bool a = s2.Equals(sl);

bool b = string.Equal(sl,s2);

Above statements will return a Boolean value true (if equal) or false (if not equal).

Or you can also use the “==” operator for comparing the strings. Like as:

 

if (s1 == s2)

console.write (” both are equal”);

In this statement, it will return a Boolean value true (if equal) or false (if not equal

Mutable String

Mutable strings are those strings, which can be modifying dynamically. This type of strings are created using StringBuilder class. For Example:

 

StringBuilder sl = new StringBuilder (“welcome”);

StringBuilder s2 = new StringBuilder ( );

The string sl is created with an initial size of seven characters and s2 is created as an empty string. They can grow dynamically as more character added to them. Mutual string are also referred as a dynamic strings. Mutable string can be modify dynamically.

The StringBuilder class supports many methods that are useful for manipulating dynamic strings. Some of the most common methods are listed below:

                       Some of the most common methods

StringBuilder also provides some attributes to access some properties of strings, such as:

                        StringBuilder also provides some attributes to access some properties of strings

Here is an example program for mutable string. To use string builder class we have to use System. Text in the program.

Mutable string

using system.Text;        // For using StringBuilder

using system;

class strMethod

{

public static void Main( )

{

StringBuilder s = new stringBui1der(“c”};

console.writeLine(” stored string is :”+ s);

console.writeLine(“Length of string is :”+s.Length);

 

s.Append(“Sharp “);   II appending the string s

 

Console.writeLine(“After Append String is :”+ s);

console.writeLine(“Length of string is :”+s.Length);

//space will be count in “sharp” string.

 

s.Insert(7,”Language”); II inserting the string at last in s

 

console.writeLine(“After Insertion String is:”+ s);

console.writeLine(“Length of string is :”+s.Length);

 

int n = s.Length;

 

s [n] = “!”;

console.writeLine (” At Last String is:”+ s);

}

}

 

OUTPUT:

 

Stored string is : C

Length of string is 1

After Append string is : cSharp

Length of string is : 7

After Insertion string is : cSharp Language

Length of string is: 15

At Last String is : cSharp Language!

You’ll also like:

  1. C Program Using Pointers that Deletes all Occurences of a Particular Character in a String and Returns corrected String
  2. How will you define String Processing along with the different String Operations
  3. Difference Between Type Conversion and Type Casting
  4. Explicit Type Conversion (Type Casting)
  5. String Processing — Write out a function that prints out all the permutations of a String. For example, abc would give you abc, acb, bac, bca, cab, cba.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW