String Manipulation in C#

in StemSocial2 days ago

image.png

Image Source

In this blog, we gonna see basic introduction to C# and do some operations on those strings. Strings are data type for storing text and manipulating them. Strings in C# are enclosed within double quotation ("") and can contain letters, characters, special symbols, even spaces or a combination of these. System.string class has an object string in C# that can help us to perform operations on the strings.

Lets start with a simple program. The following program demonstrates how string is initialized and concatenated using + operator.

using System;

class Program
{
    static void Main()
    {
        string text1 = "Hello";
        string text2 = "World";

        Console.WriteLine(text1 + " " +text2);
    }
}

The output of the above program will be as below. Remember that I have added some whitespace in between the text.

image.png

Another way to do the same thing is to use concat function as:

string message = string.Concat(text1,text2)

Console.WriteLine(message);

It will show the same output as above but there will be no whitespaces between these two texts.

Another string operation we can do is changing the uppercase and lowercase of the letters. To convert to uppercase we can write Console.WriteLine(text1.ToUpper()); and to change to lowercase we can write Console.WriteLine(text2.ToLower());. It will show the output as:

image.png

Another operation we can do on string is replacing the text. Suppose we want to print "GreetingsWorld" instead of "HelloWorld". We can do the same by doing: Console.WriteLine(message.Replace("Hello", "Greetings"));.
The output is as below:

image.png

In the next post, I am gonna show you about some more string manipulation like string interpolation, accessing strings and using special characters like double quotes inside double quotes.

Link to other C# posts:

  1. Introduction to C# and .NET
Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord 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.