Declaring a looooong single line string in C#

那年仲夏 提交于 2019-12-03 14:28:11

问题


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?


回答1:


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 (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.

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).




回答2:


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.




回答3:


If using 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!




回答4:


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




回答5:


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.

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+", " ");



回答6:


you can use StringBuilder like this:

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.




回答7:


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




回答8:


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.




回答9:


You could use StringBuilder




回答10:


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.




回答11:


Use the Project / Properties / Settings from the top menu of Visual Studio. Make the scope = "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;



来源:https://stackoverflow.com/questions/1565847/declaring-a-looooong-single-line-string-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!