|
What is the best way to contact three strings.
Method1
string key = "test" +"::"+ "test";
Method2
sting key = String.Format("{0}::{1}","test","test");
Is there any memory enhancement in using the second method?
Thanks!
Charith Jayasundara
|
|
|
|
|
If you are going to concat many string then use StringBuilder class as it will perform better. In my opinion String.format is better option as the code is more readable.
By the way, String.Format uses StringBuilder under the hood.
|
|
|
|
|
u can use stringbuffer also
rahul
|
|
|
|
|
StringBuffer? Isn't that a Java class? This is the C# forum...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I'd go with string.Concat .
string key = string.Concat("test", "::", "test");
|
|
|
|
|
try STRINGBUILDER function for that
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
If that's all you're doing, I'd stick with method 1. (And let the compiler handle.)
I often use method 2 when concatenating literal text with variables; I find it easier to read (when a lot of space is added).
But if you're doing a bunch of concatenations in a loop (to produce one value), use StringBuilder.
Also look into String.Join
|
|
|
|
|
I believe the 2nd example uses a StringBuilder internally, and for large strings, with multiple instances of addition, it's more efficient. I almost always use string.Format over "string + string ".
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: with multiple instances of addition, it's more efficient
That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: John Simmons / outlaw programmer wrote:
with multiple instances of addition, it's more efficient
That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.
And I told him *when* using string.Format is more efficient.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Before you decide on a method to perform the concatenation, see this[^] link.
/ravi
|
|
|
|
|
Your method 1 is absolutely the most efficient. It concatenates the strings while compiling, so the only work done at runtime is allocating a reference of an already existing string to the variable.
I suppose that you meant to concatenate string values from variables, not literal strings, so there is actually something done at runtime:
Method 1:
string key = str1 + "::" + str2;
Method 2:
string key = string.Format("{0}::{1}", str1, str2);
Method 1 is still the most efficient. It will compile into a call to the String.Concat(string, string, string) method, which adds the lengths of the strings, allocates a string with that size, and copies the data from the strings into the new string.
Method 2 uses a StringBuilder , which is overkill when you just want to concatenate three strings. It calls the AppendFormat method which has to parse the format string to know what to do with the strings.
Using the String.Format method often produces more readable code, and it can certainly do a lot more than just concatenating strings, but for your specific question it's not the most efficient.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Thursday, January 1, 2009 9:04 AM
|
|
|
|
|
Hi friends i have write a window service which monitor the Server's and send the data to a web server. in this service i get the Event of the System from the Event log and check the Entry of each Event and compare it Creation with last update date if it is greater then send it to the web server.
This is Code
foreach (EventLog log in logs)
{
foreach (EventLogEntry entry in log.Entries)
{
//we are only interested in bad events
//check that the entry is newer than the last update timestamp
//Add the Entry to Xml
}
}
when i run this code on server Window Xp its Fine.
but when i run this code on Window Server 2003 it take 1 minute know i don't know either there is some problem in my Algo or there is some Differnt l0gic used to get the Event of Window Server 2003
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
Open up the eventlog of your server, and do a count. How many events are you for-eaching through?
BTW, you might want to add some code to measure the time that it takes to complete a single for-each. That gives an indication of the time required to process an n-amount of events.
Second BTW, you might want to check out the AForge DLL that can be found on CodeProject. It can transform your foreach into a parallel-version that uses multiple cpu-cores, speeding up the foreach (if the server has multiple cores).
I are troll
|
|
|
|
|
Hi
I need to convert c code to C#,
I need to open a file, read strings and init local members.
The file is composed of strings and ‘\0’ between each string
How can I read string while the indication of end of string is ‘\0’?
The c code is
int MAX_STR = 200;
byte[] comment = new byte[MAX_STR];
fread( (char*)& comment, sizeof(comment),1,file);
C# code that I Try is
int MAX_STR = 200;
byte[] comment = new byte[MAX_STR];
comment = m_brSWL.ReadBytes(MAX_STR);
but the array is not init with the string
I also try readChar but it read also the second string , not till uit reach ‘\0’
Any Idea?
Thanks
Ronen
|
|
|
|
|
I don't have such a file at hand, but try this:
using (StreamReader sr = new StreamReader("file")) {
string content = sr.ReadToEnd();
string[] strings = content.Split('\0');
}
Regards,
Lev
|
|
|
|
|
It appears that you are trying to read the file as binary (the file was created by a legacy system?), so try the System.IO.BinaryReader 's ReadString method, I haven't tried it (yet), but I suspect it may work.
[Edit: I've tried it, it expects the new kind of string, not a null-terminated string. ]
On the other hand, I'd more likely read in each byte separately.
modified on Wednesday, December 31, 2008 11:15 AM
|
|
|
|
|
Here's a fairly simple method:
public static string
ReadString
(
System.IO.Stream Stream
,
byte Terminator
)
{
string result = null ;
System.Collections.Generic.List<byte> bytes =
new System.Collections.Generic.List<byte>() ;
int temp ;
while ( true )
{
temp = Stream.ReadByte() ;
if ( ( temp == Terminator ) || ( temp == -1 ))
{
break ;
}
bytes.Add ( (byte) temp ) ;
}
if ( bytes.Count > 0 )
{
result = System.Text.Encoding.ASCII.GetString ( bytes.ToArray() ) ;
}
else
{
if ( temp == Terminator )
{
result = "" ;
}
}
return ( result ) ;
}
You can convert it to an Extension Method if you like.
|
|
|
|
|
|
i have a check box list control in winform.i want to get index of an item by searching with datavaluemember. i will pass data value member as input.
hi
|
|
|
|
|
hi, i am going to provide a smart way of locking the computer screen and when someone wanna use the pc should provide username and password(i don't mean the windows lock! just the screen lock.)
for exammple after loging in, the screen comes up and have 2 fiels(user name and password) .
i think that this is a windows form that have the property of top most to true and the border style to false, but it doesn't satisfy me
it should also "locked": disable key combinations like ctrl+alt+del, alt+tab, etc
any one have any idea? thanks
|
|
|
|
|
You can't disable ctrl+alt+del. For other combination you will need to use windows hooks. There are several article here at codeproject that show how to use global keyboard hooks from c#.
|
|
|
|
|
How to disable ctrl+alt+del...
[^]
|
|
|
|
|
As the author states it's a workaround, not a solution. In your code you can't detect when alt-ctrl-delete is clicked.
|
|
|
|
|