Re: Characters in Strings
- From: "Chip Pearson" <chip@xxxxxxxxxxxx>
- Date: Mon, 15 Jan 2007 09:34:38 -0600
John,
I should have added that you should become familiar with the InStr and
InStrRev functions, which are VB/VBA's equivalent of C's strstr function.
InStr searches left-to-right and InStrRev searches right-to-left. Both
return the 1-based offset (always from the left) of the first character of
the search-for string within the search-in string. Both return 0 if the
seach-for string is not found.
VB/VBA also has a StrComp function which is essentially the same as C's
strcmp function, except that you can specify whether the comparison it
case-sensitive or case-insensitive.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
"Chip Pearson" <chip@xxxxxxxxxxxx> wrote in message
news:%23OtQ6cLOHHA.2140@xxxxxxxxxxxxxxxxxxxxxxx
John,
To get a single character, use Mid. E.g,.
Dim C As String
C = Mid("abcdefg", 5, 1)
will put "e" in C (5th character, length 1)/
I'm used to strings
being arrays of characters so this seems mad!
Strings in VB/VBA are actually what are called BSTRs, and part of
COM/OLE/Automation. See
http://msdn2.microsoft.com/en-us/library/ms221069.aspx
You can treat them as if they were arrays of characters. You can't use
pointers as you do in C/C++. Instead of a point to a character, you use an
1-based index into the string.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
"John Pritchard" <JohnPritchard@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:9E23E75C-F14B-4999-B95F-E62DCAD9CB40@xxxxxxxxxxxxxxxx
I wanted to read along a string character by character in VB and ended up
with a home made function :-
Public Function GetChr(ByVal InPutStr As String, ByVal position As
Integer)
As String
If position <= Len(InPutStr) Then
GetChr = Right(Left(InPutStr, position), 1)
Else
GetChr = Chr(0)
End If
End Function
Is there an easier (and more efficient) way than this. I'm used to
strings
being arrays of characters so this seems mad!
.
- References:
- Re: Characters in Strings
- From: Chip Pearson
- Re: Characters in Strings
- Prev by Date: File and Don't save changes - Read Only File
- Next by Date: Re: Copy / Paste between workbooks into a specific work*** query
- Previous by thread: Re: Characters in Strings
- Next by thread: File and Don't save changes - Read Only File
- Index(es):