views:

2022

answers:

9

Hi, What's the best way of adding spaces between strings

myString = string.Concat("a"," ","b")

or

myString = string.Concat("a",Chr(9),"b")

I am using stringbuilder to build an XML file and looking for something efficient.

Thanks

Edit ~ Language VB.NET

+8  A: 

Create your XML file with the XmlDocument class. Your wasting your time creating a string from scratch.

Sir Psycho
Maybe not XmlDocument, but _something_ from the System.Xml namespace or related: XmlWriter, Dataset.WriteXml(), etc.
Joel Coehoorn
Totally agree that you're better off using either an XmlTextWriter or XmlDocument. Don't reinvent the wheel.
Robert Paulson
it depends how "big" the XML doc is, but in general I think this should be the accepted answer. Unless you have a good reason not to, use the built in XML generation tools in your language!
Jeff Atwood
+5  A: 

String.Join is a static method that can take the separator (in this case, " ") and an array of strings.

string sentence = String.Join(" ", new string[] { "The", "quick", "brown", "fox" });
defeated
In this case, his string variable, isn't a string, but a StringBuilder. For a large string that's not finished yet and will keep growing, that's gonna be _much_ more efficient than String.Join()
Joel Coehoorn
Ow, ow, but then you'd have to allocate an array. :-(
Edward Z. Yang
I think the extra allocation of array is irrelevant as string.Concat(Object[] param) will create an array before calling the method anyway. If there is more than 2 strings to be joined with a space in between, String.Join would be best choice.
Samuel Kim
StringBuilder doesn't have a Concat method does it?
ICR
+1  A: 

Definitely not Chr(9). Not everyone uses ascii, after all.

Joel Coehoorn
chr(9) is the same in ASCII and Unicode (as is chr(32) which is what the questioner meant to say, I believe).
paxdiablo
@Pax: But let's us not forget about EBCDIC.
James Curran
@James: That's less funny than you think - I do a fair bit of work on the System z mainframes that still use EBCDIC. Their UNIX System Services is an abomination (EBCDIC under the covers and RACF instead of /etc/passwd); oh well, at least they have zLinux.
paxdiablo
A: 

As an alternative to the "tranditional" XMLDocument , if you're using .NET 3.5 and up, take a look at the new XDocument / XElement classes in LINQ.

A good tutorial is here:

http://www.hookedonlinq.com/Print.aspx?Page=LINQtoXML5MinuteOverview

defeated
A: 

Isn't Chr(9) a tab, anyway?

Alf Zimmerman
+4  A: 

Well, for a start, chr(9) is a tab character - you would want to use chr(32) to get a space.

That said, the first option, string.Concat("a"," ","b"), is a more readable one. I would be concentrating on getting your code functionally correct to start with. Optimization should always be a last step and targeted only to those areas that need it. In other words, you need a baseline to check your optimizations against.

Far too many times, you optimize then find yourself having to change the code anyway, meaning that your optimization effort was wasted.

paxdiablo
Well said...Thank you.
Saif Khan
A: 

The simplest way is to use an aggregate function to combine the elements.

string[] input = new string[]{"a", "b"};
var withSpaces = input.Aggregate( (x,y) => x + " " + );
JaredPar
A: 

I don't see where in the code example in the question there is a stringbuilder; but since you say you are using one; I would use:

sb.AppendFormat("{0} {1}", a, b);

DancesWithBamboo
string (as in string.Concat()) is a variable, not a type.
Joel Coehoorn
stringVar.Concat is an extension method that takes an IEnumerable<string>. The only method with a signature that matches the signature he's using is the static method Concat on the type string. Unless I'm misunderstanding you Joel.
ICR
+1  A: 

If you're concatenating a known number of strings it's probably better to just use + as the compiler translates it into a call to string.Concat anyway. So

s = a + " " + b

becomes

s = string.Concat(a, " ", b)

But the first is a lot more readable. Though the usual caveat, StringBuilders are generally preferable when doing this in a loop.

Using Chr(32) over " " will make no difference speed wise as in this case Chr(x) is translated at compile time in VB.Net (don't know if it always is, but on my machine it did) so you're just making it more difficult to read with no benifit. Chr is mainly there for backwards compatibility and is generally best used for defining characters outside of the printable range.

That said, it's probably better to use one of the framework library to build XML unless it's a very small fragment.

ICR
I was a samall fragment extracted from a dataset. I agree with your comments also, although I read somewhere a while ago, it's better off using the String.Concat.
Saif Khan
Their justification was probably that String.Concat doesn't suffer from string immutability in quite the same way. However, if you look at what the compiler produced a + " " + b becomes string.Concat anyway so it just comes down to whats more readable.
ICR