String.Concat vs String.Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Brown

    #16
    Re: String.Concat vs String.Format

    This kind of discussion is the result of too much pre-optimization and
    also trying to steadfastly adhere to such overly generalized rules as
    "concatenat ion is bad!".

    For simple log statements, or when you're concatentation is limited,
    it's fine.

    It's when you are concatenating strings (especially long strings) in
    loops, or doing lots of editing to the string, when you should look for
    alternatives.

    Comment

    • Mark Wilden

      #17
      Re: String.Concat vs String.Format

      "Matthew Brown" <Octavius@gmail .com> wrote in message
      news:1150083110 .288566.205520@ y43g2000cwc.goo glegroups.com.. .[color=blue]
      > This kind of discussion is the result of too much pre-optimization and
      > also trying to steadfastly adhere to such overly generalized rules as
      > "concatenat ion is bad!".
      >
      > For simple log statements, or when you're concatentation is limited,
      > it's fine.
      >
      > It's when you are concatenating strings (especially long strings) in
      > loops, or doing lots of editing to the string, when you should look for
      > alternatives.[/color]

      Actually, we've both read
      <http://www.yoda.arachs ys.com/csharp/stringbuilder.h tml>. :)


      Comment

      • Göran Andersson

        #18
        Re: String.Concat vs String.Format

        Then you shouldn't use either method. Use parameters instead.

        Parameters are somewhat slower than putting the query together yourself,
        but it's easier and safer. As the database call is going to use 99% of
        the execution time of the routine anyway, you are looking in the wrong
        place if you are trying to speed it up by optimizing the creation of the
        query.

        If you want to put together the queries yourself anyway, you should
        escape the characters that the database uses in strings. For MySQL, for
        an example, it is done like this:

        "SomeValue= '" + m_Value.Replace ("\\","\\\\").R eplace("'","\\' ") + "'"

        If you don't, you will encounter problems whenever someone uses any of
        those characters. If you are lucky you only end up with an error
        message. If you are unlucky someone used it for SQL injections, and all
        your data is stolen and/or deleted.


        ramadu wrote:[color=blue]
        > Its mostly for select statements in a DataTable.
        >
        > - Sri
        >
        > :[color=green]
        >> The Concat would be slightly faster and memory efficient, but if you
        >> are trying to optimize your code you are probably looking in the wrong
        >> place.
        >>
        >> What are you going to use the string for?
        >>
        >> ramadu wrote:[color=darkred]
        >>> I know its a sin to use strings, lets skip that part...
        >>>
        >>> Which of these is faster and uses less memory?
        >>>
        >>> String.Format(" SomeValue='{0}' ", m_Value);
        >>>
        >>> or
        >>>
        >>> String.Concat(" SomeValue='", m_Value, "'");
        >>>
        >>> - ramadu[/color][/color][/color]

        Comment

        Working...