locked
How to put spaces in a string in an asp: label RRS feed

  • Question

  • User855106052 posted

    Hey guys,

    I have a label <asp: Label ID = "LblClientPhone" runat = "server"> </ asp: Label> that is filled in by a Code Behind ... I needed this label to have spaces every 3 numbers in a row. At this moment it presents 123456789 and I wanted it to show 123 456 789 to be easier to read. Any idea? I tried regex but it didn't work because I don't have the label on a repeater or datalist.

    To fill the label i'm using:

             Con = New OleDbConnection (StrCon)
             Dset = New DataSet ()
             Adpt = New OleDbDataAdapter ("SELECT * FROM TabClient WHERE TabClient.ClientCod =" & Request.QueryString ("cod"), Con)
             Adpt.MissingSchemaAction = MissingSchemaAction.AddWithKey
             Adpt.Fill (Dset, "AllContents")
             Drw = Dset.Tables ("AllContents"). Rows.Find (Request.QueryString ("cod"))
    LblClientPhone.Text = Drw ("ClientPhone"). ToString

    Thanks for the help

    Saturday, April 3, 2021 9:03 PM

Answers

  • User-1716253493 posted

    LblClientPhone.Text = String.Format("{0:N0}", Convert.ToDouble(Drw("ClientPhone"))).Replace(",", " ")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2021 7:48 AM

All replies

  • User409696431 posted

    There is nothing about regex that requires the label to be in a repeater or a datalist.

    You are fetching a string. Create a modified string and assign that as the value of the label.

    Show us the code you tried.

    Also, you don't need to use Regex if you don't want to.  You can also modify the string with String.Insert, especially if the value in the database has already been validated and you know it is always 9 characters.  You'd take your original string and .Insert(2," ").Insert(6," ") to get a new string with a space inserted after the 3rd character (index 2), resulting in a 10 character string, then a space inserted after the 7th character (index 6) in that string, resulting in an 11 character string.

    Sunday, April 4, 2021 3:30 AM
  • User-1545767719 posted

    indesk

    At this moment it presents 123456789 and I wanted it to show 123 456 789 to be easier to read. Any idea?

    Consider using the numeric format specifier (N) in the standard numeric format strings:

    Numeric format specifier (N)
    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#numeric-format-specifier-n

    See the following Microsoft document for a sample code:

    NumberFormatInfo.NumberGroupSeparator Property
    https://docs.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo.numbergroupseparator?view=net-5.0

    Sunday, April 4, 2021 4:04 AM
  • User-1716253493 posted

    LblClientPhone.Text = String.Format("{0:N0}", Convert.ToDouble(Drw("ClientPhone"))).Replace(",", " ")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 4, 2021 7:48 AM
  • User855106052 posted

    Perfect! that's exactly what I needed. I was actually using something similar but with regex and it wasn't working but that is perfect. Thanks

    Sunday, April 4, 2021 8:17 AM