ASP.Net C# String Length Function

Updated on 18 May 2012,
Published on 04 Jul 2008

In ASP.Net C# string length function returns the maximum length of the string associated with System.String object. Length of the string can be used in C# string functions like remove and insert. C# string length function helps in getting the index of last character of the specified string. Suppose you want to remove or insert any string value nearest to the last character from the specified string then you can use length -1 or length -2 to get last or second last position in the target string.

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

Syntax for ASP.Net C# String Length Function

string stringVar = "Text value for string";


Above syntax can be used to get the length of the string associated with string type value returned by any control or variable.

Example of ASP.Net C# String Length Function

Example 1
strText = "C# string length function-123";

Response.Write(strText.Remove(strText.Length - 1) + "<br />");

Response.Write(strText.Remove(strText.Length - 2) + "<br />");

Response.Write(strText.Remove(strText.Length - 3) + "<br />");

Response.Write(strText.Remove(strText.Length - 4) + "<br />");

C# string length function-12

C# string length function-1

C# string length function-

C# string length function

Above C# example code shows the use of string length function with string remove function to remove the character of the string placed at last 4 indexes.

Example 2
Response.Write(strText.Insert(strText.Length - 4, " \"new string inserted\" "));

C# string length function "new string inserted" -123

Above C# example code shows the use of string length function with string insert function to insert the new string value at 4th position from the last index of the old string.


Learn with the help of live sample of C# String Length Function from the following link:

String Length Function

Continue to next tutorial: ASP.Net C# String Format Function to learn Number formatting and DateTime formatting using format specifiers with C# string format function.

0 Responses to "ASP.Net C# String Length Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email