Check the string length in if statement in CSharp

Description

The following code shows how to check the string length in if statement.

Example


using System; /*from   www. jav  a  2 s .co m*/

public class MainClass{ 
   static void Main(string[] args) {  
      string input="java2s.com";  
      if (input == "")
      { 
         Console.WriteLine("You typed in an empty string."); 
      } 
      else if (input.Length < 5) 
      { 
         Console.WriteLine("The string had less than 5 characters."); 
      } 
      else if (input.Length < 10) 
      { 
         Console.WriteLine("The string had at least 5 but less than 10 Characters."); 
      } 
      Console.WriteLine("The string was " + input); 
   } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception