Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - STRINGS


A String is a collection of letters/alphabets. It can be a word or a sentence.


The Data Type for String is string.


Declaring a String with Double Quotes ("")


When C# finds data inside Double Quotes (""). It creates a String type variable for that value.


var x = "Hello World";

var is independent of data type. i.e. The above variable x can accept a value of any data type.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
     	var x = "Hello World";
   		System.Console.WriteLine(x);
    }
}


Output :



  Hello World

And that's all! You have a String in place.


Well! There are other ways using which you can create a string.


Declaring a String with string data type


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
     	string x = "Hello World";
   		System.Console.WriteLine(x);
    }
}	


Output :



  Hello World

So, in the above code, we have declared a variable x of String type.


string x = "Hello World";

So that it would not accept any values other than a String.


Also one more advantage of declaring using a string type is that you can assign the value to the String variable later.


Let us understand with the below code.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string x;
   	    x = "Hello World";
   	    System.Console.WriteLine(x);
    }
}	


Output :



  Hello World

Just note that we have not initialised the variable x. We have just declared it.


string x;

So that an empty variable x would be created that would only hold a String.

C_Sharp

Then in the next line, we have assigned the value Hello World to the variable x.


x = "Hello World";
C_Sharp


Well! We have declared a String type variable and assigned it later.


But the below code won't work, if we do not specify a data type.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        var x;
   	    x = "Hello World";
   	    System.Console.WriteLine(x);
    }
}	


Output :



  error CS0818: Implicitly-typed variables must be initialized

So far, we have seen, how to declare a String type variable in one line.


Now, what if, we want a paragraph, separated by lines, to be assigned to a variable.


i.e. Let's say, you want to assign the below paragraph to a variable.

"In a huge pond,
there lived many fish.
They were arrogant
And never listened to anyone."

In such cases, you can use @ before double quotes.


Declaring a multiline String with verbatim identifier (@)


Declaring a multiline String with verbatim identifier (@) :


Example :



public class MyApplication
{
public static void Main(string[] args)
{
var str = @"In a huge pond,
there lived many fish.
They were arrogant
And never listened to anyone.";

System.Console.WriteLine(str);
}
}


Output :



  In a huge pond,
  there lived many fish.
  They were arrogant
  And never listened to anyone.

Accessing the characters of a String


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        var x = "Hello"; 
	    var y = x[1];
   	    System.Console.WriteLine(y);
    }
}


Output :



  e

So, we have a string Hello,


var x = "Hello";

And we have assigned Hello to a variable x.

C_Sharp

Let us elaborate more, on how the String Hello is stored in the variable x.

C_Sharp

So, just for the sake of understanding, you can assume the variable x is divided into 5 parts to store Hello.


Now, if you check from the front, the count starts from 0 and ends with 4.


So, if we look at the next line in the code,


var y = x[1];

We are trying to access the 2nd location,

C_Sharp

So, x[1] is referring to the 2nd location where e is stored.


var y = x[1];

And we have assigned e to the variable y.

C_Sharp

Note : The square brackets '[]' are used with the string type variable to get the variable at that position(i.e. x[1] refers to the 2nd location that contains 'e').

Now, if you look at the print statement,


System.Console.WriteLine(y)

The value of y is printed as output.

Output :



  e

In the earlier examples, we have seen the + sign. And as we know, + is used for addition of two numbers.


Well! In C#, + can be used to join two Strings as well.


Concatenation of Strings


Say, we have two Strings, Hello and World. And we want to join/concatenate them into one String, HelloWorld.


Let us solve it with the below example.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        var x = "Hello";
       	var y = "World";
       	var z = x + y;
       	System.Console.WriteLine(z);
    }
}


Output :



  HelloWorld

So, we have stored the String Hello in x.


var x = "Hello";

And, stored the String World in y.


var y = "World";

And used the + operator to join/concatenate, Hello and World.


var z = x + y;

And store the concatenated value in z.

C_Sharp

So, + is not just used to add two numbers but + is also used to join/concatenate two Strings.


Next, let us see, how can we iterate the elements of a string.


Iterate a String using for range loop


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
       	string x = "Hello";
        foreach (var i in x) {
            System.Console.WriteLine(i);
        }
    }
}


Output :



  H
  e
  l
  l
  o

So, in each Iteration of for loop,


foreach (var i in x)

The values are taken from the variable x, one by one and put in the variable i.


And values are printed at each Iteration.


System.Console.WriteLine(i)

Note : The below topic String Interpolation is already explained in the tutorial Input & Output.

String Interpolation


Say, you have stored a number in a variable and you want to insert it in the middle of a String.


i.e. Say, you have two numbers 3 and 4 and you have added them and want to display the result in the following way,


The added result of 3 and 4 is 7

Well! We can achieve it using String formatting.


String Interpolation using '$' and '{}'


Let us rewrite the above example using the String Template Operator $.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
       	var x = 3;
    	var y = 4;
    	var z = x + y;
    	System.Console.WriteLine($"The added value of {x} and {y} is {z}");
    }
}


Output :



  The added value of 3 and 4 is 7

Now, if you look at the output,


The added value of 3 and 4 is 7

The variables of x, y and z is replaced with the actual values i.e. 3, 4 and 7.

C_Sharp

How to find the length of a String?


Length keyword is used to return the length of a String.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
       	var x = "Hello";
        System.Console.WriteLine("The length of the String is : "+x.Length);
    }
}


Output :



  The length of the String is : 5