extracting a character from a string

J

John

I want to compare a character from a string by doing something like
this:

Dim txt As String
txt = "beware"
If txt(0) = "b" Then MsgBox "This Works OK"

How do I access a character from a string? I tried using txt(0) and
txt[0] (as I would in the C language) but got an error.

Also, I'm assuming that the first character of a string is at postion
0 rather than 1 (as it is for an array by default). Is this correct?
 
J

Jezebel

VB/VBA doesn't automatically treat strings as character arrays; and
character positions count from 1.

You could use

If left$(txt,1) = "b" then

or

If txt like "b*" then
 
J

John

Thanks. It sounds very inefficient and slow for such a basic
operation, but if that's the way it has to be done then I'll have to
do it that way!

How do I extract a character from within a string, eg the third
character of txt? Updating my incorrect example, how would I extract
the "w" of "beware"...


Jezebel said:
VB/VBA doesn't automatically treat strings as character arrays; and
character positions count from 1.

You could use

If left$(txt,1) = "b" then

or

If txt like "b*" then






John said:
I want to compare a character from a string by doing something like
this:

Dim txt As String
txt = "beware"
If txt(0) = "b" Then MsgBox "This Works OK"

How do I access a character from a string? I tried using txt(0) and
txt[0] (as I would in the C language) but got an error.

Also, I'm assuming that the first character of a string is at postion
0 rather than 1 (as it is for an array by default). Is this correct?
 
J

Jean-Guy Marcil

John was telling us:
John nous racontait que :
Thanks. It sounds very inefficient and slow for such a basic
operation, but if that's the way it has to be done then I'll have to
do it that way!

Every language is different. And don't worry, it is not "slow"!
How do I extract a character from within a string, eg the third
character of txt? Updating my incorrect example, how would I extract
the "w" of "beware"...

If Mid$(txt, 3, 1) = "w" Then...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

John

Thanks, that's great. I have a few more questions...

1) How do I put a character into a string, replacing the character
that was already there. Eg, using incorrect VB to make txt equal to
"beyare":

Dim txt As String
txt = "beware"
txt(3) = "y"


2) If I want to add something to the end of a string, can I use

txt = txt & "end bit"


3) In C, I can shorten a string by putting a '\0' in the character
position where I want the new end to be. Is there a way to do this in
VB?

Eg txt = "beware" - is there a quick way to make it equal to "be" by
putting an end character (if there is such a thing in VB) in character
position 3?
 
M

Martin Seelhofer

Hi John
1) How do I put a character into a string, replacing the character
that was already there. Eg, using incorrect VB to make txt equal to
"beyare":

Dim txt As String
txt = "beware"
txt(3) = "y"

You're almost there:

Dim txt As String
txt = "beware"
Mid(txt,3,1) = "y"
2) If I want to add something to the end of a string, can I use

txt = txt & "end bit"
yes.

3) In C, I can shorten a string by putting a '\0' in the character
position where I want the new end to be. Is there a way to do this in
VB?

Eg txt = "beware" - is there a quick way to make it equal to "be" by
putting an end character (if there is such a thing in VB) in character
position 3?

The short answer is no. However, to achieve the same result,
you can simply cut away the remaining chars using VB's Left-
function:

txt = Left("beware",2) ' => "be"

A few more examples:

txt = Left("beware",3) ' => "bew"
txt = Right("beware", 1) ' => "e"
txt = Right("beware", 3) ' => "are"
txt = Mid("beware", 4) ' => "are"
txt = Mid("beware", 3, 2) ' => "wa"
pos = Instr("beware", "w") ' => 3
pos = Instr(5, "Hello world", "l") ' => 11
(translates to: find the 1-based character position of the first
found 'l' starting the search at the fifth character)


Cheers,
Martin
 
J

John

Thanks everyone for your help at various points in this thread. My
knowledge of manipulating strings in VB is now much better.
 
A

An Hong Phan

Hello,

I'm trying to get Text from Word doc by calling

Range.Text

Most of the time it returns the right string, however, For Wingdings font,
all I get out of it is a series of "?????"

How do I get the text or character codes for Wingdings text?

Thanks.
 
K

Klaus Linke

More an anecdote than something you'd use much in Word would be a byte
array, which may sometimes be useful if you are more interested in the
actual codes of the characters (= if you're using Asc or AscW all the time):

Dim b() As Byte
Dim strVar As String, i As Long
strVar = "Hello, world?"
strVar = StrConv(strVar, vbFromUnicode)
b = strVar
For i = LBound(b) To UBound(b)
Debug.Print b(i) , Chr(b(i))
Next i
b(UBound(b)) = AscW("!")
strVar = b
MsgBox StrConv(strVar, vbUnicode)

It's not terribly useful any more because Word uses Unicode strings, and the
assignments from string to array or from array to string don't work for
"integer" arrays (arrays of 2-byte items), AFAIK.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top