Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Formatting text?

Reply
Thread Tools

Formatting text?

 
 
Edwin Knoppert
Guest
Posts: n/a
 
      11-09-2006
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?


 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      11-09-2006
> 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


 
Reply With Quote
 
 
 
 
Edwin Knoppert
Guest
Posts: n/a
 
      11-09-2006
Hah!

~as large as a mod loop but ok



"Hans Kesting" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
>> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird formatting problems appearing only in Linux version of FireFox news@celticbear.com Firefox 5 12-30-2004 03:40 PM
Formatting Question Markus Firefox 13 10-23-2004 08:17 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
Re: Formatting string in WHERE clause Lee Simpson ASP .Net 0 07-18-2003 04:21 PM
Code formatting rules in editor thaneesh ASP .Net 0 07-02-2003 09:04 AM



Advertisments