Formatting text?

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?
 
H

Hans Kesting

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
 
E

Edwin Knoppert

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top