是否有可能包括在一个字符串变量一个C#变量,而无需使用连接符? [英] Is it possible to include a C# variable in a string variable without using a concatenator?

查看:328
本文介绍了是否有可能包括在一个字符串变量一个C#变量,而无需使用连接符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道.NET 3.5 C#允许我们包括在一个字符串变量的变量,而不必使用+连接符(或的String.Format(),对于这个问题)。

Does .NET 3.5 C# allow us to include a variable within a string variable without having to use the + concatenator (or string.Format(), for that matter).

例如(在伪,我用一个$符号来指定变量):

For example (In the pseudo, I'm using a $ symbol to specify the variable):

DateTime d = DateTime.Now;
string s = "The date is $d";
Console.WriteLine(s);

输出:

日期是二○一一年四月十二日上午十一时56分39秒

The date is 4/12/2011 11:56:39 AM

修改

由于反应,建议的String.Format()的极少数,我只能假设,我原来的职位还不清楚,当我提到 ...(或的String.Format(),为此事)的。需要明确的是,我很清楚的的String.Format()方法。然而,在我的具体项目,我的工作,的String.Format()不帮我(它实际上逊于+置器)。

Due to the handful of responses that suggested string.Format(), I can only assume that my original post wasn't clear when I mentioned "...(or string.Format(), for that matter)". To be clear, I'm well aware of the string.Format() method. However, in my specific project that I'm working on, string.Format() doesn't help me (it's actually worse than the + concatenator).

另外,我推断,大多数/所有你想知道什么的背后我的问题的动机是(我想我有同样的感觉,如果我读我的问题是是)。

Also, I'm inferring that most/all of you are wondering what the motive behind my question is (I suppose I'd feel the same way if I read my question as is).

如果你是好奇之一,这里是它的短:

If you are one of the curious, here's the short of it:

我创建一个Web应用程序在Windows CE设备上运行。由于Web服务器是如何工作的,我创建了整个网页内容(CSS,JS,HTML等)内的一个字符串变量。例如,我的.cs管理code可能是这样的:

I'm creating a web app running on a Windows CE device. Due to how the web server works, I create the entire web page content (css, js, html, etc) within a string variable. For example, my .cs managed code might have something like this:

string GetPageData()
    {
    string title = "Hello";
    DateTime date = DateTime.Now;

    string html = @"
    <!DOCTYPE html PUBLIC ...>
    <html>
    <head>
        <title>$title</title>
    </head>
    <body>
    <div>Hello StackO</div>
    <div>The date is $date</div>
    </body>
    </html>
    ";

}

正如你所看到的,不必而不需要连接指定一个变量的能力,使事情变得更简单 - 特别是当内容的大小增加

As you can see, having the ability to specify a variable without the need to concatenate, makes things a bit easier - especially when the content increases in size.

推荐答案

差不多,一个小扩展方法。

Almost, with a small extension method.

static class StringExtensions
{
    public static string PHPIt<T>(this string s, T values, string prefix = "$")
    {
        var sb = new StringBuilder(s);
        foreach(var p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            sb = sb.Replace(prefix + p.Name, p.GetValue(values, null).ToString());
        }
        return sb.ToString();
    }
}

现在我们可以这样写:

And now we can write:

string foo = "Bar";
int cool = 2;

var result = "This is a string $foo with $cool variables"
             .PHPIt(new { 
                    foo, 
                    cool 
                });

//result == "This is a string Bar with 2 variables"

这篇关于是否有可能包括在一个字符串变量一个C#变量,而无需使用连接符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆