PDA

View Full Version : Using UCase function within Replace function



kona
12-18-2001, 08:48 PM
I'm new to scripting and have come accross a problem... I want to use the Replace () function on a string which has both upper and lower case characters. To replace a particular word which could consist of upper/lower/mixed with another fixed word/symbol, i decided to use:

Replace(UCase(Original_String), Search_String, Replace_String)

The only problem with that is that it actually changes 'Original_String' permanently to UpperCase, rather than the intended temporary change to uppercase for the duration of the opperation. Presumably there is a way round this to retain the case of the 'Original_String'?

Kona

coolsights2000
12-19-2001, 07:53 AM
what you have to do split the string into section left right and middle!! this one finds one charactor and change the charator to "_" you can replace that with the UCase(Mid$(SaveToFilenameText, Z, 1)) command

just remember where the 1 is used for the len() you will have to change it to the len of the word you are looking for...

and the left and right string will have to point to..
left will have to one before the (start of the word-1)
right will have to one after the (start of the word + end of word +1)



you can modify it to look for a whole word

For Z = 1 To Len(SaveToFilenameText)
Select Case Mid$(SaveToFilenameText, Z, 1)
Case "\", "/", ":", ".", "*", "?", "|", ">", "<", Chr(34)
SaveToFilenameText =Left$(SaveToFilenameText, Z - 1) & "_" & Right$(SaveToFilenameText, Len(SaveToFilenameText) - Z)
End Select
Next Z

Thanks
Mac!!!

This Is just my opinion
So if it stinks wait for another one
Cause I'm no expert

adamdelves
12-20-2001, 11:29 PM
To prevent the string from being changed use just add this extra argument to the replace funciton.

origanalString = Replace(origanalString, searchString, replaceString, , ,vbTextCompare)

vbTextCompare = will cause the replkace function to search for a character regardless of case. You therefore needant convert your variable to uppercase first. leaving the origanal string intact.

kona
12-20-2001, 11:55 PM
Thank you for your reply!
It seemed straight forward admitedly, but it only works with one of those extra commas in place, not all three. And even if just one of them is in place, then it fails to actually work and it still takes the case into account - presumably i'm still doing something stupid wrong?

adamdelves
12-21-2001, 05:40 AM
It didn't work on mine either. This does though:

org = Replace(org, search, replaceIt,1 ,-1,1)

The extra arguments are as follows:

Replace(origanal, searchString, replaceString, startPos, count, comparisonMethod)

the starPos referes to the starting position in which you want the seach to begin ie: 1 if you want to start on the first character

the count argument specifies the number of replacements it makes, when set to -1 it will make all possible replacements

finally there are two comparaison methods

vbBinaryCompare/ 0 causes the replace function to compare the ASCII character codes which are different for each upper and lower case
eg:
A = 65
a = 97
It will only replace the text if these codes match.

vbTextCompare/ 1 causes the replace function to recogonise a & A as the same character and will replace the character if it matches either of the codes