ASP.Net C# String toCharArray Function

by top54u.com 02 Jul, 2008
Spotlight.....

In ASP.Net 2.0, C# String toCharArray Function provides the functionality to convert the string to char [ ] array. You can also use the C# string toCharArray function to convert the substring of the specified string to char [ ] array. C# toStringArray function creates the memory space for each character in a string and stores it at different indexes created for each element. After converting the values into char [ ] array you can use the C# for loop through the char array to read the values stored at each index of the array. Following are the overloads of the C# String toCharArray function:

 

  1. public char[] ToCharArray();

  2. public char[] ToCharArray(int startIndex, int length);

 

 

First type of overloaded function of C# string toCharArray takes no parameter, it converts the whole string specified to the char [ ] array. Second overloaded function takes two parameters, first is an integer type that accepts the startIndex for the substring and second integer type parameter as length for the substring as a part of the specified string to convert that substring to char [ ] array.

 

Examples of ASP.Net C# String toCharArray Function

Example 1:

// string.ToCharArray function

string strText = "ASP.Net C# string.ToCharArray function";



char[] charArr1 = strText.ToCharArray();



for (i = 0; i < charArr1.Length; i++)
{
    Response.Write(charArr1[i] +
"&nbsp;&nbsp;");
}

 

Output:

A  S  P  .  N  e  t     C  #     s  t  r  i  n  g  .  T  o  C  h  a  r  A  r  r  a  y     f  u  n  c  t  i  o  n

 

 

Example 2:

 

char[] charArr2 = strText.ToCharArray(strText.IndexOf("C#"), strText.Length - strText.IndexOf("C#"));



for (i = 0; i < charArr2.Length; i++)
{
   Response.Write(charArr2[i] +
"&nbsp;&nbsp;");
}



 

Output:

C  #     s  t  r  i  n  g  .  T  o  C  h  a  r  A  r  r  a  y     f  u  n  c  t  i  o  n

 

Note that in the second example we have used the overloaded function of ASP.Net 2.0 C# String toCharArray. IndexOf String function is being used to pass the start index for the substring and length is passes as the string length reduced by the index of start index value for substring so that the function could read the substring within the limit of specified string otherwise it will throw an exception of "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection."

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 8:44:45 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....