#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Adding dashes to strings


    I need to convert numeric values of a certain number of characters into hyphenated versions of the same number.

    For instance 98765 needs to be modified to 98-765, where 12345 would be modified to 12-345.

    For the same number of characters, the dashes will always be in the same position(s), so
    NNNNNNNN will always be NNN-NNN-NN
    NNNNNNN will always be NNN-NN-NN
    NNNNNN will always be NNN-NNN
    etc.

    I could hard code this (for a Java/JSF application), but I would like to be able to make this configurable in the future, hence I am looking at regex or other options.

    I want to use a portable language too, since this may be used outside my JSF application in unknown languages/environments.

    Any advice on how to do this with regex and/or other standard & widely available string manipulation language?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    I do not know how to do it in Java, but since most modern regex implementations derive from Perl, it might give you some help if I show you how I might do it in Perl:

    Code:
    s/(\d{3})/$1-/g;

    Comments on this post

    • ManiacDan agrees

IMN logo majestic logo threadwatch logo seochat tools logo