locked
RTrim or Truncate function RRS feed

  • Question

  • User1065545921 posted

    How do I go about truncating last character off of a string.   I have a page where associates enter in their bank routing number which is a 9 digit number, but I want to trunctate the last digit of the string before passing it to my SQL table.    I have a Private sub I use to pass that data into my SQL Table, so I was thinking I would somehow use the RTrim or STRLen function where I am passing Routing Number below.

    Private Sub InsertBankInfo(ByVal RoutingNumber As String, ByVal AccountNumber As String, _

    ByVal AccountType As String, ByVal AmtofDeposit As String, ByVal Amount As Integer)

    Dim prmCol As New DbParameterCollection

    Dim prmPersonID As New DbParameter()

    prmPersonID.ParameterName = "@Person_ID"

    prmPersonID.DbType = DbType.Int32

    prmPersonID.Value =
    MyBase.PersonID

    prmCol.Add(prmPersonID.ParameterName, prmPersonID)

     

    Dim prmCompany As New DbParameter()

    prmCompany.ParameterName = "@Company"

    prmCompany.DbType = DbType.String

    prmCompany.Value =
    MyBase.Company.ToString.Trim.PadLeft(4, "0")

    prmCol.Add(prmCompany.ParameterName, prmCompany)

     

     

    Dim prmRoutingNumber As New DbParameter()

    prmRoutingNumber.ParameterName = "@bank_rout"

    prmRoutingNumber.DbType = DbType.String

    prmRoutingNumber.Value = RoutingNumber  (NEED TO TRUNCATE THE VALUE)

    prmCol.Add(prmRoutingNumber.ParameterName, prmRoutingNumber)

    Tuesday, March 31, 2009 12:23 PM

Answers

  • User-1360095595 posted

    Try this:

    string val = "123456789";
    val = val.Substring(0, 8).PadLeft(9, '0');
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 8:45 AM

All replies

  • User-1360095595 posted

    You should be able to truncate using the SubString method.

    Tuesday, March 31, 2009 12:45 PM
  • User1065545921 posted

    If I use substring would I put something like this...

    prmRoutingNumber.DbType = DbType.String.ToString.Substring(0, 8)

    The user will be inputting a 9 character number, but I only want to pas the first 8 numbers, so I am not sure if I have that substring correct.   I tried this and kept getting error

    'Index and length must refer to a location within the string.
    Parameter name: length'

     

     

    Tuesday, March 31, 2009 3:24 PM
  • User-1360095595 posted
    string val = "123456789";
    val = val.Substring(0, 8);
     Refer: http://msdn.microsoft.com/en-us/library/aka44szs.aspx
    Wednesday, April 1, 2009 6:59 AM
  • User1065545921 posted

    That worked out great.   I was just informed that i know need to pad that number with a preceeding zero, so I need to pad a zero to the front of that number, while also truncating the last digit.   Could I do both those functions in 1 substring.

     Example:   if user enters in 123456789,  I need it to look like 01234567

    Wednesday, April 1, 2009 8:21 AM
  • User-1360095595 posted

    Try this:

    string val = "123456789";
    val = val.Substring(0, 8).PadLeft(9, '0');
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 8:45 AM