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:
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.
Example 1:
// string.ToCharArray function
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] + " "); }
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."
Be the first to rate this post
Tags: asp.net 2.0, c#, c# string tochararray function, asp.net string function, asp.net examples, asp.net string tochararray, c# split, c# replace, c# string tochararray, c# indexof, c# for loop, c# char array, c# string array
8/23/2008 8:44:45 PM