ASP.NET - please explian the code

Asked By ravi t on 25-Mar-11 04:13 AM
result = emailAddress.Substring(5,4)
div v replied to ravi t on 25-Mar-11 04:23 AM
hi,

string.Substring( startIndex,lenght);



for example:
string emailAddress = "dv@gmail.com";

string result1 = emailAddress.Substring(5, 4);
Response.Write(result1);

Result:
ail.

here string startindex  from 5( a) and lenght  4(ail.)
Riley K replied to ravi t on 25-Mar-11 04:28 AM
    
Substring method returns a part of a string
the index range starts from 0 and in you example (5,3)
it retruns the string starting form index 5 and until 3 letters

see the following ex and its output


      string s = "www.eggheadcafe.com";

    string sub = s.Substring(5, 3);

    Console.WriteLine(sub);

    Console.ReadLine();

output

ggh

Mihir Soni replied to ravi t on 25-Mar-11 04:39 AM
Hello,

Here is simple explanation of Substring functionality.

 Substring:-  The Substring method is an instance method on the string class, which means you must have a non-null string to use it without triggering an exception. This program will extract the first three characters into a new string reference, which is separately allocated on the managed heap.

Here is the simple code.

using System;
 
class Program
{
  static void Main()
  {
  string input = "OneTwoThree";
 
  // Get first three characters
  string sub = input.Substring(0, 3);
  Console.WriteLine("Substring: {0}", sub);
  }
}
Now in your case you are starring with 5th position till 4th character.

So you will get an output of four letters starting from 5th letter.

It will start from 0th position.