Adding spaces between strings
Solution 1
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.
Solution 2
Create your XML file with the XmlDocument class. Your wasting your time creating a string from scratch.
Solution 3
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" });
Solution 4
Dim TestString As String
' Returns a string with 10 spaces.
TestString = Space(10)
' Inserts 10 spaces between two strings.
TestString = "Hello" & Space(10) & "World"
Solution 5
Definitely not Chr(9). Not everyone uses ascii, after all.
Saif Khan
Updated on June 13, 2022Comments
-
Saif Khan almost 2 years
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
-
Admin over 15 yearsHow about specifying the language in your query?
-
ICR over 15 yearsI assume VB, as they're using Chr which is a VB function.
-
-
Joel Coehoorn over 15 yearsMaybe not XmlDocument, but something from the System.Xml namespace or related: XmlWriter, Dataset.WriteXml(), etc.
-
Joel Coehoorn over 15 yearsIn 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()
-
paxdiablo over 15 yearschr(9) is the same in ASCII and Unicode (as is chr(32) which is what the questioner meant to say, I believe).
-
Ashwin over 15 yearsTotally agree that you're better off using either an XmlTextWriter or XmlDocument. Don't reinvent the wheel.
-
Edward Z. Yang over 15 yearsOw, ow, but then you'd have to allocate an array. :-(
-
James Curran over 15 years@Pax: But let's us not forget about EBCDIC.
-
paxdiablo over 15 years@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.
-
Samuel Kim over 15 yearsI 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.
-
Peter Burns over 15 yearsit 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!
-
Joel Coehoorn over 15 yearsstring (as in string.Concat()) is a variable, not a type.
-
ICR over 15 yearsstringVar.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 over 15 yearsStringBuilder doesn't have a Concat method does it?
-
Saif Khan over 15 yearsI 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.
-
ICR over 15 yearsTheir 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.