|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Ok i have bit of a problem here
i am trying to replace a character within a word for example if the word is InapickleInapickle and i want to change one of the n to to an m how would i go about it and keep the whole word but just change 1 specific character. also not that the "n" was an example, what i want to know is how to replace a character thats in a specific position so that if i wanted to change one of the p's i could do that aswell any help would be appreciated |
|
#2
|
||||
|
||||
|
Quote:
Hi, one way you could do it would be to use everything to the left of that character and concatenate it to your new letter and then add everything to the right of that character, in esscence replacing the specific character: Code:
mystring = inapickleinapickle 'remove the 6th character and add the new one mystring = Left$(mystring, 5) & "C" & Mid(mystring, 7) In this example I am replacing the sixth character, just make the first number = 1 below your number and the second number 1 greater than your chosen position and replace the "C" with your chosen letter. |
|
#3
|
||||
|
||||
|
yep and you can make it into neat function. vbscript syntax:
Code:
Function ReplaceInPosition(strOriginal, iCharPosition, strNew) Dim result result = "" If iCharPosition>1 Then result = result & Left(strOriginal, iCharPosition-1) End If result = result & strNew & Mid(strOriginal, iCharPosition+1) ReplaceInPosition = result End Function usage: Code:
mystring = ReplaceInPosition("inapickleinapickle", 2, "m")
this will replace the 2nd character with "m" letter. |
|
#4
|
||||
|
||||
|
Quote:
Very Slick!!! This is probably bad practice but you dont really need to set result to "" each time, you could just set it in one go: Code:
Function ReplaceInPosition(strOriginal, iCharPosition, strNew)
Dim result
If iCharPosition > 1 Then
result = Left(strOriginal, iCharPosition - 1)
End If
result = result & strNew & Mid(strOriginal, iCharPosition + 1)
ReplaceInPosition = result
End Function
|
|
#5
|
|||
|
|||
|
Geesh thankyou so much!
well thankyou very much! you actually wrote the whole code out for me which was more than i expected although thanks to the original poster who would have solved my problem just as good.
now i dont have to put my words in an array and go looping through each character and replacing every letter indivudually, and then reorganising my list cant tell you how much this has made things easier because the way i was doing it relied on a word having only one of the selected letter |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Having trouble replacing a specific character |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|