保留使用空格的字符串文字的代码结构 [英] Keeping code structure with string literal that uses whitespace

查看:86
本文介绍了保留使用空格的字符串文字的代码结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在想出搜索字词时遇到了一个奇怪的问题.如果我的程序中有多行字符串文字,是否可以在不向字符串文字添加多余空格的情况下使代码缩进保持一致?

So a bit of a weird question I was having trouble coming up with the search terms for. If I have a multi-line string literal in my program, is there anyway to keep the indentation of my code consistent without adding unwanted white space to my string literal?

例如:

if (true)
{
    if (!false)
    {
        //Some indented code;
        stringLiteral = string.format(
@"This is a really long string literal
I don't want it to have whitespace at 
the beginning of each line, so I have
to break the indentation of my program
I also have vars here 
{0}
{1}
{2}",
var1, var2, var3);
    }
}

这可能只是我的OCD交谈,但是无论如何都可以保持程序的缩进,而无需在字符串中添加不必要的空格,也不必逐行构建它(真正的字符串是超长字符串. 20〜行内有12个变量)?

It's probably just my OCD talking, but is there anyway to maintain the indentation of my program without adding unwanted whitespace to the string, or having to build it line by line (the real string is a super long string.format that is 20~ lines with 12 variables inside)?

推荐答案

我将其完全抽象为单独的静态类或资源:

I would abstract it to a separate static class or resource completely:

public static class MyStringResources
{
    public static readonly string StringLiteral = 
@"This {0} a really long string literal
I don't want {1} to have {2} at 
the beginning of each line, so I have
to break the indentation  of my program";

}

用法如下:

stringLiteral = String.Format(MyStringResources.StringLiteral, var1, var2, var3);

更好的是,通过这种方式,您可以拥有一个需要期望变量数量的好函数:

Even better, this way you can have a nice function that requires the number of expected variables:

public static class MyStringLiteralBuilder
{
    private static readonly string StringLiteral = 
@"This {0} a really long string literal
I don't want {1} to have {2} at 
the beginning of each line, so I have
to break the indentation  of my program";

    public static string Build(object var1, object var2, object var3)
    {
        return String.Format(MyStringResources.StringLiteral, var1, var2, var3);
    }
}

那么您就不会意外遗漏变量(甚至可能将其强力键入数字,布尔值等)

Then you can't miss variables accidentally (and possibly even strongly type them to numbers, booleans, etc.)

stringLiteral = MyStringLiteralBuilder.Build(var1, var2, var3);
stringLiteral = MyStringLiteralBuilder.Build(var1, var2); //compiler error!

当然,在这一点上,您可以使用这些构建器执行几乎所有您想做的事情.为您的程序中每个特殊的大"stringLiteral"创建一个新的生成器.也许不是让它们static而是可以是您可以获取/设置键属性的实例,然后您也可以给它们起一个漂亮的名字:

Of course at this point, you can do pretty much whatever you want with these builders. Make a new builder for each special big "stringLiteral" you have in your program. Maybe instead of having them static they can be instances that you can get/set the key properties, then you can give them nice names too:

public class InfoCardSummary
{
    public string Name { get; set; }
    public double Age { get; set; }
    public string Occupation { get; set; }

    private static readonly string FormattingString = 
@"This person named {0} is a pretty
sweet programmer. Even though they're only
{1}, Acme company is thinking of hiring
them as a {2}.";

    public string Output()
    {
        return String.Format(FormattingString, Name, Age, Occupation);
    }
}

var info = new InfoCardSummary { Name = "Kevin DiTraglia", Age = 900, Occupation = "Professional Kite Flier" };
output = info.Output();

这篇关于保留使用空格的字符串文字的代码结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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