STRING.LENGTH PROPERTY IN C#

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 


String.Length

 
Length is used to get the number of character used in the specified string from the given string. Its return type is Integer. Length property always implemented as String.Length.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringLength
    {
        public static void Main()
        {
            string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
            Console.WriteLine("Length");
            //Using Length
            Console.WriteLine("Using String: " + str.Length);
            Console.WriteLine("Using Substring: " + str.Substring(str.LastIndexOf("T")).Length);
        }
    }
}


Output

FIGURE 1


Click imaginationhunt.blogspot to see latest Blogs

In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- In first we learn how to get the length of string. We have done this by using the Length property across the string 'str'. The output that we get is the total characters used in the string i.e. 53, which means there are 53 characters used in the string 'str'.
b) Using Substring- Second one shows how we can get a sub part of the given string using substring() method and then find the length of the substring. For ex- fetching out 'Tap@tap.com' and then using the Length property across the substring will give the number of character used in the string. Hence, as result we get 11, which means there are 11 characters used in the substring.

Real time example of using Length:

Suppose your task is to build user input program in which user has to input a password. And the maximum length of the password is 10 characters.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class
StringLength 
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Enter your password - Max size 10 digit");
            string password = Console.ReadLine();
            int passwordlength = password.Length;
            if (passwordlength <= 10)
            {
                Console.WriteLine("Password saved.");
            }
            else
            {
                Console.WriteLine("Password length should be 10 digit max");

            }
         }
    }
}

 

Click imaginationhunt.blogspot to see latest Blogs

Output


FIGURE 2
Figure 2 shows the outcome when password length is within limit.

FIGURE 3
Figure 3 shows the outcome when password length is exceeding limit.

To read IS STRING MUTABLE OR IMMUTABLE IN .NET? - Click here.

Related Question:

#newtoprgm try firstprogram

Q-1 What will be the output of the code?

using System;

namespace AspnetSolutions
{
    class
StringLength
     {
        public static void Main(string[] args)
        {

           string emptyStr = "";
           string emptyStr3 = "\0";

           Console.WriteLine("Strpassed = {0} and Length = {1}", emptyStr, emptyStr.Length);
           Console.WriteLine("Strpassed = {0} and Length = {1}", emptyStr3, emptyStr3.Length);

        }
    }
}


A) 0,0
B) 0,1
C) 1,0
D) 1,1
Ans- Option(B).

Click imaginationhunt.blogspot to see latest Blogs

Q-2 What will be the output of the code?


using System;

namespace AspnetSolutions
{
    class
StringLength
     {
        public static void Main(string[] args)
        {

             Console.WriteLine("Imaginationhunt.blogspot".Length);
        }
    }
}

A) 23
B) 24
C) 0
D) 1
E) Error
Ans- Option(B).
Explanation- Yes, the statement is correct. Literals can be declared like this.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions