0

I need to improve the Presentation and readability of a huge string in my code for a multiline string verification from my system using C# and I found some options:

1- One of them is using verbatim string literal, but they can't insert identation:

string target = @"
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Morbi metus massa, dapibus et lorem sed, eleifend viverra urna.
Integer ut nulla ac metus sodales egestas at quis erat.
Ut placerat magna vel enim molestie faucibus.";

2- The other one is using normal string with the explicit breaks and new lines:

string target= " Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Morbi metus massa, dapibus et lorem sed, eleifend viverra urna.\n Integer ut nulla ac metus sodales egestas at quis erat.\n Ut placerat magna vel enim molestie faucibus.";

But in my case is a huge text and it will produce a long long line.

3- The last one is using normal string with concatenation:

string target = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n "+
    "Morbi metus massa, dapibus et lorem sed, eleifend viverra urna.\n "+
    "Integer ut nulla ac metus sodales egestas at quis erat.\n "+
    "Ut placerat magna vel enim molestie faucibus.\n ";

This seems very useful because finally is possible to ident and multiline, but looks so ugly.

Does somebody have another idea?

  • Can't you just replace \t, \r and \n in there, remove spaces, convert to upper (or lower) and compare? – briba Feb 11 '22 at 14:24
  • Or even do some kind of normalization and calculating similarity between 2 words? https://stackoverflow.com/questions/9453731/how-to-calculate-distance-similarity-measure-of-given-2-strings – briba Feb 11 '22 at 14:25
  • You can indent option 1 also. (if it is just for SQL code it does not matter for SQL server) – Magnus Feb 11 '22 at 14:27
  • Depending on the context, a stringbuilder may be useful. – Andrew Corrigan Feb 11 '22 at 14:27
  • What is the question? Presentation and readability of a big ass string ? Or Comparaison of 2 string ? – Drag and Drop Feb 11 '22 at 14:29
  • Hi @briba the problem is about how is shown on the code, not the comparison itself. I'll edit my question. Txs a lot for the comment, I'll take a look on the suggestions. – thiago.araujo Feb 11 '22 at 14:50
  • Hi @Magnus I improve my question, this is a text compare, not an specific for SQl query. – thiago.araujo Feb 11 '22 at 14:53
  • @DragandDrop yes!! You got it! I'll change my question with your interpretation! – thiago.araujo Feb 11 '22 at 14:57
  • 2
    I wonder about the merits of having the string be burned into the code like that. I'm always inclined to add large strings into my project as text files and then use the build system to transform the text file into whatever I need at compile time. Usually such user-facing strings (which often need to be localized anyway) are not needed at compile time. So I'd rethink having that text be part of your C# code at all. Maybe read the file at run-time, or even having a string resource would be ok (depending on what kind of app this is). – Wyck Feb 11 '22 at 15:04
  • It will really depends on what those string are. If it's a Error message, you will almost never wrap it and read it. Perhaps once in code review to see if any spelling mistake. – Drag and Drop Feb 14 '22 at 10:17
  • If there is string interpolation or variable concatenation. Wrap may be important. You may want to have those thing happend in the readable part of the screen. But once again for error message, it can happend outside of the readable screen. It can just be so global variable and stack thing that you don't really care about. If the long String is a Sql query, it has to be in the readable space. And tools like ReShaper may help you warping it https://www.jetbrains.com/help/resharper/Reference__Options__Languages__CSharp__Formatting_Style__Line_Breaks_and_Wrapping.html – Drag and Drop Feb 14 '22 at 10:17

0 Answers0