English 中文(简体)
c#中与reg体相加的格式
原标题:Format string with regex in c#
  • 时间:2012-04-23 18:36:04
  •  标签:
  • c#
  • regex

我愿作这样的描述。

BPT4SH9R0XJ6

想像这一点

BPT4-SH9R-0XJ6

扼杀永远是12封信和数目的组合。

任何建议都将受到高度赞赏。

最佳回答

Try Regex.Replace (input, @" (w{4})(w{4})(w{4}”), @“$1-$2-$3”;

Regex is often derided, but is a pretty neat way of doing what you need. Can be extended to more complex requirements that are difficult to meet using string methods.

问题回答

您可使用<代码>({4})({4})({4})“作为你的表述和 1-$2-$3”作为替代。 然而,这几乎不是一种很好的用途:你可以更容易地使用<>。

var res = s.Substring(0,4)+"-"+s.Substring(4,4)+"-"+s.Substring(8);

如果该规则总是分为四大类的三组,则无需作例改动:

str.Substring(0,4) + "-" + str.Substring(4,4) + "-" + str.Substring(8,4)

似乎将<代码>合并起来。 String.Concat and string. 替代标准/代码应当考虑到你需要的一切。

  var str = "BPT4SH9R0XJ6";
  var newStr = str.Substring(0, 4) + "-" + str.Substring(4, 4) + "-" + str.Substring(8, 4);

你们想要做什么? 您仅可插入hy子:

string s = "BPT4SH9R0XJ6";
for(int i = 4; i < s.length; i = i+5)
    s = s.Insert(i, "-");

这将使每4个特性都加上phen子,如果扼杀太短/太长/太短,不会错。

return original_string.SubString(0,4)+"-"+original_string.SubString(4,4)+"-"+original_string.SubString(8,4);
string str = @"BPT4SH9R0XJ6";
string formattedString = string.Format("{0}-{1}-{2}", str.Substring(0, 4), str.Substring(4,4), str.Substring(8,4));

这种做法有很长的路要走:

            for (int i = 0; i < (int)Math.Floor((myString.Length - 1) / 4d); i++)
            {
                myString = myString.Insert((i + 1) * 4 + i, "-");
            }

采用这一方法的末级

var original = "BPT4SH9R0XJ6".ToCharArray();

var first = new string(original, 0, 4);
var second = new string(original, 4, 4);
var third = new string(original, 8, 4);
var mystring = string.Concat(first, "-", second, "-", third);

成就

如果你们得到保证的话,你根据《12个性法典》重新运作,那么你为什么不只使用替代物? 为什么你们需要雷克斯?

String theString = "AB12CD34EF56";
String theNewString = theString.Substring(0, 4) + "-" + theString.Substring(4, 4) + "-" + theString.Substring(8, 4); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...