create account

String Manipulation in C#: Part 2 by leoumesh

View this thread on: hive.blogpeakd.comecency.com
· @leoumesh ·
$3.81
String Manipulation in C#: Part 2
<div class="text-justify">


<center>![image.png](https://files.peakd.com/file/peakd-hive/leoumesh/Eo8QuYF55MpVoxHQpM9Y7oqzFewGKb5TTkYf45UQLhVnDvDoH65h2onoZn2uQntU6Bb.png)</center>
<center>[Image Source](https://witscad.com/course/csharp-basics/chapter/string-manipulations)</center>

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.");
    }
}
```
<hr>
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).

<h4>**Link to other C# posts**</h4>

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)
</div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 264 others
👎  
properties (23)
authorleoumesh
permlinkstring-manipulation-in-c-part-2
categoryhive-196387
json_metadata{"app":"peakd/2025.3.5","format":"markdown","tags":["csharp","code","coding","program","programming","tech","geek","tutorial","education","blog"],"users":["leoumesh"],"image":["https://files.peakd.com/file/peakd-hive/leoumesh/Eo8QuYF55MpVoxHQpM9Y7oqzFewGKb5TTkYf45UQLhVnDvDoH65h2onoZn2uQntU6Bb.png","https://files.peakd.com/file/peakd-hive/leoumesh/23swgECbxgxKVJXGW1dRHxHiNsi2kXg27MfX4jWv3Mm7g1zknw7Z8cv5VJnAZKnWrW26f.png","https://files.peakd.com/file/peakd-hive/leoumesh/23swgECbxgxKLcKktoCokKF5FxVrUM4SJEqregANN2eGfBTJVQHwbGdxJzKgoDGYzMZqt.png","https://files.peakd.com/file/peakd-hive/leoumesh/23swg9r87ZuqqgtLM73tgDAmxjPYhUa2a1pQqRukV4yQrSpjmijC3EnbJbymv6Zmdp5zY.png"]}
created2025-03-27 02:31:30
last_update2025-03-27 02:31:30
depth0
children1
last_payout2025-04-03 02:31:30
cashout_time1969-12-31 23:59:59
total_payout_value1.874 HBD
curator_payout_value1.936 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,029
author_reputation212,340,493,251,438
root_title"String Manipulation in C#: Part 2"
beneficiaries
0.
accountstemsocial
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,705,298
net_rshares12,007,329,877,457
author_curate_reward""
vote details (329)
@stemsocial ·
re-leoumesh-string-manipulation-in-c-part-2-20250327t034851955z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-leoumesh-string-manipulation-in-c-part-2-20250327t034851955z
categoryhive-196387
json_metadata{"app":"STEMsocial"}
created2025-03-27 03:48:51
last_update2025-03-27 03:48:51
depth1
children0
last_payout2025-04-03 03:48:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length545
author_reputation22,920,436,264,631
root_title"String Manipulation in C#: Part 2"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,706,077
net_rshares0