---
title: "Is there a better way to format this string?"  
description: "Is there a better way to format this string?"  
author: "Manoj Bhatt"  
published: 2013-12-12  
updated: 2013-12-12  
canonical: https://www.mindstick.com/forum/1770/is-there-a-better-way-to-format-this-string  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Is there a better way to format this string?

Is there a better way to [format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-format) this [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)?

[var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) [Mac](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) = sdr.GetString(0);

string trimMac = Mac.Replace("-", "");

string formatMac = trimMac.Insert(4, ".");

string formatAgain = formatMac.Insert(9, ".");

string dudeWTF = formatAgain.Trim();

\
Please help.

## Replies

### Reply by Pravesh Singh

Hi Manoj,\
\

Why do something simple, when you can use a...

REGULAR EXPRESSION

```
private static readonly string _InnerPattern ="([a-zA-Z0-9]{2})";private static readonly string _Pattern =string.Format("{0}-{0}-{0}-{0}-{0}-{0}", _InnerPattern);private static readonly string _ReplacePattern ="$1$2.$3$4.$5$6";private static readonly Regex _TransformRegex = new Regex(_Pattern, RegexOptions.Compiled);public static string TransformMacAddressUsingRegex(string input){    return _TransformRegex.Replace(input, _ReplacePattern);}
```

The expression above creates a capture group for each set of characters separated by a hyphen "-", then constructs the output string using the contents of those capture groups.


---

Original Source: https://www.mindstick.com/forum/1770/is-there-a-better-way-to-format-this-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
