Formatting text?

  • Thread starter Thread starter Edwin Knoppert
  • Start date Start date
E

Edwin Knoppert

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?
 
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
 
Hah!

~as large as a mod loop but ok :)



Hans Kesting said:
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
 
Back
Top