Adding spaces between strings

15,600

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.

Share:
15,600
Saif Khan
Author by

Saif Khan

Updated on June 13, 2022

Comments

  • Saif Khan
    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
      Admin over 15 years
      How about specifying the language in your query?
    • ICR
      ICR over 15 years
      I assume VB, as they're using Chr which is a VB function.
  • Joel Coehoorn
    Joel Coehoorn over 15 years
    Maybe not XmlDocument, but something from the System.Xml namespace or related: XmlWriter, Dataset.WriteXml(), etc.
  • Joel Coehoorn
    Joel Coehoorn over 15 years
    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()
  • paxdiablo
    paxdiablo over 15 years
    chr(9) is the same in ASCII and Unicode (as is chr(32) which is what the questioner meant to say, I believe).
  • Ashwin
    Ashwin over 15 years
    Totally agree that you're better off using either an XmlTextWriter or XmlDocument. Don't reinvent the wheel.
  • Edward Z. Yang
    Edward Z. Yang over 15 years
    Ow, ow, but then you'd have to allocate an array. :-(
  • James Curran
    James Curran over 15 years
    @Pax: But let's us not forget about EBCDIC.
  • paxdiablo
    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
    Samuel Kim over 15 years
    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.
  • Peter Burns
    Peter Burns over 15 years
    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!
  • Joel Coehoorn
    Joel Coehoorn over 15 years
    string (as in string.Concat()) is a variable, not a type.
  • ICR
    ICR over 15 years
    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
    ICR over 15 years
    StringBuilder doesn't have a Concat method does it?
  • Saif Khan
    Saif Khan over 15 years
    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.
  • ICR
    ICR over 15 years
    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.