Truncate String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69

    Truncate String

    The code below is suppose to work were if a user enters a value in the
    text box 12 characters long it will hit this loop and truncate the
    value to only 12 characters long....however this doesn;t happen...what
    do i need to do?

    If txtRightLabel.T ext.Substring(0 , 12) Then
    Dim Righttextlable As String
    Righttextlable = txtRightLabel.T ext
    txtRightLabel.T ext =
    Righttextlable. Substring(Right textlable.Lengt h - 12)
    End If
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Truncate String

    cmdolcet69 wrote:
    The code below is suppose to work were if a user enters a value in the
    text box 12 characters long it will hit this loop and truncate the
    value to only 12 characters long....however this doesn;t happen...what
    do i need to do?
    >
    If txtRightLabel.T ext.Substring(0 , 12) Then
    Two errors:

    1. The expression returns a string, which you use as a condition in the
    If statement.

    2. If the text is shorter than 12 characters you will get an exception
    when you try to get the first 12 characters.
    Dim Righttextlable As String
    Righttextlable = txtRightLabel.T ext
    txtRightLabel.T ext =
    Righttextlable. Substring(Right textlable.Lengt h - 12)
    This would not make the string 12 characters long, it would shorten the
    string by 12 characters.
    End If
    From your specification, this is what you want:

    If txtRightLabel.T ext.Length 12 Then
    txtRightLabel.T ext = txtRightLabel.T ext.Substring(0 , 12)
    End If

    or:

    txtRightLabel.T ext = txtRightLabel.T ext.Substring(0 ,
    Math.Min(txtRig htLabel.Text.Le ngth, 12))

    Or why not simply limit the text box to 12 characters?

    txtRightLabel.M axLength = 12

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • \(O\)enone

      #3
      Re: Truncate String

      Göran Andersson wrote:
      or:
      >
      txtRightLabel.T ext = txtRightLabel.T ext.Substring(0 ,
      Math.Min(txtRig htLabel.Text.Le ngth, 12))
      or:

      txtRightLabel.T ext = Strings.Left(tx tRightLabel.Tex t, 12)

      --

      (O)enone


      Comment

      Working...