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. Advertising

  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. Advertising

  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. Advertising

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. Winanjaya Amidjojo

    Re: formatting text in textbox while typing

    Winanjaya Amidjojo, Jul 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    434
    Winanjaya Amidjojo
    Jul 3, 2003
  2. ald
    Replies:
    0
    Views:
    363
  3. avnrao
    Replies:
    1
    Views:
    675
    =?Utf-8?B?dG9ueQ==?=
    May 13, 2004
  4. Swanand Mokashi
    Replies:
    0
    Views:
    611
    Swanand Mokashi
    May 13, 2004
  5. Ian
    Replies:
    0
    Views:
    2,232
Loading...

Share This Page