String manipulation problem

Results 1 to 2 of 2

Thread: String manipulation problem

  1. #1
    Join Date
    Dec 1969

    Default String manipulation problem

    I need to create a function to add a space where all uppercase characters exists in a string (except the first)<BR><BR>eg HowAreYou becomes How Are You<BR><BR>Being a newbie am having difficulty splitting the string into an array. This is the code thus far, can anyone point out my problem. <BR><BR>Function displayName(strName)<BR>&#039;purpose format a database name for display<BR><BR>Dim character<BR><BR> &#039;cut off first three characters<BR> strDBFieldName = colName(strName)<BR><BR> &#039;put spaces where capitals are used (after the first) <BR> for character = 1 to len(strName)-1<BR> if chr(strName(character)) &#062; 64 and chr(character) &#060; 91 then &#039;is uppercase<BR> displayName = displayName + " "<BR> end if<BR> displayName = displayName + strName(character)<BR> Next<BR>End Function<BR><BR>Thanks<BR><BR>Ian<BR>

  2. #2
    Join Date
    Dec 1969

    Default ASC, not CHR!

    You are using the wrong function! CHR is used to convert a number to a character. You want to go the other way!<BR><BR>But you don&#039;t *really* need either function (see below).<BR><BR>You also have to use the MID function to get single characters from a string. You appear to be used to C or C++ or JavaScript? Sorry...not as simple with VBS.<BR><BR>Also, you need to go all the way to the end of the string. You do *NOT* use a -1 with VBScript (ah...more evidence of a JS/C/C++/Java upbringing, yes?). Oh...I just saw why you did that: You also assumed the character numbering of a string starts at zero. Nope. Starts at 1.<BR><BR>ALSO... *Always* use the & operator for string concatenation. The + operator *usually* works, but when it doesn&#039;t you&#039;ll find yourself spending hours tracking down the bug.<BR><BR>ALSO... You get better performance using a local variable to build the function result and then only assigning to the function name last thing. (Minor point, but since we are going to all this trouble...)<BR><BR>SO...<BR><BR>&#060;%<BR>Functio n displayName(strName)<BR>&nbsp; &nbsp; &#039;purpose format a database name for display<BR>&nbsp; &nbsp; Dim cnum, character, temp<BR>&nbsp; &nbsp; &#039;cut off first three characters<BR>&nbsp; &nbsp; &#039; ???? WHAT IS THIS FOR???<BR>&nbsp; &nbsp; strDBFieldName = colName(strName)<BR>&nbsp; &nbsp; &#039; ???? YOU NEVER THEN USE strDBFieldName ???<BR><BR>&nbsp; &nbsp; &#039;put spaces where capitals are used (after the first) <BR>&nbsp; &nbsp; temp = Left( strName, 1 ) &#039; initialize the result!<BR>&nbsp; &nbsp; For cnum = 2 to Len(strName)<BR>&nbsp; &nbsp; &nbsp; &nbsp; character = Mid( strName, cnum, 1 )<BR>&nbsp; &nbsp; &nbsp; &nbsp; If character &#062;= "A" AND character &#060;= "Z" Then temp = temp & " "<BR>&nbsp; &nbsp; &nbsp; &nbsp; temp = temp & character<BR>&nbsp; &nbsp; Next<BR>&nbsp; &nbsp; displayName = temp<BR>End Function<BR>%&#062;<BR><BR>See? No need for either ASC or CHR.<BR><BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts