C# Working With Strings

C# Code Snippets Working with strings
Share:

About

In this code snippet, we will learn how to work with and manipulate strings in C#.

Let’s have a look at the code below to work with strings.

Code:

using System;

namespace strings
{
    class Program
    {
        static void Main(string[] args)
        {

            string stringOne = "Hello World!";


            //Make a string uppercase.
            Console.WriteLine(stringOne.ToUpper());
            //Make a string lowercase.
            Console.WriteLine(stringOne.ToLower());


            //Get a character at a specific position like so(a string is essentialy an array of characters):
            char ch = stringOne[3];

            Console.WriteLine(ch);


            //Replace a charater/string within your string:
            Console.WriteLine(stringOne.Replace("Hello", "Hell"));


            //Check if your string contains a specific character 
            if (stringOne.Contains("a"))
            {
                Console.WriteLine("Yes.");
            }
            else
            {
                Console.WriteLine("No.");
            }


            //Split your string it into many strings when a specified character is encountered and store them in an array.
            string[] strings = stringOne.Split('l');

            //Display the string in the array.
            foreach (string str in strings)
            {
                Console.WriteLine(str);
            }

            Console.WriteLine("");


            //Get the index of a character/string within your string.
            Console.WriteLine(stringOne.IndexOf("Wor"));


            //Insert a character/string into your string.
            Console.WriteLine(stringOne.Insert(0, "HHH"));
            //remove a character/string from your string.
            Console.WriteLine(stringOne.Remove(0, 1));


            //Get the a part of your string between the specified positions.
            Console.WriteLine(stringOne.Substring(5, 7));


            string stringTwo = "   Trim me. ";

            //Get rid of spaces.
            Console.WriteLine(stringTwo.Trim());
            //Get rid of spaces at the front.
            Console.WriteLine(stringTwo.TrimStart());
            //Get rid of spaces at the end.
            Console.WriteLine(stringTwo.TrimEnd());


            //Joining strings.
            Console.WriteLine("Hello" + " " + "World!");
            //Or if you have an array.
            string[] words = { "Hello", " ", "World", "!" };
            Console.WriteLine(string.Concat(words));


            //Get the length of your string.
            Console.WriteLine(stringOne.Length);


            Console.ReadLine();
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger