Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Pedantically speaking...

Took part in an interesting but highly pedantic discussion about the relative speeds of 4 different ways to remove a trailing comma from a string. It actually started out discussing purely the performance aspect, then drifted into elegance. Of course I came up with the least most efficacious option, but, IMHO the most elegant!! :-D

Anyways, here were the different ways that the group arrived at:

Method One - Simple:


If Right(theString,1) = ","  Then
 theString = Left(theString, Len(theString)-1) 
End If

Method Two - Array (maybe 20x slower):

temp = Split( list, "," ) 
ReDim Preserve temp( UBound(temp)-1 ) 
list = Join( temp, "," ) 

Method Three - RegEx (maybe 30x slower):


Set re = New RegExp
re.Pattern = "\,$"
list = re.Replace( list, "" ) 

Method Four - Switching language contexts (slowest of all):

<% 
Dim myStrng : myStrng = "the,grand,poobah,"
myStrng = RemoveTrailingComma( myStrng ) 
Response.Write myStrng 
%>

<script language="jscript" runat="server"> 
function RemoveTrailingComma( strng ) {     
    return strng.replace( /\,$/, '') ; 
} 
</script>

Oh the fun of it :-) Today I have to read up on creating skins for Windows Media Player 9 for the jukebox app. I've started by reading this nice intro: A simple skin and, of course there's always the Media Player 9 home page: Windows Media Player 9 Series

2 Comments

  • About the way to remove a comma in a &quot;string list&quot; is to put commas in front of the string.


    Instead of building a string list like this :


    myStr = &quot;&quot;


    myStr = myStr &amp; strItem1 &amp; &quot;,&quot;


    myStr = myStr &amp; strItem2 &amp; &quot;,&quot;


    ...


    Let's use :


    myStr = &quot;&quot;


    myStr = myStr &amp; &quot;,&quot; &amp; strItem1


    myStr = myStr &amp; &quot;,&quot; &amp; strItem2


    ...


    Then you can remove the first comma like this :


    myStr = Mid(myStr, 2)

  • How much more performant is that? You'd still have to wrap the result in a test to see that the length of the &quot;myStr&quot; variable was &gt; 0; and how big would the string get before you did some sort of optimization on the string building process itself anyway? Hmmmmm? :P

Comments have been disabled for this content.