Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Remove the last character from a string

From Wiki

Jump to: navigation, search

Summary: Remove the last character from a string

There may be times when you have built up a delimited string of character or words, and then needed to remove the last character/delimiter from the end of the string.

Here's a simple function that you can just pass your string to and it will return the string with the last character removed:

  1. Imports System.Text.RegularExpressions
  2. Partial Class Default1
  3.     Inherits System.Web.UI.Page
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.         Response.Write(RemoveLastCharacter("1,2,3,4,5,"))
  7.     End Sub
  8.  
  9.     Private Function RemoveLastCharacter(ByVal Text As String) As String
  10.         Return Text.Substring(0, Text.Length - 1)
  11.     End Function
  12. End Class

Output:

  1. 1,2,3,4,5


This Hack is part of the ASP.NET Hacks collection

482 Rating: 0.0/5 (0 votes cast)