Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

charAt in vb ?? 1

Status
Not open for further replies.

mattscotney

Programmer
Jul 31, 2002
57
AU
Hi all,

myWord = "hello"

Is it possible to see if a character is at a specific position in a string without using an array?

EXAMPLE: If the character at position 2 is an e then change the character at position 5 to an x.

In Javascript you could use the following code:
If myWord.charAt(2) = "e"
{
myWord.charAt(5) = "x"
}


How can I do the same in vb?

Thanks in advance,
Matt Scotney
 
Have a look at the Mid$ function, which can be used to both determine a character (or string) starting at a particular location, and also replaceit with an alternative.
 
strongm,

Could you provide the vb code which does the same as my example Javascript?

Thanks,
Matt Scotney
 
Well, this sort of thing is covered prety well in the help files. However:

If Mid$(myWord, 2, 1) = "e" Then
Mid$(myWord, 5, 1) = "x"
End If
 
Hi,
Try the following function.

Public Function replaceCharacter(ByVal searchedString As String, ByVal searchedCharacter As String, ByVal position As Integer, ByVal replacementCharacter As String) As String
if len(searchedCharacter)> 1 then
Msgbox "Search character can be a single character only."
Exit Sub
endif
If InStr(position, searchedString, searchedCharacter, vbBinaryCompare) <> 0 Then
replaceCharacter = Mid$(searchedString, 1, position - 1) & replacementCharacter & Mid$(searchedString, position + 1, Len(searchedString) - position)
Else
replaceCharacter = searchedString
End If
End Function


This will search for a specified character at a position in a string and if found, replaces that character with a specified replacement character. If not found, returns the original string. Hope it helps. Let me know what happens.
With regards,
PGK
 
Thanks pgk,
But strongm gets the star this time for simplicity!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top