您的位置:首页 > 编程语言 > C#

如何在string.Format方法中输出大括号({})

2012-02-12 12:52 489 查看
C#(a.cs)

using System;

public class Test
{
public void Middle(string start, string end)
{
string pat1 = string.Format("(?<={0}).*(?={1})", start, end);
Console.WriteLine(pat1);

string pat2 = string.Format("(?<={{0}}).*(?={{1}})", start, end);
Console.WriteLine(pat2);

string pat3 = string.Format("(?<={{{0}}}).*(?={{{1}}})", start, end);
Console.WriteLine(pat3);
}

public static void Main(string[] args)
{
string start = "12";
string end = "35";
Test t = new Test();

t.Middle(start, end);
}
}


编译运行:

E:\>csc a.cs
Microsoft(R) Visual C# 2010 编译器 4.0.30319.1 版
版权所有(C) Microsoft Corporation。保留所有权利。

E:\>a.exe
(?<=12).*(?=35)
(?<={0}).*(?={1})
(?<={12}).*(?={35})

E:\>

结论: {{ 得到
{
, }} 得到 }

更多信息可参考:http://msdn.microsoft.com/zh-cn/library/txafckwd.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息