PDA

View Full Version : Strings Query



john-86
03-09-2006, 05:38 AM
Hello all,
I'm afraid I may be making a few postings on this forum over the next few days as I continue to develop my application (which seems to be getting more complex by the day!).

The first language I learned properly (bar some COMOL in highschool) was JavaScript. I (maybe wrongly) try and relate methods and properties in JS to VBA. I was wondering if there was a VBA equivalent of the JS charAt() property.

For those of you who may be unfamiliar with JS I'll try and explain with a demo.

If we declare a string variable called "str" and assign it the value of "bread" then str.charAt(0) would return "b". Each character in the string is indexed (using 0-indexing) so each character within a string can be referenced.

Sorry if I haven't explained this very well. If anyone would like me to try again please don't hesitate to ask.

Any help with this would, again, be greatly appreciated.

Kind regards,
John

john-86
03-09-2006, 06:34 AM
I just thought that maybe seeing the context of how I plan to use this may help some understand the problem.

Here is the code I've written so far... there is one line which is incomplete which is where I need to use the afore mentioned property.


Function removeCharFromFrontOfString(str, x)
''' Function takes two arguments. x is the number of characters to
''' be removed from the string. str is the string from which
''' characters are to be removed. Function returns a string.

Dim outputString As String
Dim index As Integer
Dim outputIndex As Integer

index = 0
outputString = ""
outputIndex = x

Do While (index < (str.FieldSize - x))
ouputString = outputString & **!!missing code!!**
index = index + 1
outputIndex = outputIndex + 1
Loop

End Function

For you JSers out there the missing code (in JS) would go like this:
str.charAt(outputIndex)

Hope that helps.

Thanks,
John

Tommy
03-10-2006, 09:33 AM
Hi John,

If I am understanding correctly,


Function removeCharFromFrontOfString(str As String, x As Long) As String
removeCharFromFrontOfString = Mid$(str, x + 1)
End Function



will return what you wish.

john-86
03-13-2006, 04:51 AM
Thank you Tommy that is very helpful!

Kind regards,
John