String concatenation in c# - c#

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.

Related

C# - Add a whitespace between two strings

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.

Convert unformatted string (not valid json) to json

I have a string which I get from a webservice which looks like this:
({
id=1;
name="myName";
position="5";
})
which is not a parsable json. I wanted to ask if there are any ways besides going character to character and correcting them to convert such string into a parsable json like this:
{
"id":1,
"name":"myName",
"position":"5"
}
Check this link it will be helpful :
https://forum.unity3d.com/threads/json-web-services.366073/
You cold run a bunch of regex replaces for each change. But you'll need captures for the property names etc The performance will be horrible.
If the format is known and reliable (eg what happens with collections/arrays and sub-objects). And the service provider does not provide a client or SDK. Then your best bet is to write your own parser. It's not that hard to create your own from scratch. Or you can use a parser library like Irony.net or eto.parse. Both of these allow you to construct a grammar in c# so it is fully self contained without the need for compiler-compilers and generated code. There is also a class of parser called "monadic" parsers like Sprache which are of a simpler nature (once you wrap your head around them).
Whichever approach is taken you'll end up with a way of recognising each property and object boundary where you can do what you need to do: set a property; create a JToken; whatever...
Then you can wrap the whole lot in a MediaTypeFormatter and call the service via HttpClient and get objects out.
Finally I had to write my own function to convert it to a parsable json, here's the function I wrote:
public string convertToJson(string mJson)
{
mJson = mJson.Replace("(","[");
mJson = mJson.Replace(")","]");
string mJson2 = mJson.Trim('[',']');
string[] modules = mJson2.Split(',');
for(int i = 0;i<modules.Length;i++)
{
Debug.Log("module["+i+"]: " + modules[i]);
}
for(int m=0;m<modules.Length;m++)
{
char[] mCharacter = {'{','}'};
modules[m] = modules[m].Replace("{",string.Empty).Replace("}",string.Empty).Trim();
Debug.Log("module["+m+"] after trim: " + modules[m]);
string[] items = modules[m].TrimEnd(';').Split(';');
modules[m] = "{";
for(int j=0;j<items.Length;j++)
{
Debug.Log("item["+j+"]: " + items[j]);
string[] keyValue = items[j].Split('=');
Debug.Log("key,value: " + keyValue[0] + ", " + keyValue[1]);
modules[m] = modules[m] + "\"" + keyValue[0].Trim() + "\":" + keyValue[1].Trim() + ",";
}
modules[m] = modules[m].Substring(0,modules[m].Length-1) + "}";
Debug.Log("modules["+m+"] final: " + modules[m]);
}
string finalJson = "[";
for(int m=0;m<modules.Length;m++)
{
finalJson = finalJson + modules[m] + ",";
}
finalJson = finalJson.Substring(0,finalJson.Length-1) + "]";
Debug.Log("finalJson: " + finalJson);
return finalJson;
}

String declaration by character "A" + "B" vs "AB"

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";

Are there benefits to using string formatting versus string concatenation? [duplicate]

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}"

String concatenation doesn't seem to work in C#

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

Categories