function Instr

H

helpasap

I have the following string:
"test01 test02 test03"
Now I want to get, with the instr function, the first word of the string
'test01'
I use: word01=instr(1,"test01 test02 test03"," ",vbTextCompare)
But I got not the result "test01".

Who can help me?

Jan
 
J

Jonathan West

Hi Jan,

Instr doesn't do this. You need the Split function

Dim vSplit as Variant
vSplit = Split("test01 test02 test03", " ")
word01 = vSplit(0)

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jay Freedman

Hi Jan,

To continue your education, the Instr function returns the position -- that
is, the index counting from the first character -- of the first occurrence
of the character or string in the third argument. Because the space
character occurs in the seventh character of the string in your example,
Instr would return the number 7.

This code is an alternative to Jonathan's, and may help you to see what's
happening:

Dim nPosition As Long
Dim OriginalString As String
Dim FinalString As String

OriginalString = "test01 test02 test03"

nPosition = InStr(1, OriginalString, " ", vbTextCompare)
If nPosition > 0 Then
FinalString = Left$(OriginalString, nPosition)
MsgBox "First space at position " & nPosition & _
vbCr & "First word = " & FinalString
Else
MsgBox OriginalString & " does not contain a space"
End If
 
H

helpasap

Hallo

It still doesn't work.

I have declared the function GetPrivateProfileString (kernel32.dll)

sub test
sString = String(255,"*")
dSize = len(sString)
dSizeSting =
GetPrivateProfileString(vbNullstring,"","",sString,dSize,sIniname)
sString=left(sString,dSizeString)
debug.print instr(1,sString," ",vbTextCompare)
end sub

If I have a debug.print left(sString,dSizeString), It gives the following
result:
"test01 test02 test03"
This means that the declare function PrivateProfileString is working.
the debug.print instr(1,sString," ",vbTextCompare) the result is 0 instead
of 7.

If I do this:
sString = "test01 test02 test03"
debug.print instr(1,sString," ",vbTextCompare)
then the result is: 7

The SPLIT-function gives the same results the first code is incorrect the
second code correct.

Greetings
Jan



"Jay Freedman" schreef:
 
T

Tony Jollans

It would appear you don't have normal spaces in your string. What is the
result from ..

Debug.print ascw(mid$(sString,7,1))

I would guess the most likely results are 0 (=null char) or 160 (=hard
space). When you know what character you are dealing with you can alter your
code to use it.
 
H

helpasap

Thank You Tony

That's the solution of my problem.
The code is:
debug.print instr(1,sString,chr(0),vbTextCompare)

result:=7

Perfect


"Tony Jollans" schreef:
 
T

Tony Jollans

Graag gedaan

--
Enjoy,
Tony


helpasap said:
Thank You Tony

That's the solution of my problem.
The code is:
debug.print instr(1,sString,chr(0),vbTextCompare)

result:=7

Perfect


"Tony Jollans" schreef:
 

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