Wednesday, June 30, 2021

C# String Manipulations Properties and Methods

Topics Discussed

  1. Length of string
  2. String contains a character or not
  3. String contains a character or not, ignore case
  4. String contains a substring or not
  5. String contains a substring or not, ignore case
  6. String starts with a character or not
  7. String starts with a substring or not
  8. String starts with a substring or not, ignore case
  9. String ends with a character or not
  10. String ends with a substring or not
  11. String ends with a substring or not, ignore case
  12. get a substring from a string
  13. get a substring from a string from an index position
  14. remove all from an index position of the string
  15. remove all from an index position of the string up to a length
  16. insert a substring into a string
  17. check if two strings are equal or not
  18. split a string into an array using a delimiter
  19. concatenate strings
  20. join strings
  21. check whether string is null or empty
  22. get a repeated character as string 
  23. use of string class various methods


using System;

namespace ConsoleStringFunc
{
    class Program
    {
        static void Main(string[] args)
        {
            string myemail = "ak.shrishesh@linkedin.com";
            Console.WriteLine(myemail.Length); //25
            Console.WriteLine("\nContains");
            Console.WriteLine(myemail.Contains("@")); //True
            Console.WriteLine(myemail.Contains("Link", StringComparison.CurrentCultureIgnoreCase)); //True
            Console.WriteLine(myemail.Contains('@')); //True
            Console.WriteLine(myemail.Contains('R')); //False
            Console.WriteLine(myemail.Contains('R', StringComparison.CurrentCultureIgnoreCase)); //True
            Console.WriteLine("\nStartsWith");
            Console.WriteLine(myemail.StartsWith('a')); //True
            Console.WriteLine(myemail.StartsWith("ak")); //True
            Console.WriteLine(myemail.StartsWith("Ak", StringComparison.CurrentCultureIgnoreCase)); //True
            Console.WriteLine("\nEndsWith");
            Console.WriteLine(myemail.EndsWith('@')); //False
            Console.WriteLine(myemail.EndsWith("@")); //False
            Console.WriteLine(myemail.EndsWith("com")); //True
            Console.WriteLine(myemail.EndsWith("Com", StringComparison.CurrentCultureIgnoreCase)); //True
            Console.WriteLine("\nSubstring");

            //start position i.e. index starts from 0, length of substring

            Console.WriteLine(myemail.Substring(3));//shrishesh@linkedin.com, index=3
            Console.WriteLine(myemail.Substring(3, 9));//shrishesh, index=3, length=9
            Console.WriteLine("\nRemove(int startIndex, int Count)");
            Console.WriteLine(myemail.Remove(3));//ak.
            Console.WriteLine(myemail.Remove(3, 9)); //ak.@linkedin.com
            Console.WriteLine("\nEquals(string? value, )");
            Console.WriteLine(myemail.Equals("ak.shrishesh@linkedin.com")); //True
            Console.WriteLine(myemail.Equals("AK.shrishesh@linkedin.com")); //False
            Console.WriteLine(myemail.Equals("AK.shrishesh@linkedin.com", StringComparison.CurrentCultureIgnoreCase)); //True
            Console.WriteLine(myemail.Insert(3, "Ajeet"));//ak.Ajeetshrishesh@linkedin.com
            Console.WriteLine("LastIndexOf has 9 overloads");
            /*
             * Reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance
            */

            Console.WriteLine(myemail.LastIndexOf("@"));//12
            Console.WriteLine(myemail.LastIndexOf("@", 15));//12
            Console.WriteLine(myemail.LastIndexOf("@", myemail.Length));//12
            Console.WriteLine(myemail.LastIndexOf("shri"));//3
            Console.WriteLine(myemail.LastIndexOf("shri", 2));//-1
            Console.WriteLine("Split() method has 10 overloads and returns array of strings");

            string[] texts = myemail.Split("@");
            foreach (var text in texts)
            {
                Console.WriteLine(text);
            }
            //How many times i is repeated in myemail
            int repeatCounts = myemail.Split("x").Length - 1;

            Console.WriteLine("i appears {0} times.",repeatCounts);

            //repeat a character n times

            string rept = new string('A', 5);
            Console.WriteLine(rept);//AAAAA

            // string class methods

            Console.WriteLine(string.Concat("ajeet", "shrishesh", "@","gmail.com"));//ajeetshrishesh@gmail.com

            string zls = string.Empty; //zero length string

            Console.WriteLine(zls.Length);
            Console.WriteLine(string.IsNullOrEmpty(zls)); //True
            Console.WriteLine(string.Format("{0:c}", 100));//$100.00 currency format
            Console.WriteLine(string.Format("{0:d}", 100));//100 , decimal format
            Console.WriteLine(string.Format("{0:e}", 100));//1.000000e+002
            Console.WriteLine(string.Format("{0:p}", 100));//10,000.00%
            Console.WriteLine(string.Format("{0:x}", 100));//64 hexadecimal format
            Console.WriteLine(string.IsNullOrWhiteSpace("Ajeet Kumar"));//False
            Console.WriteLine(string.IsNullOrWhiteSpace("    "));//True
            Console.WriteLine(string.Join('-', new string[] { "Ajeet", "Kumar", "Shrishesh" }));//Ajeet-Kumar-Shrishesh

            string str1 = "Ajeet";
            string str2 = "Ajeet";

            //ReferenceEquals determines whether the specified System.Object instances are the same instance.
            Console.WriteLine(string.ReferenceEquals(str1, str2));//True

            string str3 = "ajeet";
            Console.WriteLine(string.ReferenceEquals(str1, str3));//False

            string obj1 = new string("Ajeet");
            string obj2 = new string("Ajeet");
            Console.WriteLine(string.ReferenceEquals(obj1, obj2)); //False
            Console.Read();
        }
    }
}

No comments:

Post a Comment

Hot Topics