Friday, April 30, 2021

C# Examples of String Methods

using System;

namespace Console_String_Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            string quote = "To live alone is the fate of all great souls.";
            int quoteLength = quote.Length;
            Console.WriteLine(quoteLength);
            Console.WriteLine(quote.ToUpper());
            Console.WriteLine(quote.ToLower());
            Console.WriteLine(quote.Substring(3));
            Console.WriteLine(quote.Substring(3,10));
            Console.WriteLine(quote.Substring(3,4).StartsWith('L'));
            Console.WriteLine(quote.Substring(3,4).StartsWith('l'));
            Console.WriteLine(quote.Split(' ')[2]);
            Console.WriteLine(quote.Replace("live","die"));
            Console.WriteLine(quote.Replace(" ","*"));
            Console.WriteLine(quote.Remove(3,6));
            Console.WriteLine("Total characters: {0}",quote.ToCharArray().Length);
            Console.WriteLine("Total characters without spaces:{0}",quote.Replace(" ", "").ToCharArray().Length);
        }
    }
}

OUTPUT
45
TO LIVE ALONE IS THE FATE OF ALL GREAT SOULS.
to live alone is the fate of all great souls.
live alone is the fate of all great souls.
live alone
False
True
alone
To die alone is the fate of all great souls.
To*live*alone*is*the*fate*of*all*great*souls.
To lone is the fate of all great souls.
Total characters: 45
Total characters without spaces:36

No comments:

Post a Comment

Hot Topics