Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mr Teatime

Accessing the individual characters of a string in VB.

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am completely new to visual basic and would like to know how to get access to the individual characters of a string. You dont appear to be able to declare character variables, so I was just wondering how this can be achieved otherwise in Visual Basic. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Dim MyVariable As String

MyVariable = "hello world"

MsgBox MyVariable

MsgBox Left(MyVariable, 1)

MsgBox Left(MyVariable, 5)

MsgBox Right(MyVariable, 5)

''Hope this helps you,
''-Rich

Share this post


Link to post
Share on other sites
Please use Left$(), Right$(), and Mid$()... that makes it return Strings, instead of Variants. I recommend you dont use Variants unless you need to.

Later,
Lord Hen

"I am free of all prejudices. I hate everyone equally." - W. C. Fields

[edited by - Lord Hen on December 2, 2008 2:32:49 AM]

Share this post


Link to post
Share on other sites
Yeah, get Project Analyser and use it to teach you good VB practices like this. Very useful for anyone from beginner to advanced.

Share this post


Link to post
Share on other sites
Another option is to simply assign the string to a byte array, and directly access the elements in the byte array. An assign from a string to a byte array will perform a copy, you dont need to dim the byte array first (VB handles this). VB strings use double bytes (unicode ready) so 2 bytes will be used in the byte array for each chaacter.


Dim strText As String
Dim arrayText() As Byte

strText = "ABC Hello"

arrayText = strText

MsgBox arrayText(0) ''65 (A)
MsgBox arrayText(2) ''66 (B)
MsgBox arrayText(4) ''67 (C)

MsgBox LBound(arrayText) ''lower bound = 0
MsgBox UBound(arrayText) ''lower bound = 17


This is useful for lower level speedier operations, for unicode work, and for those with C backgrounds who are more familiar with strings as arrays of chars.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!