Instead of
string someString = "AB";
I stumbled upon
string someString = "A" + "B";
Is there any technical explanation which renders the second way more advantageous?
var answer = "The only thing advantageous is readability, if you have large " +
"amounts of text it can be useful to break up the text inside the " +
"source code for better readability. The compiler will turn it " +
"in to a single string at compile time anyway";
Related
I have code to display a vehicle by its Make and Model.
productName.Text = p.Make + p.Model
The above code displays the text as such: "BentleyContinental", how can I make the text display as such "Bentley Continental".
You can use string.Format():
productName.Text = string.Format("{0} {1}", p.Make, p.Model);
Or you can use string interpolation (if you are on C# 6 or higher):
productName.Text = $"{p.Make} {p.Model}";
Or you can do just as you have (string concatenation) but add in a space:
productName.Text = p.Make + " " + p.Model;
Use the string.concat method to concatenate string parts.
productName.Text = string.concat(p.Make, " ", p.Model);
In general, you use the string.concat when you know you'll add less than a dozen parts. More than that, or if you are in a loop, there is more benefits using the StringBuilder class.
productName.Text = p.Make + " " + p.Model
Just concatenate a space between two words. I think this is the easiest way.
One of my page uses jQuery in *.cs file as follows. But I heard that string concatenation will reduce the performance. I can not write it in page (ie in *.aspx) because I am using UpdatePanel, which wipe out all client code. Is there any other alternative method ? How about StringBuilder?
The code is in MyTestPage.aspx.cs and strings are concatenated using +
// Function to be called by jQuery
#"function ddlAssignCaseTo_SelectIndexChanged() {
var value = $('#" + ddlAssignCaseTo.ClientID + #"').val();
value == '1' ? $('#" + divAction.ClientID + #"').show() : $('#" + divAction.ClientID + #"').hide();
}
function ddlReviewedBy_SelectIndexChanged() {
var value = $('#" + ddlReviewedBy.ClientID + #"').val();
value == '0'
? $('#" + divReviewee.ClientID + #"').hide()
: $('#" + divReviewee.ClientID + #"').show();
value == '0'
? $('#" + lblIn.ClientID + #"').hide()
: $('#" + lblIn.ClientID + #"').show();
}"
That's Javascript - are you building that up somehow in C#?
Anyway, if you aren't concatenating the string within a loop or something then the overhead of creating a StringBuilder is not worth it. A rule of thumb I've seen often cited is to change to a StringBuilder when you have more than 8x concats - but I've seen more benchmarks which suggest that it is more than this.
Remember that inline concatenations will be optimised out anyway:
string s = "string1" + "string2";
Is no slower than:
string s = "string1string2";
In this case you should use String.Format("#{0}", ddlAssignCaseTo.ClientID) as this uses StringBuilder under the hood but allows you to keep your code concise.
You should certainly try and avoid concatenating strings for all the answers provided.
You could do worse than use StringBuilder which is designed for this very reason.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C# String output: format or concat?
what is the benefit of using this:
Console.WriteLine("{0}: {1}", e.Code, e.Reason);
VS. this:
Console.WriteLine(e.Code + ": " + e.Reason);
????
The reason I always use .Format() is for readability and consequently bug reduction and maintenance benefits. Of course this makes no sense if you're relatively new to code as the second example is more intuitive at first. The first appears needlessly complex.
However if you choose the second pattern then you start to have problems when you have lots of variables. This is where it is easier to appreciate the benefits of the first pattern.
e.g
Console.Writeline(var1 + " " + var2 + "-" + var3 +
" some sort of additional text" + var4);
Note the bug: I need another space after "text" but that's not easy to see in this example.
However if I do it the other way:
Console.Writeline("{0} {1}-{2} some sort of additional text{3}",
var1, var2, var3, var4)
It's clearer to see what's going on. Its easier to appreciate the final result when you split the formatting from the variables that are going to be used.
If we want to think even further long term then it helps with globalisation/customisation. If we put those format strings into config we can then change the formatting or ordering of the variables without touching the code.
For me, the benefits of the string.Format pendant are:
Improved readability
Better translatable
From a performance perspective, I did never do any measurements; it could well be that the concatenation is faster then the string.Format pendant.
Practically, the only difference is that the first allows you to control the layout
string.Format("*{0:3}*",1); // * 1*
or control formatting:
string.Format("*{0:c}*",1); // *$1.00*
The performance of concatenation can be considered when doing lots of it in tight loops, in which case StringBuilder.Append and StringBuilder.AppendFormat are both much preferred. AppendFormat and string.Format are very close, performance-wise, so there is no need to substitute the second for the first.
It boils down to "When is it better to use String.Format vs string concatenation".
See this question for the answer:
When is it better to use String.Format vs string concatenation?
As strings are immutable (cant change an existing string must create a new one each time) string format can have performance benefits as it wont create as many memory references.
result = string1 + " " + string2 + " " + string3
This creates 10 references
result
string1
string2
string3
" "
" "
string1 + " "
string1 + " " + string2
string1 + " " + string2 + " "
string1 + " " + string2 + " " + string 3
result = string.Format("{0} {1} {2}", string1, string2, string3)
This creates 5 references
result
string1
string2
string3
"{0} {1} {2}"
In .NET I can provide both \r or \n string literals, but there is a way to insert
something like "new line" special character like Environment.NewLine static property?
Well, simple options are:
string.Format:
string x = string.Format("first line{0}second line", Environment.NewLine);
String concatenation:
string x = "first line" + Environment.NewLine + "second line";
String interpolation (in C#6 and above):
string x = $"first line{Environment.NewLine}second line";
You could also use \n everywhere, and replace:
string x = "first line\nsecond line\nthird line".Replace("\n",
Environment.NewLine);
Note that you can't make this a string constant, because the value of Environment.NewLine will only be available at execution time.
If you want a const string that contains Environment.NewLine in it you can do something like this:
const string stringWithNewLine =
#"first line
second line
third line";
EDIT
Since this is in a const string it is done in compile time therefore it is the compiler's interpretation of a newline. I can't seem to find a reference explaining this behavior but, I can prove it works as intended. I compiled this code on both Windows and Ubuntu (with Mono) then disassembled and these are the results:
As you can see, in Windows newlines are interpreted as \r\n and on Ubuntu as \n
var sb = new StringBuilder();
sb.Append(first);
sb.AppendLine(); // which is equal to Append(Environment.NewLine);
sb.Append(second);
return sb.ToString();
One more way of convenient placement of Environment.NewLine in format string.
The idea is to create string extension method that formats string as usual but also replaces {nl} in text with Environment.NewLine
Usage
" X={0} {nl} Y={1}{nl} X+Y={2}".FormatIt(1, 2, 1+2);
gives:
X=1
Y=2
X+Y=3
Code
///<summary>
/// Use "string".FormatIt(...) instead of string.Format("string, ...)
/// Use {nl} in text to insert Environment.NewLine
///</summary>
///<exception cref="ArgumentNullException">If format is null</exception>
[StringFormatMethod("format")]
public static string FormatIt(this string format, params object[] args)
{
if (format == null) throw new ArgumentNullException("format");
return string.Format(format.Replace("{nl}", Environment.NewLine), args);
}
Note
If you want ReSharper to highlight your parameters, add attribute to the method above
[StringFormatMethod("format")]
This implementation is obviously less efficient than just String.Format
Maybe one, who interested in this question would be interested in the next question too:
Named string formatting in C#
string myText =
#"<div class=""firstLine""></div>
<div class=""secondLine""></div>
<div class=""thirdLine""></div>";
that's not it:
string myText =
#"<div class=\"firstLine\"></div>
<div class=\"secondLine\"></div>
<div class=\"thirdLine\"></div>";
If you really want the New Line string as a constant, then you can do this:
public readonly string myVar = Environment.NewLine;
The user of the readonly keyword in C# means that this variable can only be assigned to once. You can find the documentation on it here. It allows the declaration of a constant variable whose value isn't known until execution time.
static class MyClass
{
public const string NewLine="\n";
}
string x = "first line" + MyClass.NewLine + "second line"
newer .net versions allow you to use $ in front of the literal which allows you to use variables inside like follows:
var x = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
If you are working with Web application you can try this.
StringBuilder sb = new StringBuilder();
sb.AppendLine("Some text with line one");
sb.AppendLine("Some mpre text with line two");
MyLabel.Text = sb.ToString().Replace(Environment.NewLine, "<br />")
If I understand the question: Couple "\r\n" to get that new line below in a textbox. My example worked -
string s1 = comboBox1.Text; // s1 is the variable assigned to box 1, etc.
string s2 = comboBox2.Text;
string both = s1 + "\r\n" + s2;
textBox1.Text = both;
A typical answer could be s1
s2 in the text box using defined type style.
I like more the "pythonic way"
List<string> lines = new List<string> {
"line1",
"line2",
String.Format("{0} - {1} | {2}",
someVar,
othervar,
thirdVar
)
};
if(foo)
lines.Add("line3");
return String.Join(Environment.NewLine, lines);
Here, Environment.NewLine doesn't worked.
I put a "<br/>" in a string and worked.
Ex:
ltrYourLiteral.Text = "First line.<br/>Second Line.";
I don't know what is wrong with the following string:
"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
I can't get the concatenated string. I am getting Report(29-Dec-2009. That's all and
the rest gets left out from the string.
What is the reason?
Try this:
string filename =
String.Format(
"Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
System.DateTime.Now, System.DateTime.Now.AddMonths(-1));
EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:
filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";
Seems some browsers doesn't handles whitespaces very nicely: Filenames with spaces are truncated upon download. Please check it you can to download other filenames with whitespace in other sites.
You need to assign it to something:
string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
Update: I just saw your update to the question. How are you displaying the string? I'm guessing that you are displaying it in a GUI and the label is too short to display the complete text.
Try this:
string newstring =
string.Format(
"Report ({0} to {1})",
System.DateTime.Now.ToString("dd-MMM-yyyy"),
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
);
What are you assigning the result to? It would be easier to read the code if you used string.Format
You are not assigning the concatenated result to anything, so can't use it:
string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";
Using this code...
string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";
I saw the following result.
Report(29-Dec-2009 to 29-Nov-2009)
It could be that the string is being truncated later on. Make sure that you set a breakpoint right after this code is run and check the value of the variable to which it is assigned (test in my case).
If, as in your previous question, you are using this value to create a file, it may be that it's the space before "to" that is causing the problem. Try to use:
"Report("
+ System.DateTime.Now.ToString("dd-MMM-yyyy")
+ "To"
+ System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
+ ")"
instead and see if that fixes it.
If that does fix it, you'll probably need to either figure out how to quote the entire file name so it's not treated as the three separate arguments, "Report(29-Dec-2009", "to" and "29-Nov-2009)". Or simply leave your reports names without spaces.
I'd choose the latter but then I'm fundamentally opposed to spaces in filenames - they make simple scripts so much harder to write :-)