在 C# 中声明一个 looooong 单行字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1565847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:56:12  来源:igfitidea点击:

Declaring a looooong single line string in C#

c#stringcoding-stylecode-formatting

提问by Cory

Is there a decent way to declare a long single line string in C#, such that it isn't impossible to declare and/or view the string in an editor?

是否有一种体面的方法可以在 C# 中声明一个长的单行字符串,以便在编辑器中声明和/或查看该字符串并非不可能?

The options I'm aware of are:

我知道的选项是:

1: Let it run. This is bad because because your string trails way off to the right of the screen, making a developer reading the message have to annoying scroll and read.

1:让它运行。这很糟糕,因为您的字符串会拖到屏幕右侧,使阅读消息的开发人员不得不烦人地滚动和阅读。

string s = "this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  this is my really long string.  ";

2: @+newlines. This looks nice in code, but introduces newlines to the string. Furthermore, if you want it to look nice in code, not only do you get newlines, but you also get awkward spaces at the beginning of each line of the string.

2:@+换行符。这在代码中看起来不错,但在字符串中引入了换行符。此外,如果您希望它在代码中看起来不错,不仅会得到换行符,而且还会在字符串的每一行的开头得到尴尬的空格。

string s = @"this is my really long string.  this is my long string.
             this line will be indented way too much in the UI. 
This line looks silly in code.  All of them suffer from newlines in the UI.";

3: "" + ...This works fine, but is super frustrating to type. If I need to add half a line's worth of text somewhere I have to update all kinds of +'s and move text all around.

3:"" + ...这很好用,但打字非常令人沮丧。如果我需要在某处添加半行文本,我必须更新各种 + 并四处移动文本。

string s = "this is my really long string.  this is my long string. " + 
           "this will actually show up properly in the UI and looks " +
           "pretty good in the editor, but is just a pain to type out " +
           "and maintain";

4: string.format or string.concat. Basically the same as above, but without the plus signs. Has the same benefits and downsides.

4: string.format or string.concat。与上面基本相同,但没有加号。具有相同的优点和缺点。

Is there really no way to do this well?

真的没有办法做好吗?

采纳答案by Ogre Psalm33

It depends on how the string is going to wind up being used. All the answers here are valid, but context is important. If long string "s" is going to be logged, it should be surrounded with a logging guard test, such as this Log4net example:

这取决于字符串将如何结束使用。这里的所有答案都是有效的,但上下文很重要。如果要记录长字符串“s”,则应将其括在日志保护测试中,例如此 Log4net 示例:

if (log.IsDebug) {
    string s = "blah blah blah" + 
    // whatever concatenation you think looks the best can be used here,
    // since it's guarded...
}

If the long string s is going to be displayed to a user, then Developer Art's answer is the best choice...those should be in resource file.

如果要向用户显示长字符串 s ,那么Developer Art的答案是最佳选择......那些应该在资源文件中。

For other uses (generating SQL query strings, writing to files [but consider resources again for these], etc...), where you are concatenating more than just literals, consider StringBuilder as Wael Dalloulsuggests, especially if your string might possibly wind up in a function that just may, at some date in the distant future, be called many many times in a time-critical application (All those invocations add up). I do this, for example, when building a SQL query where I have parameters that are variables.

对于其他用途(生成 SQL 查询字符串、写入文件 [但再次考虑这些资源] 等...),在连接不仅仅是文字的情况下,请按照Wael Dalloul 的建议考虑 StringBuilder ,特别是如果您的字符串可能会缠绕在一个函数中,它可能会在遥远的将来的某个日期在时间关键的应用程序中被多次调用(所有这些调用加起来)。例如,我在构建 SQL 查询时执行此操作,其中我的参数是变量。

Other than that, no, I don't know of anything that both looks pretty and is easy to type (though the word wrap suggestion is a nice idea, it may not translate well to diff tools, code print outs, or code review tools). Those are the breaks. (I personally use the plus-sign approach to make the line-wraps neat for our print outs and code reviews).

除此之外,不,我不知道任何看起来漂亮且易于输入的东西(尽管自动换行建议是个好主意,但它可能无法很好地转换为差异工具、代码打印输出或代码工具)。这些是休息时间。(我个人使用加号方法为我们的打印输出和代码使换行整齐)。

回答by Cory

There is a way. Put your very long string in resources. You can even put there long pieces of text because it's where the texts should be. Having them directly in code is a real bad practice.

有一种方法。把你很长的字符串放在资源中。您甚至可以在那里放置很长的文本,因为它应该是文本所在的位置。将它们直接放在代码中是一种非常糟糕的做法。

回答by Jimmeh

You could use StringBuilder

你可以使用 StringBuilder

回答by stevehipwell

If using Visual Studio

如果使用 Visual Studio

Tools > Options > Text Editor > All Languages > Word Wrap

I'm sure any other text editor (including notepad) will be able to do this!

我相信任何其他文本编辑器(包括记事本)都可以做到这一点!

回答by David Bo?jak

I either just let it run, or use string.format and write the string in one line (the let it run method) but put each of the arguments in new line, which makes it either easier to read, or at least give the reader some idea what he can expect in the long string without reading it in detail.

我要么让它运行,要么使用 string.format 并将字符串写在一行中(让它运行方法)但将每个参数放在新行中,这使其更易于阅读,或者至少给读者一些想法他可以在不详细阅读的情况下从长字符串中得到什么。

回答by d91-jal

Does it have to be defined in the source file? Otherwise, define it in a resource or config file.

是否必须在源文件中定义?否则,在资源或配置文件中定义它。

回答by Wael Dalloul

you can use StringBuilder like this:

您可以像这样使用 StringBuilder:

StringBuilder str = new StringBuilder();
str.Append("this is my really long string.  this is my long string. ");
str.Append("this is my really long string.  this is my long string. ");
str.Append("this is my really long string.  this is my long string. ");
str.Append("this is my really long string.  this is my long string. ");
string s = str.ToString();

You can also use: Text files, resource file, Database and registry.

您还可以使用:文本文件、资源文件、数据库和注册表。

回答by James

Personally I would read a string that big from a file perhaps an XML document.

就个人而言,我会从一个文件中读取一个那么大的字符串,也许是一个 XML 文档。

回答by tvanfosson

For really long strings, I'd store it in XML (or a resource). For occasions where it makes sense to have it in the code, I use the multiline string concatenation with the +operator. The only place I can think of where I do this, though, is in my unit tests for code that reads and parses XML where I'm actually trying to avoid using an XML file for testing. Since it's a unit test I almost always want to have the string right there to refer to as well. In those cases I might segregate them all into a #region directive so I can show/hide it as needed.

对于非常长的字符串,我会将其存储在 XML(或资源)中。对于在代码中使用它有意义的场合,我使用带有+运算符的多行字符串连接。不过,我唯一能想到的地方是在我对读取和解析 XML 的代码的单元测试中,我实际上试图避免使用 XML 文件进行测试。由于它是一个单元测试,我几乎总是希望在那里也有字符串可以引用。在这些情况下,我可能会将它们全部隔离到 #region 指令中,以便我可以根据需要显示/隐藏它。

回答by John Fisher

If you reallywant this long string in the code, and you really don't want to type the end-quote-plus-begin-quote, then you can try something like this.

如果您真的希望在代码中使用这个长字符串,并且您真的不想键入 end-quote-plus-begin-quote,那么您可以尝试这样的操作。

string longString = @"Some long string, 
    with multiple whitespace characters 
    (including newlines and carriage returns)
    converted to a single space
    by a regular expression replace.";

longString = Regex.Replace(longString, @"\s+", " ");