Tutorialspoint.dev

C# | String Properties

The string is an array of characters. String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of String class is to provide the properties and methods so that it becomes easy to work with strings.

There are the two properties of String class :

  1. Chars[Int32]: Used to get the Char object at a specified position in the current String object. In C#, the Chars property is an indexer.
  2. Length: Used to get the number of characters in the current String object.

String.Chars Property (Int32)

Syntax:

public char this[int index] 
{ 
      get; 
}
  • Parameters: This method only take one parameter i.e. index which is the position in the current string and its type is System.Int32. The index parameter is zero-based.
  • Return Value: This method returns the Char object at the position specified by the index parameter and Its property value type is System.Char.

Below are the programs to illustrate the Chars Property:



  • Program 1: Each Char object in the string can be accessed by using code such as the following. .
    // C# program to demonstrate the 
    // Chars property of String class
    using System;
    class Geeks {
      
    // Main Method
    public static void Main()
    {
        string str = "GeeksforGeeks";
        for (int i = 0; i <= str.Length - 1; i++ )
        Console.Write("{0} ", str[i]);
    }
    }

    Output:

    G e e k s f o r G e e k s
    
  • Program 2: The use of this indexer in a routine to validate a string.
    // C# program to check the whether the 
    // character is number or character
    using System;
    class Geeks {
        
        // Main Method
        public static void Main()
        {
            string str = "11Gee45for78geeks";
      
            for (int i = 0; i < str.Length; i++)
            {
                if (Char.IsDigit(str[i]))
                    Console.WriteLine("{0} is a Number.", str[i]);
                else
                    Console.WriteLine("{0} is a character.", str[i]);
            }
        }
    }

    Output:

    1 is a Number.
    1 is a Number.
    G is a character.
    e is a character.
    e is a character.
    4 is a Number.
    5 is a Number.
    f is a character.
    o is a character.
    r is a character.
    7 is a Number.
    8 is a Number.
    g is a character.
    e is a character.
    e is a character.
    k is a character.
    s is a character.
    

String.Length Property

The Length property returns the number of Char objects in this instance, not the number of Unicode characters because a Unicode character might be represented by more than one Char.

Syntax:

public int Length 
{ 
    get;
}
  • Return Value: It returns the number of Char objects in this instance of String.

Note: In C and C++, a null character indicates the end of a string but in C#, a null character can be embedded in a string. When a string includes one or more null characters, then it also considers in the total length of that string. For example, If in a string, the substrings “xyz” and “abc” are separated by a null character like as String value is “xyzabc”, Then Length property returns 7, where it includes the six alphabetic characters as well as the null character.

Below are the programs to illustrate the Length Property:

  • Program 1:
    // C# program to demonstrate the 
    // Length property
    using System;
    class Geeks
    {
          
       // Main Method
       public static void Main()
       {
      
          // here include four null character
          // between xyz and abc substring
          string str = "xyzabc";
          Console.WriteLine(str.Length); 
       }
    }

    Output:

    10
    
  • Program 2:
    // C# program to illustrate the 
    // Length property of String class
    using System;
    class Geeks {
       
    // Main Method
    public static void Main()
    {
      
            // taking the string variable
            // and then find length
        string str = "GeeksforGeeks";
      
        Console.WriteLine("'{0}' length : {1}", str, str.Length);
      
             // Directly use length property 
            // with string here no variable use 
        Console.WriteLine("'{0}' length : {1}", "GEEKS", "GEEKS".Length);
              
            // return value stored the in 
            // integer variable l
        int l = str.Length;
        Console.WriteLine("'{0}' length : {1}", str, l);
    }
    }

    Output:

    'GeeksforGeeks' length : 13
    'GEEKS' length : 5
    'GeeksforGeeks' length : 13
    

References:



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter