none
string manipulation - adding spaces between number RRS feed

  • Question

  • Hi

    I have the need to modify an existing string by a) removing some characters from start and b) adding space between each number left.

    e.g.

    ID: 1234567890

    I want to remove ID: and put spaces between rest so would end up with.

    1 2 3 4 5 6 7 8 9 0

    The bit to remove at the beginning will always be the same e.g. ID: but the numbers will vary, it wall always been 10 digits however.

    Any ideas on best way to achieve this please?

    Thank you very much


    Darren Rose

    Tuesday, November 19, 2019 8:12 PM

Answers

  • Here is one idea where all numbers are 0-9 

    Dim originalValue = "ID: 1234567890"
    Dim newValue = String.Join(" ", Regex.
                                  Replace(originalValue, "[^0-9\-/]", "").
                                  ToCharArray().Select(Function(value) $"{value}").
                                  ToArray())
    Console.WriteLine(newValue)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 19, 2019 8:32 PM
    Moderator

All replies

  • Dim input = "ID: 1234567890"
    Dim output = String.Join(" ", input.Substring(4).AsEnumerable)
    

    Tuesday, November 19, 2019 8:31 PM
  • Here is one idea where all numbers are 0-9 

    Dim originalValue = "ID: 1234567890"
    Dim newValue = String.Join(" ", Regex.
                                  Replace(originalValue, "[^0-9\-/]", "").
                                  ToCharArray().Select(Function(value) $"{value}").
                                  ToArray())
    Console.WriteLine(newValue)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 19, 2019 8:32 PM
    Moderator
  • Thank you both very much!  - both solutions work perfectly, but Karens is more universal for me :)

    Darren Rose

    Tuesday, November 19, 2019 8:47 PM