Formatting text?

Discussion in 'ASP .Net' started by Edwin Knoppert, Nov 9, 2006.

  1. I have a string like:

    "abcdefg123456"
    for readabillity i would like to insert a space at every 4th position like:
    "abc def g12 345 6"

    I can do this using % (mod) but isn't there an easier method?
     
    Edwin Knoppert, Nov 9, 2006
    #1
    1. Advertisements

  2. Edwin Knoppert

    Hans Kesting Guest

    > I have a string like:
    >
    > "abcdefg123456"
    > for readabillity i would like to insert a space at every 4th position like:
    > "abc def g12 345 6"
    >
    > I can do this using % (mod) but isn't there an easier method?


    You could try this:

    using System.Text.RegularExpressions;
    public static void Main()
    {
    string s = "abcdefghijklmnopqrstuvwxyz";
    MatchEvaluator myEvaluator = new MatchEvaluator(AddSpace);

    string s2 = Regex.Replace(s, ".{3}", myEvaluator);
    Console.WriteLine(s);
    Console.WriteLine(s2);

    }

    private static string AddSpace(Match m)
    {
    return m.ToString() + " ";
    }



    Hans Kesting
     
    Hans Kesting, Nov 9, 2006
    #2
    1. Advertisements

  3. Hah!

    ~as large as a mod loop but ok :)



    "Hans Kesting" <> schreef in bericht
    news:...
    >> I have a string like:
    >>
    >> "abcdefg123456"
    >> for readabillity i would like to insert a space at every 4th position
    >> like:
    >> "abc def g12 345 6"
    >>
    >> I can do this using % (mod) but isn't there an easier method?

    >
    > You could try this:
    >
    > using System.Text.RegularExpressions;
    > public static void Main()
    > {
    > string s = "abcdefghijklmnopqrstuvwxyz";
    > MatchEvaluator myEvaluator = new MatchEvaluator(AddSpace);
    >
    > string s2 = Regex.Replace(s, ".{3}", myEvaluator);
    > Console.WriteLine(s);
    > Console.WriteLine(s2);
    >
    > }
    >
    > private static string AddSpace(Match m)
    > {
    > return m.ToString() + " ";
    > }
    >
    >
    >
    > Hans Kesting
    >
    >
     
    Edwin Knoppert, Nov 9, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ald
    Replies:
    0
    Views:
    456
  2. avnrao
    Replies:
    1
    Views:
    758
    =?Utf-8?B?dG9ueQ==?=
    May 13, 2004
  3. Ian
    Replies:
    0
    Views:
    2,357
  4. Chris
    Replies:
    2
    Views:
    6,422
    Chris
    Oct 26, 2004
  5. Bart Schelkens

    formatting text using buttons

    Bart Schelkens, Dec 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    375
    Eliyahu Goldin
    Dec 9, 2004
  6. Rolf
    Replies:
    2
    Views:
    427
    Steve C. Orr [MVP, MCSD]
    Feb 7, 2005
  7. =?Utf-8?B?c21IYWln?=

    formatting text problem

    =?Utf-8?B?c21IYWln?=, Mar 2, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    566
    =?Utf-8?B?c21IYWln?=
    Mar 2, 2006
  8. leo
    Replies:
    1
    Views:
    482
    Bob Lehmann
    Dec 5, 2005
Loading...