在 C# 中声明一个冗长的单行字符串
是否有一种在 C# 中声明长单行字符串的好方法,这样就可以在编辑器中声明和/或查看该字符串了?
我知道的选项是:
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:@+换行符。这在代码中看起来不错,但会在字符串中引入换行符。此外,如果你希望它在代码中看起来不错,你不仅会得到换行符,而且还会在字符串的每一行的开头得到尴尬的空格。
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: "" + ...
这工作正常,但输入起来非常令人沮丧。如果我需要在某处添加半行文本,我必须更新各种 + 并四处移动文本。
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 或 string.concat
。与上面基本相同,但没有加号。具有相同的优点和缺点。
难道真的没有办法做好这件事吗?
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?
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.
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.
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.
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.
Is there really no way to do this well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
就我个人而言,我会从文件(也许是 XML 文档)中读取这么大的字符串。
Personally I would read a string that big from a file perhaps an XML document.
你可以使用 StringBuilder
You could use StringBuilder
对于很长的字符串,我会将其存储在 XML(或资源)中。对于在代码中使用它有意义的情况,我使用带有
+
运算符的多行字符串连接。不过,我唯一能想到的地方是在读取和解析 XML 的代码的单元测试中,我实际上试图避免使用 XML 文件进行测试。由于这是一个单元测试,我几乎总是希望有该字符串可供参考。在这些情况下,我可能会将它们全部隔离到 #region 指令中,以便我可以根据需要显示/隐藏它。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.我要么让它运行,要么使用 string.format 并将字符串写在一行中(让它运行方法),但将每个参数放在新行中,这使得它更容易阅读,或者至少给读者无需详细阅读,就知道他可以在长字符串中期待什么。
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.
使用 Visual Studio 顶部菜单中的
Project / Properties / Settings
。使范围=“应用程序”
。在“值”框中,您可以输入很长的字符串,并且作为奖励,换行符会被保留。然后您的代码可以像这样引用该字符串:
string sql = Properties.Settings.Default.xxxxxxxxxxxxx;
Use the
Project / Properties / Settings
from the top menu of Visual Studio. Make thescope = "Application"
.In the Value box you can enter very long strings and as a bonus line feeds are preserved. Then your code can refer to that string like this:
string sql = Properties.Settings.Default.xxxxxxxxxxxxx;
有一个办法。将很长的字符串放入资源中。您甚至可以在那里放置长文本,因为这是文本应该放置的位置。直接将它们放在代码中是一种非常糟糕的做法。
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.
如果您确实想要在代码中使用这个长字符串,并且您确实不想键入 end-quote-plus-begin-quote,那么您可以尝试这样的操作。
If you really want 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.
如果使用 Visual Studio,
我确信任何其他文本编辑器(包括记事本)都能够做到这一点!
If using Visual Studio
I'm sure any other text editor (including notepad) will be able to do this!
这取决于字符串最终将如何使用。这里的所有答案都是有效的,但上下文很重要。如果要记录长字符串“s”,则应该用日志保护测试包围它,例如这个 Log4net 示例:
如果要向用户显示长字符串 s,则Developer Art 的答案是最好的选择...那些应该在资源文件中。
对于其他用途(生成 SQL 查询字符串、写入文件[但要再次考虑这些资源]等),您要连接的不仅仅是文字,请考虑 StringBuilder,如 Wael Dalloul 建议的那样,特别是如果您的字符串可能最终出现在一个函数中,而该函数可能在遥远的将来的某个日期在时间关键的应用程序中被多次调用(所有这些调用加起来)。例如,在构建 SQL 查询时,我会执行此操作,其中参数是变量。
除此之外,不,我不知道有什么东西既看起来漂亮又易于输入(尽管自动换行建议是个好主意,但它可能无法很好地转换为 diff 工具、代码打印输出或代码审查工具) )。这些就是休息时间。 (我个人使用加号方法使换行整齐,以便打印输出和代码审查)。
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:
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.
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 Dalloul suggests, 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.
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).
您可以像这样使用 StringBuilder:
您还可以使用:文本文件、资源文件、数据库和注册表。
you can use StringBuilder like this:
You can also use: Text files, resource file, Database and registry.
必须在源文件中定义吗?否则,在资源或配置文件中定义它。
Does it have to be defined in the source file? Otherwise, define it in a resource or config file.