2014-01-23 66 views
19

是否可以向字符串格式说明符中添加一些描述性文本?字符串格式描述文本

实施例:

string.Format ("{0:ForeName} is not at home", person.ForeName); 

在该示例ForeName被添加作为说明。

上面的语法显然是不正确的,但只是为了显示这个想法。

我问其原因,是因为在我的情况的字符串是在资源文件中,所以在资源文件中,您目前只能看到

{0} is not at home 
在某些情况下,这是很难

上把握上下文{0}是。

编辑:

在C#6串插与$运营商已经出台,所以string.Format不需要了:

$"{person.ForeName} is not at home"; 
+2

您希望正在阅读代码的开发人员知道“{0}”中发生了什么,就是这样吗?一种评论? – Johnny5

+1

这里有什么意图? – crush

+7

只是谷歌“字符串格式命名参数”。没有原生的方法,只有解决方法。 http://stackoverflow.com/questions/159017/named-string-formatting-in-c-sharp –

回答

14

我们通常会将评论放入我们的资源文件中,例如{0} = Forename

然后,任何人可能会翻译字符串知道什么{0}代表,并可以相应地翻译。

此外,如果您使用ReSharper,则可以在将字符串添加到资源时同时输入注释。

+0

谢谢,我使用了Resharper,所以很了解! – thumbmunkeys

+0

有时最好的解决方案是简单的 – Johnny5

2
string.Format ("{0} is not at home {1} ", person.ForeName, person.Something); 

这应打印用的名字,而不是{ 0}以及{1}中的内容。如你所说,没有办法。

+12

我认为作者理解'string.Format'是如何工作的。 – crush

+0

对不起,我的问题更清晰 – thumbmunkeys

5

这没有内置的C#函数。我建议最好是插入注释(这将在不影响性能):

string.Format ("{0"/*ForeName*/+"} is not at home", person.ForeName); 

Personnaly,我并不觉得可读,最好的计算策略是使用第三方工具大卫Khaykin (见this answer

+0

谢谢,但重点是描述应该在字符串中,所以它进入资源文件 – thumbmunkeys

+0

我喜欢这个“没有性能影响”部分。但作者正在谈论资源文件。我们可以在资源文件中添加注释吗? – Johnny5

+0

@ Johnny5可以给resx文件条目添加注释,所以这是一个可行的选项,只是在那里发表评论 – thumbmunkeys

8

对于字符串您的方法应该工作,因为字符串将忽略任何格式说明符。然而,你跑不小心使用,对于非字符串类型的风险,在这种情况下,字符串要么被翻译成格式代码或字面上显示:

string.Format ("{0:ForeName} is not at home", "Johnny"); 
//"Johnny is not at home" 

string.Format ("{0:ForeName} will be home at {1:HomeTime}", "Johnny", DateTime.Today) 
//Johnny will be home at 0o0eTi0e -- H, h, and m are DateTime format codes. 

但是因为你在一个资源存储这些文件,我会使用资源文件中的“注释”字段 - 您可以存储格式字符串的副本并在其中添加您的描述。

14

Phil Haack和Peli写了一些关于默认string.format函数替代方法的有趣博客文章。他们可能会感兴趣。

他们基本上允许您使用格式字符串像这里面的对象属性:

string s = NamedFormat("Hello {FullName} ({EmailAdrress})!", person); 

您可以在相关的博客文章在这里:

也许这些博客文章中涵盖的解决方案之一可以满足您的需求。

+0

1st是一个死链接 – iambriansreed

5

IDEOne.com demo

这是一个有点天真实施StackOverflow上的formatUnicorn方法:

using System; 
using System.Collections.Generic; 
using System.Text.RegularExpressions; 
using System.Reflection; 

public class Test 
{ 
    public static void Main() 
    { 
     string formatString = "{firstName} {lastName} is awesome."; 

     Console.WriteLine(formatString.FormatUnicorn(new { 
      firstName = "joe", 
      lastName = "blow" 
     })); 
    } 
} 

public static class StringExtensions { 
    public static string FormatUnicorn(this string str, object arguments) { 
     string output = str; 

     Type type = arguments.GetType(); 

     foreach (PropertyInfo property in type.GetProperties()) 
     { 
      Regex regex = new Regex(@"\{" + property.Name + @"\}"); 
      output = regex.Replace(output, property.GetValue(arguments, null).ToString()); 
     } 

     return output; 
    } 
} 

这里最大的缺点是使用反射,这可能是缓慢。另一个是它不允许使用格式说明符。

更好的方法可能是创建一个更复杂的正则表达式来删除注释。

1

从Visual Studio 2015开始,您可以使用Interpolated Strings(它是一种编译器技巧,因此您选择哪种版本的.net框架并不重要)。

然后,该代码看起来是这样的

string txt = $"{person.ForeName} is not at home {person.Something}"; 

它不是理想的,如果你想要把字符串到资源文件进行翻译,但它oftern使代码更易读且不易出错。