Framework Fundamentals - String

  • A C# string represents a sequence of Unicode Characters and it is defined in System namespace as System.string class.
  • String is immutable.
  • Construction of String
           //Method 1 - using Literals
            string s1 = "Sample string";
            //Method 2 - repeating sequence of characters with string Constructor
            string s2 = new string('*', 10); // **********
            //Method 3 - using Character array
            char[] charArray = "Hello".ToCharArray();
            string s3 = new string(charArray); // Hello
  • Null and empty strings
    • An empty string has a length of zero.
    • To create an empty string, you can use either a literal("") or the static string.Empty field;
    • Because strings are reference types, they can also be null.
    • The static string.IsNullOrEmpty method is a useful shortcut for testing whether a given string is either null or empty.
  • Accessing characters within a string
            //Method 1 - using Indexer
            string s1 = "Hello";
            char charAtIndexOne = s1[1]; // e

            //Method 2 - using foreach
            foreach (char item in s1)
            {
                Console.Write($"{item},"); // H,e,l,l,o,
            }
  • Searching within strings
    • string class provides instance method for searching within strings
    • Take Note: all string methods are case-sensitive and culture-sensitive.
            string s1 = "Hello Mr. X";
            Console.WriteLine(s1.Contains("x"));     //False - as it is smaller case
            Console.WriteLine(s1.StartsWith("HE"));  //False
            Console.WriteLine(s1.StartsWith("He"));  //True - as it is matches case
            Console.WriteLine(s1.EndsWith("x"));     //False -
            Console.WriteLine(s1.IndexOf("X"));      //10
            Console.WriteLine(s1.IndexOf("M",7));    // -1 , it searches from index 7 i.e character r in the string
                                                     // so it cannot find and returns  -1

            Console.WriteLine(s1.IndexOf("l"));      //2 searches from first character of the string i.e H
            Console.WriteLine(s1.LastIndexOf("l"));  //3 searches from last character of the string i.e X
            Console.WriteLine(s1.LastIndexOf("l", 1)); // -1 , it searches from index 1 i.e character e in the string

            Console.WriteLine(s1.IndexOfAny("abcde".ToCharArray())); // searches for either a or b or c or d or e
                                                                     //1 - found e in index 1
            Console.WriteLine(s1.LastIndexOfAny("abcde".ToCharArray())); // searches for either a or b or c or d or e
                                                                         //Search from last character
                                                                         //1 - found e in index 1

            Console.WriteLine(s1.IndexOfAny("Xabcde".ToCharArray())); // searches for either X or a or b or c or d or e
                                                                     //1 - found e in index 1
                                                                     //Searches from first character
            Console.WriteLine(s1.LastIndexOfAny("Xabcde".ToCharArray())); // searches for either a or b or c or d or e
                                                                         //Search from last character
                                                                         //10 - found e in index 1

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...