String Manipulation in C#: Part 2

@leoumesh · 2025-03-27 02:31 · StemSocial
![image.png](https://files.peakd.com/file/peakd-hive/leoumesh/Eo8QuYF55MpVoxHQpM9Y7oqzFewGKb5TTkYf45UQLhVnDvDoH65h2onoZn2uQntU6Bb.png) [Image Source](https://witscad.com/course/csharp-basics/chapter/string-manipulations) In my previous tutorial we saw a basic introduction to string and various in-built functions that can be used to perform operation and manipulation on C# strings. In this tutorial, we are gonna discuss other some key concepts in string operations that you may come across in C#. One of the key concept in string operation is **String Interpolation** that was introduced in the year 2014 in C# version 6. It provides a more convenient and human readable approach to writing code by allowing us to substitute a value of string into a placeholder. Allowing this would save a chunk of time, memory allocation which would have been caused due to string concatenation. Lets see a simple example of this one:
using System;

class Program
{
    static void Main()
    {
        int age = 25;
        string name = "Peter";

        Console.WriteLine($"My name is {name} and I am {age} years old.");
    }
}
--- If you run this code, you will get the following output: ![image.png](https://files.peakd.com/file/peakd-hive/leoumesh/23swgECbxgxKVJXGW1dRHxHiNsi2kXg27MfX4jWv3Mm7g1zknw7Z8cv5VJnAZKnWrW26f.png) See how much easier it is to write and debug a code using this approach. You need to use **"$"** sign while interpolating string and then **"{}"** brackets while substituting a value. Another thing we want to look on is accessing a strings. We can do this by using an index number of string inside a **"[]"** brackets. If we run the code `Console.WriteLine(name[3]);` then we will get the output **e** as index always starts from 0. We can also use function called `IndexOf` to get the index value of a characters in a string.
using System;

class Program
{
    static void Main()
    {
        int age = 25;
        string name = "Peter";

        Console.WriteLine($"My name is {name} and I am {age} years old.");

        Console.WriteLine(name[3]);
        Console.WriteLine(name.IndexOf("t"));
    }
}
You can get the corresponding output for this code: ![image.png](https://files.peakd.com/file/peakd-hive/leoumesh/23swgECbxgxKLcKktoCokKF5FxVrUM4SJEqregANN2eGfBTJVQHwbGdxJzKgoDGYzMZqt.png) The last thing we wanna see about is using special characters like double quotes inside a double quotes. String should always be written inside a double quote in C# and if you want to write a string that contains double quote inside it then you can use backslash escape character.
using System;

class Program
{
    static void Main()
    {
        string message = "Sometimes this process is also referred to as \"vaporization\".";

        Console.WriteLine(message);

    }
}
If you try to run the code without using backslash character(\) then you will get the error as compiler will misunderstand the syntax for the string. Lets see how to print a statement that contains **'s** inside a string in C#. For this you need to use single backslash(\) as:
using System;

class Program
{
    static void Main()
    {

        string message = "Sometimes this process is also referred to as \"vaporization\".";

        Console.WriteLine(message);

        string message2 = "It\'s known as vaporization.";

        Console.WriteLine(message2);

    }
}
The output for this code is: ![image.png](https://files.peakd.com/file/peakd-hive/leoumesh/23swg9r87ZuqqgtLM73tgDAmxjPYhUa2a1pQqRukV4yQrSpjmijC3EnbJbymv6Zmdp5zY.png) There are other special useful escape characters in C# that we will see as we go along the tutorial like New line (\n), backspace(\b), tab(\t).

**Link to other C# posts**

1. [Introduction to C# and .NET](https://hive.blog/hive-196387/@leoumesh/introduction-to-c-and-net) 2. [String Manipulation in C#](https://hive.blog/hive-196387/@leoumesh/string-manipulation-in-c)
#csharp #code #coding #program #programming #tech #geek #tutorial #education #blog
Payout: 0.000 HBD
Votes: 329
More interactions (upvote, reblog, reply) coming soon.