Replace character in string

202 views
Skip to first unread message

esrispy

unread,
Nov 22, 2007, 9:34:40 AM11/22/07
to MapInfo-L
Hello
Is there some "string replace" functionality in MapBasic

I have a stringvariable and if it contains a Space Character I want to
convert it to %20
(URL encode)

Something as below in VB

Replace(myString," ","%20")


esrispy

unread,
Nov 22, 2007, 10:22:29 AM11/22/07
to MapInfo-L
I got it found some similar problem :-)

'first convert to lowercase

StrValue = LCase$(StrValue)
'then convert the spaces
StrValue = SpacesToUrl(StrValue)



Function ReplaceCharAtPosition(ByVal StrValue As String, ByVal
Position As Integer, ByVal ReplaceWith As String) As String
Dim StrBegin As String
Dim StrEnd As String
StrBegin = Left$(StrValue, Position - 1)
StrEnd = Right$(StrValue, Len(StrValue) - Position)
ReplaceCharAtPosition = StrBegin & ReplaceWith & StrEnd
End Function

Function SpacesToUrl(ByVal StrValue As String) As String
Dim Ptr as Integer
Ptr = InStr(1, StrValue, " ")
While (Ptr > 0)
StrValue = ReplaceCharAtPosition(StrValue, Ptr, "%20")
Ptr = InStr(1, StrValue, " ")
Wend
SpacesToUrl = StrValue
End Function

CB

unread,
Nov 22, 2007, 11:17:35 AM11/22/07
to MapInfo-L
There is nothing built in to MapBasic, but you might try something
like this:

' Replace any space characters with '_'
Sub replaceSpaceWithScore(str as string)

Dim posSpace as integer
Dim lastSpace as integer
Dim posStart as Integer
Dim newStr as String
Dim copy as string

copy = str

newStr = ""
posStart = 1
posSpace = Instr(1, copy, " ")
lastSpace = posSpace

While posSpace <> 0
lastSpace = posSpace
newStr = newStr + Mid$(copy, posStart, posSpace - 1)
newStr = newStr + "_"

posStart = posSpace + 1
posSpace = Instr(posStart, copy, " ")
Wend

' if we found a space, we want to copy the rest of the string
If lastSpace <> 0 Then
newStr = newStr + Mid$(copy, lastSpace + 1, Len(copy) -
lastSpace)
Else
newStr = copy
End If

str = newStr

End Sub

It's very specific, but should be easy enough to generalize.

Charles Bates
Pitney Bowes MapInfo Corp.
Reply all
Reply to author
Forward
0 new messages